diff --git a/project.godot b/project.godot index 6e7fddf..e33700a 100644 --- a/project.godot +++ b/project.godot @@ -14,3 +14,16 @@ config/name="JulyGame" run/main_scene="uid://bjk7nrcwo0ikb" config/features=PackedStringArray("4.4", "Forward Plus") config/icon="res://icon.svg" + +[input] + +ForcePush={ +"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":70,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +] +} +Shoot={ +"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":82,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +] +} diff --git a/scenes/bullet.tscn b/scenes/bullet.tscn new file mode 100644 index 0000000..7015a87 --- /dev/null +++ b/scenes/bullet.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=3 format=3 uid="uid://bgwa5osciu6kj"] + +[ext_resource type="Script" uid="uid://bemhplfq3bapd" path="res://scripts/bullet.gd" id="1_mkf8s"] + +[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_h1aey"] +radius = 7.0 +height = 32.0 + +[node name="bullet" type="Area2D"] +script = ExtResource("1_mkf8s") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +rotation = 1.55509 +shape = SubResource("CapsuleShape2D_h1aey") +debug_color = Color(0.849979, 0, 0.312876, 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..b189c95 --- /dev/null +++ b/scenes/crate.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=3 format=3 uid="uid://cm72yvkbgso3"] + +[ext_resource type="Script" uid="uid://caikxm70kr5wu" path="res://scripts/crate.gd" id="1_b66cd"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_uwrxv"] +size = Vector2(82, 52) + +[node name="crate" type="RigidBody2D" groups=["pushable"]] +rotation = 0.00469979 +script = ExtResource("1_b66cd") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2(4, 0) +rotation = 0.50364 +shape = SubResource("RectangleShape2D_uwrxv") +debug_color = Color(0.920063, 0.281624, 0, 0.42) diff --git a/scenes/game.tscn b/scenes/game.tscn index a606a9e..c5c425d 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -1,22 +1,24 @@ [gd_scene load_steps=8 format=3 uid="uid://bjk7nrcwo0ikb"] [ext_resource type="Script" uid="uid://bc6rpr127gn6l" path="res://scripts/gamecontroller.gd" id="1_lnu2h"] -[ext_resource type="Script" uid="uid://r3vurapv33hu" path="res://scripts/player.gd" id="1_uwrxv"] +[ext_resource type="PackedScene" uid="uid://cm72yvkbgso3" path="res://scenes/crate.tscn" id="2_lbhrr"] +[ext_resource type="PackedScene" uid="uid://bbg4idgjqm8wr" path="res://scenes/player.tscn" id="2_lnu2h"] +[ext_resource type="Script" uid="uid://slrv7nc7hjkl" path="res://scripts/scene_manager.gd" id="2_p57ef"] [ext_resource type="PackedScene" uid="uid://d1gmq2hler2x3" path="res://scenes/trigger.tscn" id="2_yqjtg"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"] size = Vector2(66, 20) -[sub_resource type="RectangleShape2D" id="RectangleShape2D_uwrxv"] -size = Vector2(82, 52) - [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_8cj0n"] - -[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"] +distance = 15.0 [node name="Game" type="Node2D"] script = ExtResource("1_lnu2h") +[node name="SceneManager" type="Node2D" parent="."] +unique_name_in_owner = true +script = ExtResource("2_p57ef") + [node name="StaticBody2D" type="StaticBody2D" parent="."] position = Vector2(205, 132) metadata/_edit_group_ = true @@ -24,53 +26,18 @@ metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"] shape = SubResource("RectangleShape2D_8cj0n") -[node name="RigidBody2D" type="RigidBody2D" parent="."] -position = Vector2(156, 49) -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D"] -position = Vector2(4, 0) -rotation = 0.50364 -shape = SubResource("RectangleShape2D_uwrxv") -debug_color = Color(0.920063, 0.281624, 0, 0.42) - -[node name="RigidBody2D2" type="RigidBody2D" parent="."] -position = Vector2(207, -17) -rotation = 0.00469979 -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D2"] -position = Vector2(4, 0) -rotation = 0.50364 -shape = SubResource("RectangleShape2D_uwrxv") -debug_color = Color(0.920063, 0.281624, 0, 0.42) - -[node name="RigidBody2D3" type="RigidBody2D" parent="."] +[node name="crate" parent="." instance=ExtResource("2_lbhrr")] position = Vector2(272, -97) -rotation = 0.00469979 -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D3"] -position = Vector2(4, 0) -rotation = 0.50364 -shape = SubResource("RectangleShape2D_uwrxv") -debug_color = Color(0.920063, 0.281624, 0, 0.42) [node name="world-boundary" type="StaticBody2D" parent="."] -position = Vector2(490, 632) +position = Vector2(488, 645) metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="world-boundary"] shape = SubResource("WorldBoundaryShape2D_8cj0n") -[node name="CharacterBody2D" type="CharacterBody2D" parent="."] +[node name="Player" parent="." instance=ExtResource("2_lnu2h")] position = Vector2(210, 92) -script = ExtResource("1_uwrxv") -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] -shape = SubResource("CircleShape2D_8cj0n") -debug_color = Color(0, 0.64833, 0.184337, 0.42) [node name="Area2D" parent="." instance=ExtResource("2_yqjtg")] position = Vector2(1106, 604) diff --git a/scenes/player.tscn b/scenes/player.tscn new file mode 100644 index 0000000..9c729a0 --- /dev/null +++ b/scenes/player.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=3 format=3 uid="uid://bbg4idgjqm8wr"] + +[ext_resource type="Script" uid="uid://r3vurapv33hu" path="res://scripts/player.gd" id="1_3vyb7"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"] + +[node name="Player" type="CharacterBody2D"] +script = ExtResource("1_3vyb7") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_8cj0n") +debug_color = Color(0, 0.64833, 0.184337, 0.42) + +[node name="RightCast" type="RayCast2D" parent="."] +target_position = Vector2(27, 0) + +[node name="LeftCast" type="RayCast2D" parent="."] +target_position = Vector2(-28, 0) + +[node name="BulletSpawnPointRight" type="Node2D" parent="."] +position = Vector2(13, -6) + +[node name="BulletSpawnPointLeft" type="Node2D" parent="."] +position = Vector2(-14, -6) diff --git a/scripts/bullet.gd b/scripts/bullet.gd new file mode 100644 index 0000000..0aa6bda --- /dev/null +++ b/scripts/bullet.gd @@ -0,0 +1,16 @@ +class_name Bullet extends Area2D + +var speed: float = 700 +signal bulletDamageSignal(target: Node2D, bullet: Node2D) + +func setSpeed(val: float): + speed = val + +func _physics_process(delta: float) -> void: + position += transform.x * speed * delta + +func _on_body_entered(body: Node2D) -> void: + if body is Player: + return + if body is Crate && visible: + bulletDamageSignal.emit(body, self) diff --git a/scripts/bullet.gd.uid b/scripts/bullet.gd.uid new file mode 100644 index 0000000..711a16c --- /dev/null +++ b/scripts/bullet.gd.uid @@ -0,0 +1 @@ +uid://bemhplfq3bapd 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..8fd5a0f --- /dev/null +++ b/scripts/crate.gd.uid @@ -0,0 +1 @@ +uid://caikxm70kr5wu diff --git a/scripts/gamecontroller.gd b/scripts/gamecontroller.gd index 3d312d2..563212a 100644 --- a/scripts/gamecontroller.gd +++ b/scripts/gamecontroller.gd @@ -11,7 +11,15 @@ func _process(delta: float) -> void: pass -func _on_area_2d_area_trigger_signal(effect: Variant, body: Variant) -> void: - print("GC sees trigger " + effect) - if not body is Player: - body.queue_free() +func _on_area_2d_area_trigger_signal(effect: String, body: Node2D) -> void: + if body.name == "world-boundary": + return + if body is Player: + return + print("GC sees trigger " + effect + " on " + body.name) + body.queue_free() + +func bulletDamage(target: Node2D, bullet: Node2D): + print("game controller sees hit") + target.queue_free() + bullet.visible = false diff --git a/scripts/player.gd b/scripts/player.gd index ff7ce80..7cf0575 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -1,30 +1,91 @@ -class name Player extends CharacterBody2D +class_name Player extends CharacterBody2D +const bullet_scene = preload("res://scenes/bullet.tscn") + +@onready var right_cast: RayCast2D = $RightCast +@onready var left_cast: RayCast2D = $LeftCast +@onready var bullet_spawn_point_right: Node2D = $BulletSpawnPointRight +@onready var bullet_spawn_point_left: Node2D = $BulletSpawnPointLeft const SPEED = 300.0 const JUMP_VELOCITY = -400.0 +const PUSH_POWER= 2000 +var direction: float +enum FaceDirection{LEFT, RIGHT} +var facing: FaceDirection = FaceDirection.RIGHT +var pushTarget: Object +var pushEnabled: bool = false func _physics_process(delta: float) -> void: + handle_input(delta) + handle_movement(delta) + handle_collisions(delta) + +func handle_gravity(delta: float) -> void: # Add the gravity. if not is_on_floor(): velocity += get_gravity() * delta - + +func handle_input(delta: float) -> void: + var dir: int = 0 + match facing: + FaceDirection.LEFT: + dir = -1 + FaceDirection.RIGHT: + dir = 1 + # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): velocity.y = JUMP_VELOCITY + direction = Input.get_axis("ui_left", "ui_right") + if direction < 0: + facing = FaceDirection.LEFT + elif direction > 0: + facing = FaceDirection.RIGHT + + if Input.is_action_just_pressed("ForcePush") && pushEnabled: + (pushTarget as RigidBody2D).apply_central_impulse(Vector2(dir, 0) * PUSH_POWER) + + if Input.is_action_just_pressed("Shoot"): + var spawnAt: Node2D = bullet_spawn_point_left + if dir == 1: + spawnAt = bullet_spawn_point_right + #var b = bullet_scene.instantiate() + #b.position = spawnAt.position + print("make a bullet") + var b = %SceneManager.makeBullet(spawnAt.global_transform, dir) + #b.position = Player. + #b.Position = Player.Position + + +func handle_movement(delta: float) -> void: + handle_gravity(delta) # 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") if direction: velocity.x = direction * SPEED else: velocity.x = move_toward(velocity.x, 0, SPEED) - move_and_slide() - + +func handle_collisions(delta: float) -> void: for i in get_slide_collision_count(): var coll = get_slide_collision(i) if coll.get_collider() is RigidBody2D: coll.get_collider().apply_central_impulse(-coll.get_normal() * 100) + + if right_cast.is_colliding() && facing == FaceDirection.RIGHT: + var col = right_cast.get_collider() + if col != null && col.is_in_group("pushable"): + pushTarget = col + pushEnabled = true + elif left_cast.is_colliding() && facing == FaceDirection.LEFT: + var col = left_cast.get_collider() + if col != null && col.is_in_group("pushable"): + pushTarget = col + pushEnabled = true + else: + pushEnabled = false + pushTarget = null diff --git a/scripts/scene_manager.gd b/scripts/scene_manager.gd new file mode 100644 index 0000000..be3d422 --- /dev/null +++ b/scripts/scene_manager.gd @@ -0,0 +1,32 @@ +class_name SceneManager extends Node2D + +const BulletScn = preload("res://scenes/bullet.tscn") +@onready var game: Node2D = $".." + +var bullets: Array[Bullet] = [] +const MAX_BULLETS = 10 +var current_bullet = 0 + +# makes and recycles bullets +func bulletFactory() -> Bullet: + if len(bullets) < MAX_BULLETS: + # create new + var b = BulletScn.instantiate() + b.bulletDamageSignal.connect(game.bulletDamage) + add_child(b) + bullets.append(b) + return b + else: + # recycle + var i = current_bullet + current_bullet = (current_bullet + 1) % MAX_BULLETS + bullets[i].visible = true + return bullets[i] + + +func makeBullet(pos: Transform2D, dir: int) -> Node2D: + var b = bulletFactory() + #b.position = pos.get_origin() + b.transform = pos + b.setSpeed(dir * 700) + return b diff --git a/scripts/scene_manager.gd.uid b/scripts/scene_manager.gd.uid new file mode 100644 index 0000000..418584f --- /dev/null +++ b/scripts/scene_manager.gd.uid @@ -0,0 +1 @@ +uid://slrv7nc7hjkl diff --git a/scripts/trigger.gd b/scripts/trigger.gd index eb07e1a..912ec46 100644 --- a/scripts/trigger.gd +++ b/scripts/trigger.gd @@ -6,5 +6,4 @@ extends Area2D signal areaTriggerSignal(effect, Object) func _on_body_entered(body: Node2D) -> void: - print("trigger sees a body") areaTriggerSignal.emit(effect, body)