diff --git a/project.godot b/project.godot index 89fdd08..c67d326 100644 --- a/project.godot +++ b/project.godot @@ -20,3 +20,37 @@ config/icon="res://icon.svg" folder_colors={ "res://scenes/": "orange" } + +[input] + +moveLeft={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +] +} +moveRight={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +] +} +jump={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +, 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":85,"key_label":0,"unicode":117,"location":0,"echo":false,"script":null) +] +} +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":88,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null) +] +} +chuck={ +"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":67,"key_label":0,"unicode":99,"location":0,"echo":false,"script":null) +] +} diff --git a/scenes/bullet.tscn b/scenes/bullet.tscn new file mode 100644 index 0000000..f9d1af9 --- /dev/null +++ b/scenes/bullet.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=3 uid="uid://daa6idpu4gkat"] + +[ext_resource type="Script" uid="uid://cbp3pm7i587mv" path="res://scripts/bullet.gd" id="1_mkf8s"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_h1aey"] +size = Vector2(4, 2) + +[node name="Bullet" type="Area2D"] +scale = Vector2(3.2338707, 2.7645516) +script = ExtResource("1_mkf8s") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_h1aey") +debug_color = Color(0.78294253, 0.1467745, 0.21462014, 0.41960785) diff --git a/scenes/game.tscn b/scenes/game.tscn index 2dfaa27..440fd25 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -1,7 +1,9 @@ -[gd_scene load_steps=8 format=3 uid="uid://cy1i6ucex6m0d"] +[gd_scene load_steps=10 format=3 uid="uid://cy1i6ucex6m0d"] [ext_resource type="Script" uid="uid://c546prectbgc7" path="res://scripts/gamecontroller.gd" id="1_lbhrr"] [ext_resource type="PackedScene" uid="uid://qv8blu8wkqvq" path="res://scenes/crate.tscn" id="1_uwrxv"] +[ext_resource type="PackedScene" uid="uid://daa6idpu4gkat" path="res://scenes/bullet.tscn" id="2_iywne"] +[ext_resource type="Script" uid="uid://barmq4qhp5cvl" path="res://scripts/scene_manager.gd" id="2_p57ef"] [ext_resource type="PackedScene" uid="uid://dmrrbiwqptrho" path="res://scenes/trigger.tscn" id="2_yqjtg"] [ext_resource type="PackedScene" uid="uid://b1yy0sybg66hh" path="res://scenes/player.tscn" id="3_lnu2h"] @@ -15,6 +17,10 @@ position = Vector2(6, 2) script = ExtResource("1_lbhrr") +[node name="SceneManager" type="Node2D" parent="."] +unique_name_in_owner = true +script = ExtResource("2_p57ef") + [node name="StaticBody2D" type="StaticBody2D" parent="."] position = Vector2(120, 233) scale = Vector2(3.2000012, 1) @@ -41,10 +47,10 @@ metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D3"] shape = SubResource("RectangleShape2D_8cj0n") -[node name="Crate" parent="." instance=ExtResource("1_uwrxv")] +[node name="Crate" parent="." groups=["explodable", "pushable"] instance=ExtResource("1_uwrxv")] position = Vector2(112, 186) -[node name="Crate2" type="RigidBody2D" parent="."] +[node name="Crate2" type="RigidBody2D" parent="." groups=["explodable", "pushable"]] position = Vector2(147.99998, 202.99994) rotation = 2.6595373 metadata/_edit_group_ = true @@ -75,5 +81,8 @@ offset_right = 40.0 offset_bottom = 23.0 text = "0000" +[node name="Bullet" parent="." instance=ExtResource("2_iywne")] +position = Vector2(45, 205.99998) + [connection signal="triggerFiredSignal" from="Trigger" to="." method="_on_trigger_fired"] [connection signal="triggerFiredSignal" from="Trigger2" to="." method="_on_trigger_fired"] diff --git a/scenes/grenade.tscn b/scenes/grenade.tscn new file mode 100644 index 0000000..601607d --- /dev/null +++ b/scenes/grenade.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=3 format=3 uid="uid://b5n0lwes8qpu5"] + +[ext_resource type="Script" uid="uid://c3sd8uvxer0wk" path="res://scripts/grenade.gd" id="1_cvyik"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_1xt3t"] + +[node name="Grenade" type="RigidBody2D"] +contact_monitor = true +max_contacts_reported = 1 +script = ExtResource("1_cvyik") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_1xt3t") +debug_color = Color(0.7058824, 0.49411765, 0.18431373, 0.5921569) + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/scenes/player.tscn b/scenes/player.tscn index 267c090..e9b5fe2 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -10,3 +10,15 @@ script = ExtResource("1_3vyb7") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource("CircleShape2D_3vyb7") debug_color = Color(0.46210045, 0.5883958, 0.21709043, 0.41960785) + +[node name="RightCast" type="RayCast2D" parent="."] +target_position = Vector2(15, 0) + +[node name="LeftCast" type="RayCast2D" parent="."] +target_position = Vector2(-14, 0) + +[node name="RightSpawn" type="Marker2D" parent="."] +position = Vector2(15, -10) + +[node name="LeftSpawn" type="Marker2D" parent="."] +position = Vector2(-14, -10) diff --git a/scripts/bullet.gd b/scripts/bullet.gd new file mode 100644 index 0000000..0b9a97a --- /dev/null +++ b/scripts/bullet.gd @@ -0,0 +1,15 @@ +class_name Bullet extends Area2D + +var speed:float = 700 + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + position += transform.x * speed * delta + +func setSpeed(value)->void: + speed = value diff --git a/scripts/bullet.gd.uid b/scripts/bullet.gd.uid new file mode 100644 index 0000000..5dc4122 --- /dev/null +++ b/scripts/bullet.gd.uid @@ -0,0 +1 @@ +uid://cbp3pm7i587mv diff --git a/scripts/grenade.gd b/scripts/grenade.gd new file mode 100644 index 0000000..db7fef6 --- /dev/null +++ b/scripts/grenade.gd @@ -0,0 +1,26 @@ +class_name Grenade extends RigidBody2D + +var timer = Timer.new() + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + add_child(timer) + timer.wait_time = 2 + timer.one_shot = true + timer.connect("timeout", explode) + timer.start() + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + pass + +func explode()->void: + print("ima splode") + self.queue_free() + + +func _on_body_entered(body: Node) -> void: + print("Grenade hit something") + if body.is_in_group("explodable"): + body.queue_free() + explode() diff --git a/scripts/grenade.gd.uid b/scripts/grenade.gd.uid new file mode 100644 index 0000000..40c3aff --- /dev/null +++ b/scripts/grenade.gd.uid @@ -0,0 +1 @@ +uid://c3sd8uvxer0wk diff --git a/scripts/player.gd b/scripts/player.gd index 4f9e4de..421a2b9 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -3,33 +3,93 @@ extends CharacterBody2D const SPEED = 300.0 const JUMP_VELOCITY = -400.0 +var direction:float = 0 @export var BUMP_POWER = 100 +enum FaceDirection{LEFT, RIGHT} +var facing:FaceDirection = FaceDirection.RIGHT + +@onready var right_cast: RayCast2D = $RightCast +@onready var left_cast: RayCast2D = $LeftCast + +var pushTarget:RigidBody2D +var pushEnabled:bool = false + +@onready var right_spawn: Marker2D = $RightSpawn +@onready var left_spawn: Marker2D = $LeftSpawn func _physics_process(delta: float) -> void: - # Add the gravity. - if not is_on_floor(): - velocity += get_gravity() * delta - + handle_input() + handle_movement(delta) + move_and_slide() + handle_collision() + +func handle_input()->void: # Handle jump. - if Input.is_action_just_pressed("ui_accept") and is_on_floor(): + if Input.is_action_just_pressed("jump") and is_on_floor(): velocity.y = JUMP_VELOCITY - - # Get the input direction and handle the movement/deceleration. - # As good practice, you should replace UI actions with custom gameplay actions. - var direction := Input.get_axis("ui_left", "ui_right") + + direction = Input.get_axis("moveLeft", "moveRight") + if direction < 0: + facing = FaceDirection.LEFT + if direction > 0: + facing = FaceDirection.RIGHT + + if Input.is_action_just_pressed("shove") && pushEnabled: + print("I want to shove") + var shoveDirection:int + match facing: + FaceDirection.RIGHT: + shoveDirection = 1 + FaceDirection.LEFT: + shoveDirection = -1 + pushTarget.apply_central_impulse(Vector2(shoveDirection, 0) * 700) + + if Input.is_action_just_pressed("shoot"): + print("Ima shoot") + match facing: + FaceDirection.RIGHT: + print("shoot right") + %SceneManager.makeBullet(right_spawn.global_transform, 700) + FaceDirection.LEFT: + print("shoot left") + %SceneManager.makeBullet(left_spawn.global_transform, -700) + + if Input.is_action_just_pressed("chuck"): + print("Ima chuck a grenade") + match facing: + FaceDirection.RIGHT: + %SceneManager.makeGrenade(right_spawn.global_transform, 1) + FaceDirection.LEFT: + %SceneManager.makeGrenade(left_spawn.global_transform, -1) + +func handle_movement(delta)->void: if direction: velocity.x = direction * SPEED else: velocity.x = move_toward(velocity.x, 0, SPEED) - - move_and_slide() + if not is_on_floor(): + velocity += get_gravity() * delta + +func handle_collision()->void: # handle world reactions 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()*BUMP_POWER) + c.get_collider().apply_central_impulse(-c.get_normal()*BUMP_POWER) - + if right_cast.is_colliding() && facing==FaceDirection.RIGHT: + var collider = right_cast.get_collider() + if collider is Node && collider is RigidBody2D && collider.is_in_group("pushable"): + pushTarget = collider + pushEnabled = true + if left_cast.is_colliding() && facing==FaceDirection.LEFT: + var collider = left_cast.get_collider() + if collider is Node && collider is RigidBody2D && collider.is_in_group("pushable"): + pushTarget = collider + pushEnabled = true + if not right_cast.is_colliding() && 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..cfaf9ed --- /dev/null +++ b/scripts/scene_manager.gd @@ -0,0 +1,45 @@ +class_name SceneManager extends Node2D + +var bullet = preload("res://scenes/bullet.tscn") + +@onready var game: Node2D = $".." + +var bulletArray = [] +var totalAllowedBullets = 7 + +var grenade = preload("res://scenes/grenade.tscn") + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + pass + +func bulletFactory()->Bullet: + var myBullet:Bullet + if bulletArray.size() < totalAllowedBullets: + # make a new bullet + myBullet = bullet.instantiate() + game.add_child(myBullet) + else: + myBullet = bulletArray.pop_back() + + bulletArray.push_front(myBullet) + return myBullet + +func makeBullet(_bulletPosition, _bulletSpeed)->void: + print("make a bullet, put it in the world") + var myBullet = bulletFactory() + myBullet.transform = _bulletPosition + myBullet.setSpeed(_bulletSpeed) + +func makeGrenade(_grenadePosition, _grenadeDirection)->void: + print("SM make a grenade") + var myGrenade:Grenade = grenade.instantiate() + game.add_child(myGrenade) + myGrenade.apply_central_impulse(Vector2(_grenadeDirection, -1)*200) + myGrenade.transform = _grenadePosition + diff --git a/scripts/scene_manager.gd.uid b/scripts/scene_manager.gd.uid new file mode 100644 index 0000000..f0189e4 --- /dev/null +++ b/scripts/scene_manager.gd.uid @@ -0,0 +1 @@ +uid://barmq4qhp5cvl