Skip to content

Commit

Permalink
bullets, autoshooting and autoaiming
Browse files Browse the repository at this point in the history
  • Loading branch information
ricargoes committed Feb 13, 2024
1 parent 4603d93 commit c8d3842
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 4 deletions.
4 changes: 2 additions & 2 deletions addons/on_screen_terminal/on_screen_terminal.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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


27 changes: 27 additions & 0 deletions scenes/bullet.gd
Original file line number Diff line number Diff line change
@@ -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()
20 changes: 20 additions & 0 deletions scenes/bullet.tscn
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions scenes/enermy.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion scenes/enermy.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 15 additions & 0 deletions scenes/game.gd
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 7 additions & 1 deletion scenes/game.tscn
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
[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"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_y7e0y"]
size = Vector2(127.778, 128)

[node name="Game" type="Node2D"]
script = ExtResource("1_17qkh")

[node name="Enermies" type="Node2D" parent="."]

Expand All @@ -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"]
28 changes: 28 additions & 0 deletions scenes/hero.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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

13 changes: 13 additions & 0 deletions scenes/hero.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit c8d3842

Please sign in to comment.