added attacks
This commit is contained in:
23
scripts/attack.gd
Normal file
23
scripts/attack.gd
Normal file
@@ -0,0 +1,23 @@
|
||||
extends Node2D
|
||||
class_name Attack
|
||||
|
||||
@onready var cpu_particles_2d: CPUParticles2D = $CPUParticles2D
|
||||
@onready var area_2d: Area2D = $Area2D
|
||||
|
||||
var initiator: Entity
|
||||
|
||||
func set_initiator(entity: Entity) -> void:
|
||||
position = entity.position
|
||||
initiator = entity
|
||||
|
||||
func _ready() -> void:
|
||||
cpu_particles_2d.emitting = true
|
||||
cpu_particles_2d.color = initiator.color
|
||||
|
||||
func _on_cpu_particles_2d_finished() -> void:
|
||||
var collisions := area_2d.get_overlapping_bodies()
|
||||
for object in collisions:
|
||||
if object is Entity and object != initiator:
|
||||
object.hit()
|
||||
print("attack hit %s" % collisions.size())
|
||||
queue_free()
|
||||
@@ -1,23 +1,20 @@
|
||||
extends CharacterBody2D
|
||||
extends Entity
|
||||
class_name Enemy
|
||||
|
||||
@onready var sprite_2d: Sprite2D = $Sprite2D
|
||||
@onready var nav_agent: NavigationAgent2D = $NavigationAgent2D
|
||||
|
||||
var color := Color.WHITE
|
||||
@export var speed = 100
|
||||
var lives: int = 1
|
||||
var vision: int = 100
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
velocity = Vector2.ZERO
|
||||
if Input.is_action_pressed("ui_down"):
|
||||
velocity += Vector2.DOWN
|
||||
if Input.is_action_pressed("ui_up"):
|
||||
velocity += Vector2.UP
|
||||
if Input.is_action_pressed("ui_left"):
|
||||
velocity += Vector2.LEFT
|
||||
if Input.is_action_pressed("ui_right"):
|
||||
velocity += Vector2.RIGHT
|
||||
velocity = velocity.normalized() * speed
|
||||
if nav_agent.is_navigation_finished():
|
||||
call_deferred("new_target_point")
|
||||
return
|
||||
|
||||
var current_agent_position: Vector2 = global_position
|
||||
var next_path_position: Vector2 = nav_agent.get_next_path_position()
|
||||
|
||||
velocity = current_agent_position.direction_to(next_path_position) * speed
|
||||
move_and_slide()
|
||||
for i in range(get_slide_collision_count()):
|
||||
var collision := get_slide_collision(i)
|
||||
@@ -35,3 +32,19 @@ func _physics_process(delta: float) -> void:
|
||||
speed += 12
|
||||
color.b -= 0.25
|
||||
sprite_2d.modulate = color
|
||||
|
||||
func new_target_point():
|
||||
# Wait for the first physics frame so the NavigationServer can sync.
|
||||
await get_tree().physics_frame
|
||||
|
||||
var target_point := Vector2.from_angle(randf_range(0.0, TAU)) * vision
|
||||
var map := get_world_2d().navigation_map
|
||||
var closest_point := NavigationServer2D.map_get_closest_point(map, global_position + target_point)
|
||||
var point_delta := closest_point - target_point
|
||||
var is_on_map = point_delta.is_zero_approx() # Answer to original question!
|
||||
#if not is_on_map:
|
||||
## Wasn't on the map, so push in from edge. If you have thin sections on
|
||||
## your navmesh, this could push it back off the navmesh!
|
||||
#point_delta = point_delta.normalized()
|
||||
#closest_point += point_delta * 32
|
||||
nav_agent.target_position = closest_point
|
||||
|
||||
14
scripts/entity.gd
Normal file
14
scripts/entity.gd
Normal file
@@ -0,0 +1,14 @@
|
||||
extends CharacterBody2D
|
||||
class_name Entity
|
||||
|
||||
signal death
|
||||
|
||||
var color := Color.WHITE
|
||||
@export var speed = 100
|
||||
var lives: int = 1
|
||||
|
||||
func hit() -> void:
|
||||
lives -= 1
|
||||
if lives <= 0:
|
||||
death.emit()
|
||||
queue_free()
|
||||
@@ -6,6 +6,7 @@ class_name HUD
|
||||
@onready var progress_bar_blue: ProgressBar = $PanelContainer/MarginContainer/VBoxContainer/HBoxContainer/PanelContainerBlue/ProgressBar
|
||||
@onready var texture_rect: TextureRect = $PanelContainer/MarginContainer/VBoxContainer/TextureRect
|
||||
@onready var lives_container: GridContainer = $PanelContainer/MarginContainer/VBoxContainer/LivesContainer
|
||||
@onready var entity_count_label: Label = $EntityCountContainer/MarginContainer/EntityCountLabel
|
||||
const HEART = preload("res://assets/heart.png")
|
||||
|
||||
func _on_player_color_changed(color: Color) -> void:
|
||||
@@ -23,3 +24,6 @@ func _on_player_lives_changed(lives: int) -> void:
|
||||
lives_container.add_child(life)
|
||||
elif heart_containers.size() > lives:
|
||||
heart_containers.pop_back().queue_free()
|
||||
|
||||
func update_entity_count(count: int):
|
||||
entity_count_label.text = "Entity Count: %s" % count
|
||||
|
||||
@@ -8,6 +8,7 @@ var spawn_locations: Array[Vector2] = []
|
||||
|
||||
@onready var hud: HUD = $HUD
|
||||
@onready var spawn_zones: Node2D = $SpawnZones
|
||||
@onready var entity_container: Node = $EntityContainer
|
||||
|
||||
func _ready() -> void:
|
||||
var spawn_zone_array := spawn_zones.get_children()
|
||||
@@ -17,6 +18,7 @@ func _ready() -> void:
|
||||
for x in range(zone.shape.size.x / 16):
|
||||
for y in range(zone.shape.size.y / 16):
|
||||
spawn_locations.append(top_left + Vector2(x * 16, y * 16))
|
||||
hud.update_entity_count(spawn_locations.size())
|
||||
var player_loc: Vector2 = spawn_locations.pick_random()
|
||||
for spawn in spawn_locations:
|
||||
var entity
|
||||
@@ -27,4 +29,9 @@ func _ready() -> void:
|
||||
else:
|
||||
entity = ENEMY.instantiate()
|
||||
entity.position = spawn
|
||||
add_child(entity)
|
||||
entity.death.connect(_on_entity_death)
|
||||
entity_container.add_child(entity)
|
||||
|
||||
func _on_entity_death():
|
||||
var entities = entity_container.get_child_count()
|
||||
hud.update_entity_count(entities)
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
extends CharacterBody2D
|
||||
extends Entity
|
||||
class_name Player
|
||||
|
||||
signal color_changed(color: Color)
|
||||
signal lives_changed(lives: int)
|
||||
|
||||
@onready var sprite_2d: Sprite2D = $Sprite2D
|
||||
const ATTACK := preload("res://scene/attack.tscn")
|
||||
|
||||
var color := Color.WHITE
|
||||
@export var speed = 100
|
||||
var lives: int = 1
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
var input_direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
|
||||
@@ -33,3 +31,8 @@ func _physics_process(delta: float) -> void:
|
||||
color.b -= 0.25
|
||||
sprite_2d.modulate = color
|
||||
color_changed.emit(color)
|
||||
|
||||
if Input.is_action_just_pressed("ui_accept"):
|
||||
var attack: Attack = ATTACK.instantiate()
|
||||
attack.set_initiator(self)
|
||||
get_parent().add_sibling(attack)
|
||||
|
||||
Reference in New Issue
Block a user