diff --git a/addons/on_screen_terminal/on_screen_terminal.gd b/addons/on_screen_terminal/on_screen_terminal.gd index 4300aaf..2e0c1d8 100644 --- a/addons/on_screen_terminal/on_screen_terminal.gd +++ b/addons/on_screen_terminal/on_screen_terminal.gd @@ -9,9 +9,9 @@ func _ready() -> void: if not enabled: hide() -func log(message: String) -> void: +func log(message: Variant) -> void: if enabled: - $Terminal.text += "\n> " + message + $Terminal.text += "\n> " + str(message) $Terminal.get_v_scroll_bar().value = $Terminal.get_v_scroll_bar().max_value diff --git a/scenes/bullet.gd b/scenes/bullet.gd new file mode 100644 index 0000000..02b86ba --- /dev/null +++ b/scenes/bullet.gd @@ -0,0 +1,27 @@ +extends CharacterBody2D + +@export +var is_player: bool = false +@export +var orientation: float = 0.0 +@export +var bullet_speed: float = 1000 +@export +var damage: float = 10.0 + +func _ready() -> void: + collision_mask = 4 + if is_player: + collision_mask += 2 + else: + collision_mask += 1 + + velocity = Vector2.from_angle(orientation)*bullet_speed + +func _process(delta: float) -> void: + var collision = move_and_collide(velocity*delta) + if collision: + if collision.get_collider().has_method("hurt"): + collision.get_collider().hurt(damage) + + queue_free() diff --git a/scenes/bullet.tscn b/scenes/bullet.tscn new file mode 100644 index 0000000..1bbcfed --- /dev/null +++ b/scenes/bullet.tscn @@ -0,0 +1,20 @@ +[gd_scene load_steps=4 format=3 uid="uid://cv036x8hk0xcn"] + +[ext_resource type="Texture2D" uid="uid://bd5pw0yjs0ft3" path="res://resources/ui/empty_v_bar.png" id="1_aqltn"] +[ext_resource type="Script" path="res://scenes/bullet.gd" id="1_r0642"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_ysppi"] +size = Vector2(20, 14) + +[node name="Bullet" type="CharacterBody2D"] +collision_layer = 0 +collision_mask = 0 +script = ExtResource("1_r0642") + +[node name="Sprite2D" type="Sprite2D" parent="."] +rotation = 1.5708 +scale = Vector2(1, 0.2) +texture = ExtResource("1_aqltn") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_ysppi") diff --git a/scenes/enermy.gd b/scenes/enermy.gd index f58011b..c6014d2 100644 --- a/scenes/enermy.gd +++ b/scenes/enermy.gd @@ -28,6 +28,7 @@ func _process(delta: float) -> void: func hurt(damage: float): life_points -= damage + OnScreenTerminal.log(life_points) if life_points <= 0: die() diff --git a/scenes/enermy.tscn b/scenes/enermy.tscn index ac76301..d466718 100644 --- a/scenes/enermy.tscn +++ b/scenes/enermy.tscn @@ -9,7 +9,7 @@ size = Vector2(128, 126) [sub_resource type="CircleShape2D" id="CircleShape2D_3yv4y"] radius = 55.0364 -[node name="Enermy" type="CharacterBody2D"] +[node name="Enermy" type="CharacterBody2D" groups=["enemies"]] collision_layer = 2 collision_mask = 4 script = ExtResource("1_i2q4i") diff --git a/scenes/game.gd b/scenes/game.gd new file mode 100644 index 0000000..584c664 --- /dev/null +++ b/scenes/game.gd @@ -0,0 +1,15 @@ +extends Node2D + +const BULLET_SCENE = preload("res://scenes/bullet.tscn") + + +func spawn_bullet(starting_position: Vector2, orientation: float, is_player: bool): + var bullet = BULLET_SCENE.instantiate() + bullet.global_position = starting_position + bullet.orientation = orientation + bullet.is_player = is_player + $Bullets.add_child(bullet) + + +func _on_hero_shot(spawn_position: Vector2, orientation: float) -> void: + spawn_bullet(spawn_position, orientation, true) diff --git a/scenes/game.tscn b/scenes/game.tscn index 70f294b..fbcd286 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=5 format=3 uid="uid://dwl37y77358r2"] +[gd_scene load_steps=6 format=3 uid="uid://dwl37y77358r2"] [ext_resource type="PackedScene" uid="uid://f31q7ameb6hm" path="res://scenes/hero.tscn" id="1_2n3he"] +[ext_resource type="Script" path="res://scenes/game.gd" id="1_17qkh"] [ext_resource type="Texture2D" uid="uid://5rou5wm7frds" path="res://resources/icon.svg" id="1_hu7ex"] [ext_resource type="PackedScene" uid="uid://csfarad5ilhw6" path="res://scenes/enermy.tscn" id="3_i8n50"] @@ -8,6 +9,7 @@ size = Vector2(127.778, 128) [node name="Game" type="Node2D"] +script = ExtResource("1_17qkh") [node name="Enermies" type="Node2D" parent="."] @@ -29,3 +31,7 @@ texture = ExtResource("1_hu7ex") [node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"] shape = SubResource("RectangleShape2D_y7e0y") + +[node name="Bullets" type="Node2D" parent="."] + +[connection signal="shot" from="Hero" to="." method="_on_hero_shot"] diff --git a/scenes/hero.gd b/scenes/hero.gd index 6989777..15caab2 100644 --- a/scenes/hero.gd +++ b/scenes/hero.gd @@ -7,9 +7,13 @@ const MAX_JUMP_IMPULSE_TIME: float = 0.3 var player_speed: int = 200 @export var life_points: float = 100 +@export +var arm_rotation_speed: float = 2*PI var jump_impulse_time: float = 0.0 +signal shot(spawn_position: Vector2, orientation: float) + func _ready() -> void: velocity.x = player_speed $UI.sync_life(life_points) @@ -29,6 +33,12 @@ func _process(delta: float) -> void: if global_position.y > GameConstants.DEATH_GLOBAL_Y_POSITION: hurt(1000) + + var lock_on_target = select_target() + aim_to(lock_on_target.global_position, delta) + + if Input.is_action_just_pressed("ui_accept"): # To be removed + shoot() func hurt(damage: float): life_points -= damage @@ -38,3 +48,21 @@ func hurt(damage: float): func die(): get_tree().quit() + +func shoot(): + var orientation = $ShoulderPivot.rotation + shot.emit($ShoulderPivot.global_position + Vector2.from_angle(orientation)*120, orientation) + +func select_target() -> Node2D: + var target = self + var current_min_distance = 10000 + for enemy: Node2D in get_tree().get_nodes_in_group("enemies"): + if (enemy.global_position - global_position).length() < current_min_distance: + target = enemy + return target + +func aim_to(location: Vector2, delta: float): + var target_orientation = $ShoulderPivot.global_position.angle_to_point(location) + var rotation_strength = fposmod(target_orientation-$ShoulderPivot.rotation, 2*PI)/PI - 1 + $ShoulderPivot.rotation -= rotation_strength*arm_rotation_speed*delta + diff --git a/scenes/hero.tscn b/scenes/hero.tscn index b214adc..9edbb26 100644 --- a/scenes/hero.tscn +++ b/scenes/hero.tscn @@ -45,3 +45,16 @@ stretch_margin_bottom = 3 texture_under = ExtResource("3_kjtew") texture_over = ExtResource("4_r372t") texture_progress = ExtResource("4_5m4m8") + +[node name="ShoulderPivot" type="Node2D" parent="."] + +[node name="Sprite2D" type="Sprite2D" parent="ShoulderPivot"] +rotation = -1.5708 +texture = ExtResource("4_5m4m8") +offset = Vector2(0, 50) + +[node name="ShootingCooldown" type="Timer" parent="."] +wait_time = 0.3 +autostart = true + +[connection signal="timeout" from="ShootingCooldown" to="." method="shoot"]