diff --git a/project.godot b/project.godot index 212ecfe..6e5c7d5 100644 --- a/project.godot +++ b/project.godot @@ -20,3 +20,16 @@ config/icon="res://icon.svg" folder_colors={ "res://scripts/": "red" } + +[input] + +shove={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":90,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null) +] +} +shoot={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":77,"key_label":0,"unicode":109,"location":0,"echo":false,"script":null) +] +} diff --git a/scenes/bullet.tscn b/scenes/bullet.tscn new file mode 100644 index 0000000..62d5c31 --- /dev/null +++ b/scenes/bullet.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=3 format=3 uid="uid://c633ux67ehud7"] + +[ext_resource type="Script" uid="uid://d165adohwtkk5" path="res://scripts/bullet.gd" id="1_mkf8s"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_y25gk"] +size = Vector2(14, 6) + +[node name="Area2D" type="Area2D"] +script = ExtResource("1_mkf8s") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_y25gk") +debug_color = Color(0.255636, 0.520768, 0.950461, 0.42) + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/scenes/crate.tscn b/scenes/crate.tscn new file mode 100644 index 0000000..2b44931 --- /dev/null +++ b/scenes/crate.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=3 uid="uid://cym46ub26gs80"] + +[ext_resource type="Script" uid="uid://dqvbrplt605am" path="res://scripts/crate.gd" id="1_b66cd"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_gk4xx"] + +[node name="RigidBody2D" type="RigidBody2D" groups=["pushables"]] +rotation = 1.13734 +script = ExtResource("1_b66cd") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_gk4xx") +debug_color = Color(0.854534, 0.360936, 0.197499, 0.42) diff --git a/scenes/game.tscn b/scenes/game.tscn index 011a3b9..db0366c 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -1,29 +1,26 @@ -[gd_scene load_steps=8 format=3 uid="uid://cwuxdg5c8ylmp"] +[gd_scene load_steps=9 format=3 uid="uid://cwuxdg5c8ylmp"] -[ext_resource type="Script" uid="uid://dmchcjip7pcfj" path="res://scripts/player.gd" id="1_8x5qb"] [ext_resource type="Script" uid="uid://cw3ucen7ivcu3" path="res://scripts/gamecontroller.gd" id="1_wj22x"] +[ext_resource type="Script" uid="uid://bus3b1g717jlm" path="res://scripts/scene_manager.gd" id="2_lbhrr"] +[ext_resource type="PackedScene" uid="uid://cgk1d1f5ffbbd" path="res://scenes/player.tscn" id="2_lnu2h"] [ext_resource type="PackedScene" uid="uid://clf5mhteceas4" path="res://scenes/trigger.tscn" id="3_jlag1"] - -[sub_resource type="CircleShape2D" id="CircleShape2D_ryu2m"] +[ext_resource type="PackedScene" uid="uid://cym46ub26gs80" path="res://scenes/crate.tscn" id="4_iywne"] +[ext_resource type="PackedScene" uid="uid://c633ux67ehud7" path="res://scenes/bullet.tscn" id="6_p57ef"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_um86g"] size = Vector2(55.5, 41) [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_16wxc"] -[sub_resource type="RectangleShape2D" id="RectangleShape2D_gk4xx"] - [node name="Game" type="Node2D"] script = ExtResource("1_wj22x") -[node name="CharacterBody2D" type="CharacterBody2D" parent="."] -position = Vector2(471, 287) -script = ExtResource("1_8x5qb") -metadata/_edit_group_ = true +[node name="SceneManager" type="Node" parent="."] +unique_name_in_owner = true +script = ExtResource("2_lbhrr") -[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] -shape = SubResource("CircleShape2D_ryu2m") -debug_color = Color(0.287381, 0.616681, 0.378827, 0.42) +[node name="CharacterBody2D" parent="." instance=ExtResource("2_lnu2h")] +position = Vector2(471, 287) [node name="level" type="Node2D" parent="."] @@ -59,39 +56,30 @@ shape = SubResource("WorldBoundaryShape2D_16wxc") [node name="crates" type="Node2D" parent="."] -[node name="RigidBody2D" type="RigidBody2D" parent="crates"] -position = Vector2(468, 257) -rotation = 1.13734 -metadata/_edit_group_ = true +[node name="RigidBody2D" parent="crates" instance=ExtResource("4_iywne")] +position = Vector2(407, 407) -[node name="CollisionShape2D" type="CollisionShape2D" parent="crates/RigidBody2D"] -shape = SubResource("RectangleShape2D_gk4xx") -debug_color = Color(0.854534, 0.360936, 0.197499, 0.42) +[node name="RigidBody2D2" parent="crates" instance=ExtResource("4_iywne")] +position = Vector2(508, 303) -[node name="RigidBody2D2" type="RigidBody2D" parent="crates"] -position = Vector2(458, 227) -rotation = 0.693203 -metadata/_edit_group_ = true +[node name="RigidBody2D3" parent="crates" instance=ExtResource("4_iywne")] +position = Vector2(576, 221) -[node name="CollisionShape2D" type="CollisionShape2D" parent="crates/RigidBody2D2"] -shape = SubResource("RectangleShape2D_gk4xx") -debug_color = Color(0.854534, 0.360936, 0.197499, 0.42) +[node name="RigidBody2D4" parent="crates" instance=ExtResource("4_iywne")] +position = Vector2(403, 324) -[node name="RigidBody2D3" type="RigidBody2D" parent="crates"] -position = Vector2(451, 306) -rotation = -2.5155 -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="crates/RigidBody2D3"] -shape = SubResource("RectangleShape2D_gk4xx") -debug_color = Color(0.854534, 0.360936, 0.197499, 0.42) +[node name="RigidBody2D5" parent="crates" instance=ExtResource("4_iywne")] +position = Vector2(357, 331) [node name="Trigger" parent="." instance=ExtResource("3_jlag1")] -position = Vector2(396, 385) +position = Vector2(189, 316) [node name="Trigger2" parent="." instance=ExtResource("3_jlag1")] position = Vector2(638, 390) effect = "teleport" +[node name="Area2D" parent="." instance=ExtResource("6_p57ef")] +position = Vector2(366, 300) + [connection signal="triggerFired" from="Trigger" to="." method="_on_trigger_fired"] [connection signal="triggerFired" from="Trigger2" to="." method="_on_trigger_fired"] diff --git a/scenes/player.tscn b/scenes/player.tscn new file mode 100644 index 0000000..74d6b9b --- /dev/null +++ b/scenes/player.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=3 format=3 uid="uid://cgk1d1f5ffbbd"] + +[ext_resource type="Script" uid="uid://dmchcjip7pcfj" path="res://scripts/player.gd" id="1_3vyb7"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_ryu2m"] + +[node name="CharacterBody2D" type="CharacterBody2D"] +script = ExtResource("1_3vyb7") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_ryu2m") +debug_color = Color(0.287381, 0.616681, 0.378827, 0.42) + +[node name="RightCast" type="RayCast2D" parent="."] +target_position = Vector2(16, 0) + +[node name="LeftCast" type="RayCast2D" parent="."] +target_position = Vector2(-16, 0) + +[node name="RightSpawn" type="Node2D" parent="."] +position = Vector2(12, -5) + +[node name="LeftSpawn" type="Node2D" parent="."] +position = Vector2(-13, -5) diff --git a/scripts/bullet.gd b/scripts/bullet.gd new file mode 100644 index 0000000..a938bcd --- /dev/null +++ b/scripts/bullet.gd @@ -0,0 +1,16 @@ +class_name Bullet extends Area2D +var speed = 700 + +signal bulletHit(body, bullet) + +func setSpeed(value): + speed = value + +func _physics_process(delta: float) -> void: + position += transform.x * speed * delta + + +func _on_body_entered(body: Node2D) -> void: + if not body is Player: + print("Bullet hit") + bulletHit.emit(body, self) diff --git a/scripts/bullet.gd.uid b/scripts/bullet.gd.uid new file mode 100644 index 0000000..0828791 --- /dev/null +++ b/scripts/bullet.gd.uid @@ -0,0 +1 @@ +uid://d165adohwtkk5 diff --git a/scripts/crate.gd b/scripts/crate.gd new file mode 100644 index 0000000..7f42195 --- /dev/null +++ b/scripts/crate.gd @@ -0,0 +1 @@ +class_name Crate extends RigidBody2D diff --git a/scripts/crate.gd.uid b/scripts/crate.gd.uid new file mode 100644 index 0000000..5abdfdd --- /dev/null +++ b/scripts/crate.gd.uid @@ -0,0 +1 @@ +uid://dqvbrplt605am diff --git a/scripts/gamecontroller.gd b/scripts/gamecontroller.gd index 615e98e..d2d0f75 100644 --- a/scripts/gamecontroller.gd +++ b/scripts/gamecontroller.gd @@ -31,3 +31,10 @@ func _on_trigger_fired(effect: Variant, body: Variant) -> void: if totalCrates == 0: print("YOU WIN!") get_tree().reload_current_scene() + +func numberOfCrates(value): + totalCrates = value + print("Total crates at GC: "+str(totalCrates) ) + +func bulletDamage(body, bullet): + print("Game controller knows about bullet hit") diff --git a/scripts/player.gd b/scripts/player.gd index 83368f2..d2ffd83 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -1,16 +1,50 @@ -extends CharacterBody2D +class_name Player extends CharacterBody2D +@onready var right_cast: RayCast2D = $RightCast +@onready var left_cast: RayCast2D = $LeftCast +@onready var right_spawn: Node2D = $RightSpawn +@onready var left_spawn: Node2D = $LeftSpawn const SPEED = 300.0 const JUMP_VELOCITY = -400.0 -@export var pushpower = 80 +@export var bump_power = 80 +@export var shove_power = 500 + +enum FaceDirection{LEFT, RIGHT} +var facing:FaceDirection = FaceDirection.RIGHT + +var pushTarget:RigidBody2D +var pushEnabled:bool = false func _physics_process(delta: float) -> void: # Add the gravity. if not is_on_floor(): velocity += get_gravity() * delta + + handle_input() + move_and_slide() + handle_collisions() + +func handle_input() -> void: + if Input.is_action_just_pressed("shoot"): + print("Shoot a bullet") + if facing == FaceDirection.LEFT: + %SceneManager.makeBullet(left_spawn.global_transform, -700) + if facing == FaceDirection.RIGHT: + %SceneManager.makeBullet(right_spawn.global_transform, 700) + + if Input.is_action_just_pressed("shove") && pushEnabled: + print("Shove attack!") + if facing == FaceDirection.LEFT: + print("facing left") + pushTarget.apply_central_impulse(Vector2(-1,0) * shove_power) + pushEnabled = false + if facing == FaceDirection.RIGHT: + print("facing right") + pushTarget.apply_central_impulse(Vector2(1,0) * shove_power) + pushEnabled = false # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): velocity.y = JUMP_VELOCITY @@ -20,11 +54,39 @@ func _physics_process(delta: float) -> void: var direction := Input.get_axis("ui_left", "ui_right") if direction: velocity.x = direction * SPEED + if direction <0: + facing = FaceDirection.LEFT + if direction > 0: + facing = FaceDirection.RIGHT else: velocity.x = move_toward(velocity.x, 0, SPEED) + +func update_movement()-> void: + pass + +func update_states()->void: + pass - move_and_slide() +func handle_collisions()->void: for i in get_slide_collision_count(): var c = get_slide_collision(i) if c.get_collider() is RigidBody2D: - c.get_collider().apply_central_impulse(-c.get_normal() * pushpower) + c.get_collider().apply_central_impulse(-c.get_normal() * bump_power) + + if right_cast.is_colliding() && facing==FaceDirection.RIGHT: + print("right raycast is hitting something") + var collider = right_cast.get_collider() + if collider is Node && collider is RigidBody2D: + print("I think it is a crate") + pushTarget = collider + pushEnabled = true + + if left_cast.is_colliding() && facing==FaceDirection.LEFT: + print("left raycast is hitting something") + var collider = left_cast.get_collider() + if collider is Node && collider is RigidBody2D: + print("I think it is a crate on the left") + pushTarget = collider + pushEnabled = true + if not right_cast.is_colliding() and not left_cast.is_colliding(): + pushEnabled = false diff --git a/scripts/scene_manager.gd b/scripts/scene_manager.gd new file mode 100644 index 0000000..fba21cc --- /dev/null +++ b/scripts/scene_manager.gd @@ -0,0 +1,40 @@ +extends Node + +@onready var game: GameController = $".." +@onready var crates: Node2D = $"../crates" + +var bullet = preload("res://scenes/bullet.tscn") +var bulletArray=[] +var totalAllowedBullets = 7 + +func buildLevel()->void: + #count crates + var totalCrates = 0 + for obj in crates.get_children(): + if obj is Crate: + totalCrates +=1 + # tell the game controller + game.numberOfCrates(totalCrates) + +func _ready() -> void: + buildLevel() + +func bulletFactory(): + var mybullet + if bulletArray.size() < totalAllowedBullets: + #new bullet + mybullet = bullet.instantiate() + mybullet.connect("bulletHit", game.bulletDamage) + owner.add_child(mybullet) + else: + #recycled bullet + mybullet = bulletArray.pop_back() + + bulletArray.push_front(mybullet) + return mybullet + +func makeBullet(position, speed): + print("Scene manager makes a bullet") + var mybullet = bulletFactory() + mybullet.setSpeed(speed) + mybullet.transform = position diff --git a/scripts/scene_manager.gd.uid b/scripts/scene_manager.gd.uid new file mode 100644 index 0000000..e95786d --- /dev/null +++ b/scripts/scene_manager.gd.uid @@ -0,0 +1 @@ +uid://bus3b1g717jlm