Implement Player Taking Damage

This commit is contained in:
Snoweuph 2023-04-23 15:21:22 +02:00
parent 66ae4054c6
commit 2a9b0bbef0
9 changed files with 42 additions and 24 deletions

View file

@ -41,7 +41,7 @@
[ext_resource type="StyleBox" uid="uid://0jfr1uwuog0s" path="res://Assets/UI/Slider/v/background.tres" id="39_dl1e4"] [ext_resource type="StyleBox" uid="uid://0jfr1uwuog0s" path="res://Assets/UI/Slider/v/background.tres" id="39_dl1e4"]
[ext_resource type="FontFile" uid="uid://dqdeftjkwxe64" path="res://Assets/Fonts/Dogica/dogicapixel.ttf" id="40_bmcvq"] [ext_resource type="FontFile" uid="uid://dqdeftjkwxe64" path="res://Assets/Fonts/Dogica/dogicapixel.ttf" id="40_bmcvq"]
[sub_resource type="Image" id="Image_iuu86"] [sub_resource type="Image" id="Image_dx2gc"]
data = { data = {
"data": PackedByteArray(255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0), "data": PackedByteArray(255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0),
"format": "RGBA8", "format": "RGBA8",
@ -51,7 +51,7 @@ data = {
} }
[sub_resource type="ImageTexture" id="ImageTexture_g5bup"] [sub_resource type="ImageTexture" id="ImageTexture_g5bup"]
image = SubResource("Image_iuu86") image = SubResource("Image_dx2gc")
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_sj7h5"] [sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_sj7h5"]

View file

@ -1,12 +1,12 @@
[gd_scene load_steps=17 format=3 uid="uid://c4btepmue6d7p"] [gd_scene load_steps=17 format=3 uid="uid://cg4xag5jx65kg"]
[ext_resource type="TileSet" uid="uid://bj7uu2180mie3" path="res://Assets/Tileset.tres" id="1_38041"] [ext_resource type="TileSet" uid="uid://bj7uu2180mie3" path="res://Assets/Tileset.tres" id="1_38041"]
[ext_resource type="Script" path="res://Scripts/GameManager.gd" id="1_t708k"] [ext_resource type="Script" path="res://Scripts/GameManager.gd" id="1_t708k"]
[ext_resource type="Script" path="res://Scripts/MapGenerator.gd" id="2_ope8r"] [ext_resource type="Script" path="res://Scripts/MapGenerator.gd" id="2_ope8r"]
[ext_resource type="Script" path="res://Scripts/EntitySystem/BunnyGenerator.gd" id="3_o045y"] [ext_resource type="Script" path="res://Scripts/EntitySystem/BunnyGenerator.gd" id="3_o045y"]
[ext_resource type="PackedScene" uid="uid://cpl4tllohhyel" path="res://Prefabs/bunny.tscn" id="4_hpafd"] [ext_resource type="PackedScene" uid="uid://cpl4tllohhyel" path="res://Prefabs/bunny.tscn" id="4_hpafd"]
[ext_resource type="Script" path="res://Scripts/PlayerController.gd" id="5_d2cbj"]
[ext_resource type="Texture2D" uid="uid://ojlfqxj5bilo" path="res://Assets/Sprites/Jesus/jesus_spritesheet.png" id="6_p526i"] [ext_resource type="Texture2D" uid="uid://ojlfqxj5bilo" path="res://Assets/Sprites/Jesus/jesus_spritesheet.png" id="6_p526i"]
[ext_resource type="Script" path="res://Scripts/Player.gd" id="6_q3xml"]
[ext_resource type="Script" path="res://Scripts/WeaponSystem/WeaponController.gd" id="7_mtrts"] [ext_resource type="Script" path="res://Scripts/WeaponSystem/WeaponController.gd" id="7_mtrts"]
[ext_resource type="PackedScene" uid="uid://csxh42o8twxwn" path="res://Prefabs/projectile.tscn" id="8_fp1vx"] [ext_resource type="PackedScene" uid="uid://csxh42o8twxwn" path="res://Prefabs/projectile.tscn" id="8_fp1vx"]
@ -119,10 +119,11 @@ _data = {
"RESET": SubResource("Animation_o3hln") "RESET": SubResource("Animation_o3hln")
} }
[node name="GameManager" type="Node2D" node_paths=PackedStringArray("map_generator", "bunny_generator")] [node name="GameManager" type="Node2D" node_paths=PackedStringArray("map_generator", "bunny_generator", "player")]
script = ExtResource("1_t708k") script = ExtResource("1_t708k")
map_generator = NodePath("Map Generator") map_generator = NodePath("Map Generator")
bunny_generator = NodePath("Bunny Generator") bunny_generator = NodePath("Bunny Generator")
player = NodePath("Player")
[node name="Map Generator" type="TileMap" parent="."] [node name="Map Generator" type="TileMap" parent="."]
scale = Vector2(1.5, 1.5) scale = Vector2(1.5, 1.5)
@ -141,7 +142,7 @@ player = NodePath("../Player")
[node name="Player" type="CharacterBody2D" parent="." node_paths=PackedStringArray("animation_player")] [node name="Player" type="CharacterBody2D" parent="." node_paths=PackedStringArray("animation_player")]
position = Vector2(1440, 810) position = Vector2(1440, 810)
collision_layer = 0 collision_layer = 0
script = ExtResource("5_d2cbj") script = ExtResource("6_q3xml")
animation_player = NodePath("Player Animator") animation_player = NodePath("Player Animator")
[node name="Player Sprite" type="Sprite2D" parent="Player"] [node name="Player Sprite" type="Sprite2D" parent="Player"]

View file

@ -1,11 +1,11 @@
[gd_scene load_steps=17 format=3 uid="uid://dkcl1ycmxpilw"] [gd_scene load_steps=17 format=3]
[ext_resource type="Script" path="res://Scripts/GameManager.gd" id="1_1tmfd"] [ext_resource type="Script" path="res://Scripts/GameManager.gd" id="1_1tmfd"]
[ext_resource type="TileSet" uid="uid://bj7uu2180mie3" path="res://Assets/Tileset.tres" id="2_bqk0y"] [ext_resource type="TileSet" uid="uid://bj7uu2180mie3" path="res://Assets/Tileset.tres" id="2_bqk0y"]
[ext_resource type="Script" path="res://Scripts/MapGenerator.gd" id="3_n0wjg"] [ext_resource type="Script" path="res://Scripts/MapGenerator.gd" id="3_n0wjg"]
[ext_resource type="Script" path="res://Scripts/EntitySystem/BunnyGenerator.gd" id="4_0by8r"] [ext_resource type="Script" path="res://Scripts/EntitySystem/BunnyGenerator.gd" id="4_0by8r"]
[ext_resource type="PackedScene" uid="uid://cpl4tllohhyel" path="res://Prefabs/bunny.tscn" id="5_ghq1m"] [ext_resource type="PackedScene" uid="uid://cpl4tllohhyel" path="res://Prefabs/bunny.tscn" id="5_ghq1m"]
[ext_resource type="Script" path="res://Scripts/PlayerController.gd" id="6_o5qq4"] [ext_resource type="Script" path="res://Scripts/Player.gd" id="6_o5qq4"]
[ext_resource type="Texture2D" uid="uid://ojlfqxj5bilo" path="res://Assets/Sprites/Jesus/jesus_spritesheet.png" id="7_we2bi"] [ext_resource type="Texture2D" uid="uid://ojlfqxj5bilo" path="res://Assets/Sprites/Jesus/jesus_spritesheet.png" id="7_we2bi"]
[ext_resource type="Script" path="res://Scripts/WeaponSystem/WeaponController.gd" id="8_p0li1"] [ext_resource type="Script" path="res://Scripts/WeaponSystem/WeaponController.gd" id="8_p0li1"]
[ext_resource type="PackedScene" uid="uid://csxh42o8twxwn" path="res://Prefabs/projectile.tscn" id="9_k5fai"] [ext_resource type="PackedScene" uid="uid://csxh42o8twxwn" path="res://Prefabs/projectile.tscn" id="9_k5fai"]

View file

@ -1,11 +1,11 @@
[gd_scene load_steps=19 format=3 uid="uid://beio3828jo4ht"] [gd_scene load_steps=19 format=3]
[ext_resource type="Script" path="res://Scripts/GameManager.gd" id="1_nh5gk"] [ext_resource type="Script" path="res://Scripts/GameManager.gd" id="1_nh5gk"]
[ext_resource type="TileSet" uid="uid://bj7uu2180mie3" path="res://Assets/Tileset.tres" id="2_ws3yx"] [ext_resource type="TileSet" uid="uid://bj7uu2180mie3" path="res://Assets/Tileset.tres" id="2_ws3yx"]
[ext_resource type="Script" path="res://Scripts/MapGenerator.gd" id="3_335m3"] [ext_resource type="Script" path="res://Scripts/MapGenerator.gd" id="3_335m3"]
[ext_resource type="Script" path="res://Scripts/EntitySystem/BunnyGenerator.gd" id="4_38mjq"] [ext_resource type="Script" path="res://Scripts/EntitySystem/BunnyGenerator.gd" id="4_38mjq"]
[ext_resource type="PackedScene" uid="uid://cpl4tllohhyel" path="res://Prefabs/bunny.tscn" id="5_24kvo"] [ext_resource type="PackedScene" uid="uid://cpl4tllohhyel" path="res://Prefabs/bunny.tscn" id="5_24kvo"]
[ext_resource type="Script" path="res://Scripts/PlayerController.gd" id="6_1tkxe"] [ext_resource type="Script" path="res://Scripts/Player.gd" id="6_1tkxe"]
[ext_resource type="Texture2D" uid="uid://ojlfqxj5bilo" path="res://Assets/Sprites/Jesus/jesus_spritesheet.png" id="7_6m0we"] [ext_resource type="Texture2D" uid="uid://ojlfqxj5bilo" path="res://Assets/Sprites/Jesus/jesus_spritesheet.png" id="7_6m0we"]
[ext_resource type="Script" path="res://Scripts/WeaponSystem/WeaponController.gd" id="8_ohifd"] [ext_resource type="Script" path="res://Scripts/WeaponSystem/WeaponController.gd" id="8_ohifd"]
[ext_resource type="PackedScene" uid="uid://csxh42o8twxwn" path="res://Prefabs/projectile.tscn" id="9_unx3c"] [ext_resource type="PackedScene" uid="uid://csxh42o8twxwn" path="res://Prefabs/projectile.tscn" id="9_unx3c"]

View file

@ -1,10 +1,10 @@
[gd_scene load_steps=16 format=3 uid="uid://cugui0mjc6cfe"] [gd_scene load_steps=16 format=3]
[ext_resource type="TileSet" uid="uid://bj7uu2180mie3" path="res://Assets/Tileset.tres" id="1_s4utw"] [ext_resource type="TileSet" uid="uid://bj7uu2180mie3" path="res://Assets/Tileset.tres" id="1_s4utw"]
[ext_resource type="Script" path="res://Scripts/MapGenerator.gd" id="2_yhpwh"] [ext_resource type="Script" path="res://Scripts/MapGenerator.gd" id="2_yhpwh"]
[ext_resource type="Script" path="res://Scripts/EntitySystem/BunnyGenerator.gd" id="3_dvklj"] [ext_resource type="Script" path="res://Scripts/EntitySystem/BunnyGenerator.gd" id="3_dvklj"]
[ext_resource type="PackedScene" uid="uid://cpl4tllohhyel" path="res://Prefabs/bunny.tscn" id="4_0fyd5"] [ext_resource type="PackedScene" uid="uid://cpl4tllohhyel" path="res://Prefabs/bunny.tscn" id="4_0fyd5"]
[ext_resource type="Script" path="res://Scripts/PlayerController.gd" id="5_uv7bh"] [ext_resource type="Script" path="res://Scripts/Player.gd" id="5_uv7bh"]
[ext_resource type="Texture2D" uid="uid://ojlfqxj5bilo" path="res://Assets/Sprites/Jesus/jesus_spritesheet.png" id="6_es2l8"] [ext_resource type="Texture2D" uid="uid://ojlfqxj5bilo" path="res://Assets/Sprites/Jesus/jesus_spritesheet.png" id="6_es2l8"]
[ext_resource type="Script" path="res://Scripts/WeaponSystem/WeaponController.gd" id="7_8kdie"] [ext_resource type="Script" path="res://Scripts/WeaponSystem/WeaponController.gd" id="7_8kdie"]
[ext_resource type="PackedScene" uid="uid://csxh42o8twxwn" path="res://Prefabs/projectile.tscn" id="8_wx0kk"] [ext_resource type="PackedScene" uid="uid://csxh42o8twxwn" path="res://Prefabs/projectile.tscn" id="8_wx0kk"]

View file

@ -14,7 +14,7 @@ var attack_speed : float
var time_since_last_attack : float var time_since_last_attack : float
var team : int var team : int
var player : Node2D var target : Node2D
var on_death_callbacks : Array var on_death_callbacks : Array
var move_velocity : Vector2 var move_velocity : Vector2
@ -40,22 +40,24 @@ func _ready():
pass pass
func _physics_process(delta : float): func _physics_process(delta : float):
if target == null: return
randomize() randomize()
if abs(player.velocity.length() - last_player_velocity) > 100.0: if abs(target.velocity.length() - last_player_velocity) > 100.0:
time_since_got_player = max_pos_check_time time_since_got_player = max_pos_check_time
if time_since_got_player >= max_pos_check_time and player.velocity.length() > 0.1: if time_since_got_player >= max_pos_check_time and target.velocity.length() > 0.1:
time_since_got_player = randf() * max_pos_check_time * 0.5 time_since_got_player = randf() * max_pos_check_time * 0.5
update_target_pos() update_target_pos()
if time_since_got_player >= max_pos_check_time * max_pos_check_time_inactive_scalar and player.velocity.length() < 0.1: if time_since_got_player >= max_pos_check_time * max_pos_check_time_inactive_scalar and target.velocity.length() < 0.1:
time_since_got_player = -(randf() * max_pos_check_time * 0.5) time_since_got_player = -(randf() * max_pos_check_time * 0.5)
update_target_pos() update_target_pos()
if self.global_position.distance_to(player.global_position) < max_distance_to_player and time_since_got_move_location > max_get_move_location_time: if self.global_position.distance_to(target.global_position) < max_distance_to_player and time_since_got_move_location > max_get_move_location_time:
time_since_got_move_location = 0 time_since_got_move_location = 0
if agent.is_target_reachable(): if agent.is_target_reachable():
next_location = agent.get_next_path_position() next_location = agent.get_next_path_position()
randomized_next_location = next_location + Vector2(randf() - 0.5, randf() - 0.5 ) * 2 * random_move_location_spread_factor randomized_next_location = next_location + Vector2(randf() - 0.5, randf() - 0.5 ) * 2 * random_move_location_spread_factor
dir = self.global_position.direction_to(randomized_next_location).normalized().rotated((randf() - 0.5) * 2 * random_angle_change) dir = self.global_position.direction_to(randomized_next_location).normalized().rotated((randf() - 0.5) * 2 * random_angle_change)
if self.global_position.distance_to(player.global_position) >= min_distance_to_player: if self.global_position.distance_to(target.global_position) >= min_distance_to_player:
move_velocity = dir * delta * 60 * speed move_velocity = dir * delta * 60 * speed
update_animation() update_animation()
if self.global_position.distance_to(next_location) < move_velocity.length(): if self.global_position.distance_to(next_location) < move_velocity.length():
@ -70,18 +72,18 @@ func _physics_process(delta : float):
time_since_got_player += delta time_since_got_player += delta
time_since_got_move_location += delta time_since_got_move_location += delta
time_since_last_attack += delta time_since_last_attack += delta
last_player_velocity = player.velocity.length() last_player_velocity = target.velocity.length()
last_target_position = agent.target_position last_target_position = agent.target_position
pass pass
func update_attack(delta : float): func update_attack(delta : float):
if time_since_last_attack >= attack_speed: if time_since_last_attack >= attack_speed:
time_since_last_attack = 0 time_since_last_attack = 0
print("bunny attacking with: " + str(damage) + " damage") target.damage(damage)
pass pass
func update_target_pos(): func update_target_pos():
agent.target_position = Vector2(player.global_position) + Vector2(randf() - 0.5 , randf() - 0.5) * 2 * random_goal_spread_factor agent.target_position = Vector2(target.global_position) + Vector2(randf() - 0.5 , randf() - 0.5) * 2 * random_goal_spread_factor
pass pass
func take_damage(damage_amount : int): func take_damage(damage_amount : int):

View file

@ -14,7 +14,6 @@ func spawn_bunny(pos : Vector2, team : int, health : int, damage : int, attack_s
bunny.damage = damage bunny.damage = damage
bunny.attack_speed = attack_speed bunny.attack_speed = attack_speed
bunny.team = team bunny.team = team
bunny.player = player
return bunny return bunny
func spawn_wave(free_tiles : Array, team: int, amount : int, health : int, damage : int, attack_speed : float) -> Array: func spawn_wave(free_tiles : Array, team: int, amount : int, health : int, damage : int, attack_speed : float) -> Array:

View file

@ -2,6 +2,7 @@ extends Node2D
@export var map_generator : Node @export var map_generator : Node
@export var bunny_generator : Node @export var bunny_generator : Node
@export var player : Node
var bunnys = [] var bunnys = []
var gen_data : Dictionary var gen_data : Dictionary
@ -20,6 +21,14 @@ func _process(_delta):
bunnys = await bunny_generator.spawn_batched_wave(25, 0.25, gen_data.free_tiles.duplicate(), TEAM.EVIL, wave * 250, 3, 1, 5.0) bunnys = await bunny_generator.spawn_batched_wave(25, 0.25, gen_data.free_tiles.duplicate(), TEAM.EVIL, wave * 250, 3, 1, 5.0)
for bunny in bunnys: for bunny in bunnys:
bunny.sub_on_death(func(bunny): bunnys.erase(bunny)) bunny.sub_on_death(func(bunny): bunnys.erase(bunny))
bunny.target = get_enemy_target(bunny.team)
bunny.sub_on_death(func(bunny): bunny.queue_free()) bunny.sub_on_death(func(bunny): bunny.queue_free())
in_wave_gen = false in_wave_gen = false
pass pass
func get_enemy_target(own_team : int) -> Node:
match own_team:
TEAM.EVIL: return player
TEAM.GOOD: return null
_: return null
pass

View file

@ -1,7 +1,8 @@
extends CharacterBody2D extends CharacterBody2D
@export var speed = 200 @export var speed := 200.0
@export_range(0, 1) var damping_factor = 0.6 @export var health := 100
@export_range(0, 1) var damping_factor := 0.6
@export var animation_player : AnimationPlayer @export var animation_player : AnimationPlayer
func _physics_process(delta : float): func _physics_process(delta : float):
@ -42,3 +43,9 @@ func update_player_animation():
up_dot: animation_player.play("MoveUp") up_dot: animation_player.play("MoveUp")
down_dot: animation_player.play("MoveDown") down_dot: animation_player.play("MoveDown")
pass pass
func damage(amount : int):
health -= amount
if(health <= 0): print("Player Dead")
pass