From fc762e6bfdc1ba6039db7cd57fe5ffedd6379433 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Sun, 8 Jan 2023 20:53:28 +0000 Subject: [PATCH] Add olive oil bottle items and the ability to sell them --- characters/player/Player.gd | 42 +++++++++++++++++++++++-- characters/player/Player.tscn | 9 ++++++ items/BaseItem.gd | 43 ++++++++++++++++++++++++++ items/BaseItem.tscn | 6 ++++ items/ItemOliveOil.tscn | 20 ++++++++++++ project.godot | 6 ++++ scenery/facilities/Press.gd | 16 ++++++++++ scenery/facilities/Press.tscn | 15 ++++++++- scenery/facilities/Press_BottleArea.gd | 4 +++ vehicles/Postie.gd | 15 +++++---- 10 files changed, 166 insertions(+), 10 deletions(-) create mode 100644 items/BaseItem.gd create mode 100644 items/BaseItem.tscn create mode 100644 items/ItemOliveOil.tscn create mode 100644 scenery/facilities/Press_BottleArea.gd diff --git a/characters/player/Player.gd b/characters/player/Player.gd index 6e71e2f..64e2165 100644 --- a/characters/player/Player.gd +++ b/characters/player/Player.gd @@ -5,9 +5,28 @@ const SPEED_MULTIPLIER_WHEN_ATTACHED = 0.7 export var speed = 200 # How fast the player will move (pixels/sec). var screen_size # Size of the game window. +var item_holders = [] + +var direction = Vector2.ZERO + # Called when the node enters the scene tree for the first time. func _ready(): screen_size = get_viewport_rect().size + item_holders = [$ItemHolder1, $ItemHolder2] + +var _is_h_flipped = false + +func _set_h_flipped(flip: bool): + if flip == _is_h_flipped: + return + + _is_h_flipped = flip + + $AnimatedSprite.flip_h = flip + $ItemHolder1.position.x *= -1 + $ItemHolder1.scale.x *= -1 + $ItemHolder1.rotation *= -1 + func _physics_process(_delta): var velocity = Vector2.ZERO # The player's movement vector. @@ -15,7 +34,7 @@ func _physics_process(_delta): velocity.y += Input.get_action_strength("move_down") - Input.get_action_strength("move_up") if velocity.length() > 0: - var direction = velocity.normalized() + direction = velocity.normalized() if $PlayerAttachmentManager.attached != null: velocity = direction * speed * SPEED_MULTIPLIER_WHEN_ATTACHED else: @@ -25,13 +44,17 @@ func _physics_process(_delta): var _att_lin_vel = $PlayerAttachmentManager.attached.move_and_slide(velocity) var _lin_vel = self.move_and_slide(velocity) if abs(velocity.x) > 0: - $AnimatedSprite.flip_h = velocity.x < 0 + self._set_h_flipped(velocity.x < 0) + $InteractionArea.rotation = direction.angle() else: $AnimatedSprite.stop() func _process(_delta): + if Input.is_action_just_pressed("drop"): + _act_drop() + if Input.is_action_just_pressed("interact"): print("interact") @@ -51,7 +74,14 @@ func _process(_delta): if body.is_in_group("interactable"): print("interactable!") - body.interact(self) + if body.interact(self): + return + +func _act_drop(): + if $ItemHolder1.get_child_count() < 1: + return + var child = $ItemHolder1.get_child(0) + child.drop(self, direction * 10) var press_area_press = null @@ -73,7 +103,13 @@ func _crush_jump(): $Tween.interpolate_property($AnimatedSprite, "position:y", 0, -72, 0.3, Tween.TRANS_QUAD, Tween.EASE_OUT) $Tween.interpolate_property($AnimatedSprite, "position:y", -72, 0, 0.3, Tween.TRANS_QUAD, Tween.EASE_IN, 0.3) $Tween.start() + +func has_free_item_slot(): + for slot in [$ItemHolder1, $ItemHolder2]: + if slot.get_child_count() < 1: + return true + return false func _on_Tween_tween_all_completed(): $Tween.remove_all() diff --git a/characters/player/Player.tscn b/characters/player/Player.tscn index d7ffe3d..13da452 100644 --- a/characters/player/Player.tscn +++ b/characters/player/Player.tscn @@ -22,6 +22,15 @@ __meta__ = { "_edit_horizontal_guides_": [ -111.0 ] } +[node name="ItemHolder1" type="Node2D" parent="."] +position = Vector2( 39, -27 ) +rotation = 0.656244 +scale = Vector2( 0.5, 0.5 ) + +[node name="ItemHolder2" type="Node2D" parent="."] +position = Vector2( 8.64267e-07, -36 ) +scale = Vector2( 0.5, 0.5 ) + [node name="AnimatedSprite" type="AnimatedSprite" parent="."] position = Vector2( 0, -17 ) scale = Vector2( 0.05, 0.05 ) diff --git a/items/BaseItem.gd b/items/BaseItem.gd new file mode 100644 index 0000000..f9997f8 --- /dev/null +++ b/items/BaseItem.gd @@ -0,0 +1,43 @@ +extends KinematicBody2D + +var is_held = false + +var sale_value = 10 + +func interact(player: Node2D): + if is_held: + return false + + for i in range(1, 10): + var n = player.get_node_or_null("ItemHolder" + str(i)) + if n == null: + return false + + if n.get_child_count() > 0: + continue + + var cur_parent = self.get_parent() + if cur_parent != null: + cur_parent.remove_child(self) + n.add_child(self) + self._on_picked_up() + is_held = true + self.position = Vector2.ZERO + return true + + return false + +func _on_picked_up(): + pass + +func _on_dropped(): + pass + +func drop(player: Node2D, offset: Vector2): + self._on_dropped() + var cur_parent = self.get_parent() + cur_parent.remove_child(self) + player.get_parent().add_child(self) + self.position = player.position + offset + #var _v = self.move_and_slide(Vector2(1, 0)) + is_held = false diff --git a/items/BaseItem.tscn b/items/BaseItem.tscn new file mode 100644 index 0000000..5caf37a --- /dev/null +++ b/items/BaseItem.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://items/BaseItem.gd" type="Script" id=1] + +[node name="BaseItem" type="Node2D" groups=["interactable"]] +script = ExtResource( 1 ) diff --git a/items/ItemOliveOil.tscn b/items/ItemOliveOil.tscn new file mode 100644 index 0000000..a85af22 --- /dev/null +++ b/items/ItemOliveOil.tscn @@ -0,0 +1,20 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://items/BaseItem.gd" type="Script" id=1] +[ext_resource path="res://items/bottle_oliveoil.png" type="Texture" id=2] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 10, 13.5 ) + +[node name="ItemOliveOil" type="KinematicBody2D" groups=["interactable", "saleable"]] +collision_layer = 18 +collision_mask = 0 +script = ExtResource( 1 ) + +[node name="BottleOliveoil" type="Sprite" parent="."] +position = Vector2( 0, -19 ) +texture = ExtResource( 2 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( 0, -12.5 ) +shape = SubResource( 1 ) diff --git a/project.godot b/project.godot index 884a950..b59c5e7 100644 --- a/project.godot +++ b/project.godot @@ -63,6 +63,12 @@ interact={ , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null) ] } +drop={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":81,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":1,"pressure":0.0,"pressed":false,"script":null) + ] +} [layer_names] diff --git a/scenery/facilities/Press.gd b/scenery/facilities/Press.gd index 5ae5711..6c34306 100644 --- a/scenery/facilities/Press.gd +++ b/scenery/facilities/Press.gd @@ -175,6 +175,22 @@ func add_fresh_olives(num: int): new_olive_sprite.position = Vector2(0, -52) + Vector2(dist, 0).rotated(angle) fresh_olive_sprites.push_back(new_olive_sprite) +func interact_bottle_area(player: Node2D): + if not player.has_free_item_slot(): + return false + + var l = len(bottles) + for i in range(l - 1, -1, -1): + if bottles[i].visible: + bottles[i].visible = false + + var new_olive_oil = preload("res://items/ItemOliveOil.tscn").instance() + new_olive_oil.interact(player) + _next_step() + break + + return true + func _on_CollectionArea_body_exited(body): if body.has_method("on_press_area_exited"): body.on_press_area_exited(self) diff --git a/scenery/facilities/Press.tscn b/scenery/facilities/Press.tscn index 66b3147..b87de34 100644 --- a/scenery/facilities/Press.tscn +++ b/scenery/facilities/Press.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=13 format=2] +[gd_scene load_steps=15 format=2] [ext_resource path="res://items/bottle_oliveoil.png" type="Texture" id=1] [ext_resource path="res://items/bottle_oliveoil_bottle.png" type="Texture" id=2] @@ -10,6 +10,7 @@ [ext_resource path="res://scenery/facilities/Press.gd" type="Script" id=8] [ext_resource path="res://sfx/spray.wav" type="AudioStream" id=9] [ext_resource path="res://sfx/squish1.wav" type="AudioStream" id=10] +[ext_resource path="res://scenery/facilities/Press_BottleArea.gd" type="Script" id=11] [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 29.5128, 2.79108 ) @@ -17,6 +18,9 @@ extents = Vector2( 29.5128, 2.79108 ) [sub_resource type="RectangleShape2D" id=2] extents = Vector2( 104, 10 ) +[sub_resource type="RectangleShape2D" id=3] +extents = Vector2( 104, 16.5 ) + [node name="Press" type="StaticBody2D"] position = Vector2( 0, -64 ) script = ExtResource( 8 ) @@ -106,6 +110,15 @@ stream = ExtResource( 10 ) [node name="SpraySfx" type="AudioStreamPlayer2D" parent="."] stream = ExtResource( 9 ) +[node name="BottleArea" type="StaticBody2D" parent="." groups=["interactable"]] +collision_layer = 2 +collision_mask = 0 +script = ExtResource( 11 ) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="BottleArea"] +position = Vector2( -10, 44.5 ) +shape = SubResource( 3 ) + [connection signal="tween_all_completed" from="Tween" to="." method="_on_Tween_tween_all_completed"] [connection signal="body_entered" from="CollectionArea" to="." method="_on_CollectionArea_body_entered"] [connection signal="body_exited" from="CollectionArea" to="." method="_on_CollectionArea_body_exited"] diff --git a/scenery/facilities/Press_BottleArea.gd b/scenery/facilities/Press_BottleArea.gd new file mode 100644 index 0000000..b06d767 --- /dev/null +++ b/scenery/facilities/Press_BottleArea.gd @@ -0,0 +1,4 @@ +extends StaticBody2D + +func interact(player): + return get_parent().interact_bottle_area(player) diff --git a/vehicles/Postie.gd b/vehicles/Postie.gd index f11d2d3..bc8d6ea 100644 --- a/vehicles/Postie.gd +++ b/vehicles/Postie.gd @@ -19,7 +19,7 @@ func _ready(): _drive_anim() var state = null - + func _drive_anim(): $Tween.interpolate_property(self, "position", self.position, marker.position, 5.0, Tween.TRANS_CUBIC, Tween.EASE_OUT) state = "driving_in" @@ -51,9 +51,10 @@ func _drive_off(): # Attach bodies that are in the back, if they are sellable. var saleable_bodies = $SaleArea.get_overlapping_bodies() for body in saleable_bodies: - if body.is_in_group("saleable") and body.has_property("sale_value") and body.sale_value != null: + #print("SALEABLE? ", body) + if body.is_in_group("saleable") and body.sale_value != null: var glob_pos = body.global_position - body.get_parent().remove(body) + body.get_parent().remove_child(body) $N/Back/ToSell.add_child(body) body.global_position = glob_pos @@ -78,7 +79,7 @@ func _on_driven_off(): var coin_made = 0 for body in $N/Back/ToSell.get_children(): - if body.is_in_group("saleable") and body.has_property("sale_value") and body.sale_value != null: + if body.is_in_group("saleable") and body.sale_value != null: coin_made += body.sale_value else: print("trying to sell ", body, " but no value!") @@ -98,10 +99,12 @@ var sale_value_so_far = 0 func _on_SaleArea_body_entered(body): - if body.is_in_group("saleable") and body.has_property("sale_value") and body.sale_value != null: + #print("SA enter ", body) + if body.is_in_group("saleable") and body.sale_value != null: sale_value_so_far += body.sale_value func _on_SaleArea_body_exited(body): - if body.is_in_group("saleable") and body.has_property("sale_value") and body.sale_value != null: + #print("SA exit ", body) + if body.is_in_group("saleable") and body.sale_value != null: sale_value_so_far -= body.sale_value