Skip to content

Commit

Permalink
Fix lag when in high Wave
Browse files Browse the repository at this point in the history
+ Add Death Animation to Enemies
+ Hitbox Change for Enemies
  • Loading branch information
EinsKatze committed Jan 21, 2024
1 parent c50373d commit f1b0f67
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 31 deletions.
74 changes: 70 additions & 4 deletions entities/enemy.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=6 format=3 uid="uid://32uqp2kt3dy4"]
[gd_scene load_steps=8 format=3 uid="uid://32uqp2kt3dy4"]

[ext_resource type="Script" path="res://scripts/enemy.gd" id="1_yqwu2"]
[ext_resource type="Texture2D" uid="uid://bivyscdevv7ye" path="res://art/enemy/enemy-01.png" id="2_jkjfl"]
Expand All @@ -10,7 +10,68 @@ shader = ExtResource("2_qcvix")
shader_parameter/flash_color = Color(1, 1, 1, 1)
shader_parameter/progress = 0.0

[sub_resource type="RectangleShape2D" id="RectangleShape2D_kt0x3"]
[sub_resource type="CircleShape2D" id="CircleShape2D_yww2p"]

[sub_resource type="Animation" id="Animation_18ajk"]
resource_name = "Death"
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:scale")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.4),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Vector2(1.75, 1.75), Vector2(0.1, 0.1)]
}
tracks/1/type = "method"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath(".")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0, 0.4),
"transitions": PackedFloat32Array(1, 1),
"values": [{
"args": [false],
"method": &"set_physics_process"
}, {
"args": [],
"method": &"queue_free"
}]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Sprite2D:visible")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0.4),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [false]
}
tracks/3/type = "value"
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/path = NodePath("Sprite2D:rotation")
tracks/3/interp = 1
tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(0, 0.4),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [0.0, 3.14159]
}

[sub_resource type="AnimationLibrary" id="AnimationLibrary_irpdx"]
_data = {
"Death": SubResource("Animation_18ajk")
}

[node name="Enemy" type="PathFollow2D"]
rotates = false
Expand All @@ -26,11 +87,16 @@ texture = ExtResource("2_jkjfl")
[node name="Area2D" type="Area2D" parent="." groups=["enemy"]]

[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
scale = Vector2(2.9641, 2.94208)
shape = SubResource("RectangleShape2D_kt0x3")
scale = Vector2(2.7, 2.7)
shape = SubResource("CircleShape2D_yww2p")

[node name="HitTimer" type="Timer" parent="."]
wait_time = 0.2
one_shot = true

[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_irpdx")
}

[connection signal="area_entered" from="Area2D" to="." method="_on_area_2d_area_entered"]
5 changes: 4 additions & 1 deletion entities/tower/bullet.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ var speed = 500
var look_vec = Vector2.ZERO
var target
var damage = 1
var sprite: Texture2D

func _ready():
if target != null:
$Sprite2D.look_at(target.global_position)
$Sprite2D.texture = sprite
#$Sprite2D.look_at(target.global_position)
$Sprite2D.rotation = global_position.angle_to_point(target.global_position) - 4.5 # Rotate Sprite so the Top of it faces in the direction of the enemy
look_vec = target.global_position - global_position

func _physics_process(delta):
Expand Down
6 changes: 3 additions & 3 deletions entities/tower/bullet.tscn
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
[gd_scene load_steps=4 format=3 uid="uid://khxb3ax8ilx6"]

[ext_resource type="Script" path="res://entities/tower/bullet.gd" id="1_kf5fa"]
[ext_resource type="Texture2D" uid="uid://ccnc8fs76m20c" path="res://icon.svg" id="2_4nxkc"]

[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_0h6pw"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_b816v"]
size = Vector2(25, 25)

[node name="Area2D" type="Area2D" groups=["projectile"]]
scale = Vector2(0.2, 0.2)
script = ExtResource("1_kf5fa")

[node name="Sprite2D" type="Sprite2D" parent="."]
texture_filter = 2
texture = ExtResource("2_4nxkc")
texture = SubResource("PlaceholderTexture2D_0h6pw")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
scale = Vector2(5, 5)
Expand Down
2 changes: 1 addition & 1 deletion entities/tower/speed_tower.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ texture = ExtResource("2_4elmi")
[node name="SightArea" type="Area2D" parent="."]

[node name="CollisionShape2D" type="CollisionShape2D" parent="SightArea"]
scale = Vector2(30, 30)
scale = Vector2(25, 25)
shape = SubResource("CircleShape2D_47qsh")

[node name="ShootTimer" type="Timer" parent="."]
Expand Down
5 changes: 3 additions & 2 deletions entities/tower/tower.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ func _ready():
func _physics_process(_delta):
if enemies != []:
current_enemy = enemies[0]
sprite_reference.rotation = global_position.angle_to_point(current_enemy.global_position) - 3.1 # the "- 3.1 fixes a weird rotation" | uncomment line above if u want funny tux
sprite_reference.rotation = global_position.angle_to_point(current_enemy.global_position) - 3.1 # the "- 3.1 fixes a weird rotation"
if ready_to_fire == true:
ready_to_fire = false
var b = bullet.instantiate()
b.sprite = sprite_reference.texture
shoot_timer.start()
b.global_position = global_position
b.target = current_enemy
get_parent().add_child(b)


func _process(delta):
func _process(_delta):
shoot_progress_bar.value = shoot_timer.time_left
if shoot_progress_bar.value == 0:
shoot_progress_bar.visible = false
Expand Down
2 changes: 1 addition & 1 deletion entities/tower/tower.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ texture = ExtResource("2_ddckq")
[node name="SightArea" type="Area2D" parent="."]

[node name="CollisionShape2D" type="CollisionShape2D" parent="SightArea"]
scale = Vector2(30, 30)
scale = Vector2(35, 35)
shape = SubResource("CircleShape2D_47qsh")

[node name="ShootTimer" type="Timer" parent="."]
Expand Down
34 changes: 19 additions & 15 deletions main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ var health: int = 100
var money: int = 100
var difficulty: int = 5
var wave: int = 0
var enemyList: Array
#var enemyList: Array
var enemyCount: int = 0
var waveDiff: int = 0

var b_in_ui = false
@onready var enemy = preload("res://entities/enemy.tscn")
Expand All @@ -29,6 +31,18 @@ func _ready():

emit_signal("player_money_update", money)

func _physics_process(_delta: float) -> void:
if waveDiff > 0:
var spawner = spawners[randi_range(0, spawners.size()-1)]
var new_enemy = enemy.instantiate()
if waveDiff >= 10:
new_enemy.type = 2
waveDiff -= 5
else:
waveDiff -= 1
spawner.add_child(new_enemy)
enemyCount += 1

func _on_buy_tower(price) -> void:
money -= price
emit_signal("player_money_update", money)
Expand All @@ -39,20 +53,10 @@ func on_player_hover_ui(b_is_in_ui) -> void:
func _on_timer_timeout():
# TODO: Different Enemy Types | ex. Instead of Spawning 5 Enemies, spawn a more difficult one -- kind of done, need to implement more enemies
# TODO: (may be fixed with the todo above): When large amounts of enemies need to be spawned, the spawning lags the game
if enemyList == []:
if enemyCount <= 0 && waveDiff <= 0:
wave += 1
emit_signal("wave_update", wave)
var localDiff = difficulty
while localDiff > 0:
var spawner = spawners[randi_range(0, spawners.size()-1)]
var new_enemy = enemy.instantiate()
if (localDiff >= 10):
new_enemy.type = 2
localDiff -= 5
else:
localDiff -= 1
spawner.add_child(new_enemy)
enemyList.append(new_enemy)
waveDiff = difficulty

difficulty = difficulty + ceil(wave * 1.2)
print("Difficulty: " + str(difficulty))
Expand All @@ -70,8 +74,8 @@ func _on_damage_player(value) -> void:
health -= value
emit_signal("player_health_update")

func onEnemyKilled(enemy) -> void:
enemyList.erase(enemy)
func onEnemyKilled(_enemy) -> void:
enemyCount -= 1
money += 1
emit_signal("player_money_update", money)
# TODO: Dont add money when the enemy gets destroyed due to finishing the path
Expand Down
7 changes: 5 additions & 2 deletions scripts/enemy.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extends PathFollow2D
@onready var sprite_reference = get_node("Sprite2D")
@onready var type2texture = preload("res://art/enemy/enemy-02.png")
@onready var hit_timer = get_node("HitTimer")
@onready var animator = get_node("AnimationPlayer")

var health = 2
var damage = 5
Expand All @@ -17,7 +18,7 @@ func _ready():
health = 10
speed = randf_range(60.0, 120.0)

func _process(delta):
func _process(_delta):
sprite_reference.material.set_shader_parameter("progress", get_hit_timer_progress())

func get_hit_timer_progress():
Expand All @@ -41,4 +42,6 @@ func _on_area_2d_area_entered(area):
area.queue_free() # destroy the projectile
if health == 0:
eventmanager.broadcast_on_enemy_killed(self) # broadcast enemy got killed
queue_free() # "kill" the enemy
animator.play("Death")

#queue_free() # "kill" the enemy
4 changes: 2 additions & 2 deletions scripts/ghost_cursor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func _on_tower_button_pressed(towerButton):
tower_cost = towerButton.get_cost()
offset = ghost_image.size

func _process(delta):
func _process(_delta):
global_position = get_global_mouse_position() - offset
if b_can_place:
modulate = "72c65b"
else:
modulate = "f00000"
visible = is_instance_valid(tower_to_place)

func _physics_process(delta):
func _physics_process(_delta):
var collisions = collision_area.get_overlapping_areas()
if collisions == []:
collisions = collision_area.get_overlapping_bodies()
Expand Down

1 comment on commit f1b0f67

@EinsKatze
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe still not the most efficient way to spawn enemies, but it's way better than before.

Please sign in to comment.