Skip to content

Commit

Permalink
feat: Improved enemy fleet formations
Browse files Browse the repository at this point in the history
Now fleets enter the patrol area each ship start its patrol around the prefixed area indexes
  • Loading branch information
simone-lungarella authored and Lincoln-Ab committed Jul 20, 2024
1 parent 7c9a3a9 commit a3dd4df
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 101 deletions.
2 changes: 1 addition & 1 deletion assets/json_data/json_enemy_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"SHIP": {
"max_health": 100,
"impact_damage": 20,
"speed": 60,
"speed": 400,
"gun": {
"fire_rate": 60,
"ammo_burst": 0,
Expand Down
71 changes: 14 additions & 57 deletions assets/json_data/json_wave_data.json
Original file line number Diff line number Diff line change
@@ -1,66 +1,23 @@
{
"formations": [
{
"formation_name": "t",
"odds": 0.2,
"steps": {
"0": [
1,
2,
5,
7
],
"1": [
3,
4,
5,
7
],
"2": [
7,
8,
5,
7
]
"formation_name": "A",
"odds": 0.4,
"units": {
"0": [ 1, 3, 7 ],
"1": [ 3, 7, 1 ],
"2": [ 7, 1, 3 ],
}
},
{
"formation_name": "y",
"odds": 0.3,
"steps": {
"0": [
1
],
"1": [
3
],
"2": [
7
],
"3": [
12
]
}
},
{
"formation_name": "x",
"odds": 0.5,
"steps": {
"0": [
1
],
"1": [
3
],
"2": [
7
],
"3": [
11
],
"4": [
13
]
"formation_name": "X",
"odds": 0.6,
"units": {
"0": [ 1, 3, 13, 11 ],
"1": [ 3, 13, 11, 1 ],
"2": [ 7],
"3": [ 11, 1, 3, 13 ],
"4": [ 13, 11, 1, 3 ],
}
}
]
Expand Down
50 changes: 34 additions & 16 deletions scripts/enemy/enemy.gd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extends Area2D
class_name Enemy

@export var enemy_type: Enums.EnemyType = Enums.EnemyType.SHIP
@onready var animated_sprite_2d = $AnimatedSprite2D
Expand All @@ -7,6 +8,7 @@ extends Area2D
const ENEMY_REWARD: PackedScene = preload("res://scenes/enemy/Rewards.tscn")
const BULLET: PackedScene = preload("res://scenes/enemy/EnemyBullet.tscn")
const ENEMY_SHOOT_SFX = preload("res://assets/audio/enemy_shoot.wav")
const MIN_DISTANCE = 5


class Animations:
Expand All @@ -23,13 +25,12 @@ var _can_shoot: bool = true
var _burst: int
var _rate: float
var _ammo_count: int
var is_movement_enabled = false

var direction: Vector2 = Vector2.DOWN


func init(steps: Array):
print(steps)
pass
var _direction: Vector2 = Vector2.DOWN
var steps: Array
var _target_position: Vector2
var _last_reached_position: int


func _ready():
Expand All @@ -40,19 +41,36 @@ func _ready():
_burst = _enemy_stats[Literals.EnemyStats.GUN][Literals.EnemyGun.AMMO_BURST]
_rate = 60 / _enemy_stats[Literals.EnemyStats.GUN][Literals.EnemyGun.FIRE_RATE]
_ammo_count = _burst
_last_reached_position = 0
_calculate_target_position()


func _process(delta):
if is_movement_enabled:
if position.distance_to(_target_position) > MIN_DISTANCE:
position += _direction * _velocity * delta
else:
_calculate_target_position()

if _can_shoot:
var space_state = get_world_2d().direct_space_state
var start = self.global_position
var end = start + Vector2.DOWN * 1000
var query = PhysicsRayQueryParameters2D.create(start, end)
var result = space_state.intersect_ray(query)

if result and result.collider is Hero:
_shoot()

func _physics_process(_delta):
# position += Vector2.DOWN * 35 * delta
if _can_shoot:
var space_state = get_world_2d().direct_space_state
var start = self.global_position
var end = start + Vector2.DOWN * 1000
var query = PhysicsRayQueryParameters2D.create(start, end)
var result = space_state.intersect_ray(query)

if result and result.collider is Hero:
_shoot()
func _calculate_target_position():
if _last_reached_position == steps.size() - 1:
_last_reached_position = 0
else:
_last_reached_position += 1

_target_position = steps[_last_reached_position]
_direction = (_target_position - position).normalized()


func get_hurt(damage: int):
Expand Down
7 changes: 3 additions & 4 deletions scripts/enemy/nemesis.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ func clean_restart():

## Setup wave to start spawning enemies.
func _start_wave(index: int):
# TODO: decomment
# const START_DELAY: int = 3
# await get_tree().create_timer(START_DELAY, false).timeout
const START_DELAY: int = 3
await get_tree().create_timer(START_DELAY, false).timeout

# Pick random formation with given odds
var steps = select_random_formation()
Expand Down Expand Up @@ -70,7 +69,7 @@ func select_random_formation():
for formation in formations:
sum += formation[Literals.Waves.ODDS]
if choice <= sum:
return formation[Literals.Waves.STEPS]
return formation[Literals.Waves.UNITS]

push_error("No waves select, the total odds does not adds up to 100%")
return []
58 changes: 36 additions & 22 deletions scripts/fleet.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ const ENEMY_SHIP: PackedScene = preload("res://scenes/enemy/Enemy.tscn")

const NUM_SPAWN_COLUMNS := 5
const NUM_SPAWN_ROWS := 3
const MIN_DISTANCE = 5

# TODO: handle different enemy fleets
var _enemy_stats = StaticData.enemy_data[Enums.EnemyType.keys()[Enums.EnemyType.SHIP]]

var slot_size: Vector2
var patrol_area_position: Vector2
var long_side
var short_side

var _velocity
var direction


func _ready():
var viewport = get_viewport().size
Expand All @@ -22,39 +25,50 @@ func _ready():
slot_size = Vector2(long_side / NUM_SPAWN_COLUMNS, short_side / NUM_SPAWN_ROWS)
patrol_area_position = Vector2(viewport.x / 2, short_side / 2)

_velocity = _enemy_stats[Literals.EnemyStats.SPEED]
direction = (patrol_area_position - position).normalized()

func init(steps):
for index in range(0, steps.size()):
var enemy_steps = steps[String.num(index)]
var enemy = ENEMY_SHIP.instantiate()

enemy.position = get_ship_position(enemy_steps[0])
# enemy.init(enemy_steps)
add_child(enemy)
func init(units):
print("[Fleet] - Initializing %d units in the fleet" % units.size())
for index in range(0, units.size()):
var enemy = ENEMY_SHIP.instantiate()
var enemy_steps = units[String.num(index)]

enemy.position = _get_ship_position(enemy_steps[0])
enemy.steps = _get_ship_step_positions(enemy_steps)

func _physics_process(delta):
var distance: float = position.distance_to(patrol_area_position)
var _velocity = _enemy_stats[Literals.EnemyStats.SPEED]
add_child(enemy)

if distance > 5:
if position.x < patrol_area_position.x:
position.x += delta * _velocity
else:
position.x -= delta * _velocity

if position.y < patrol_area_position.y:
position.y += delta * _velocity
else:
position.y -= delta * _velocity
# position += Vector2.DOWN * 60 * delta
func _process(delta):
if position.distance_to(patrol_area_position) > MIN_DISTANCE:
position += direction * _velocity * delta
else:
set_process(false)
_enable_ships_movement()


func get_ship_position(piece_index: int) -> Vector2:
func _get_ship_position(piece_index: int) -> Vector2:
# Piece position coordinates
var side_x: float = slot_size.x * (piece_index % NUM_SPAWN_COLUMNS) - long_side / 2
var side_y: float = slot_size.y * int(piece_index / float(NUM_SPAWN_COLUMNS)) - short_side / 2

var pos_x: float = side_x + slot_size.x / 2
var pos_y: float = side_y + slot_size.y / 2
return Vector2(pos_x, pos_y)


func _enable_ships_movement():
for child in get_children():
if child is Enemy:
child.is_movement_enabled = true


func _get_ship_step_positions(enemy_steps: Array) -> Array:
var positions := Array()

for step in enemy_steps:
positions.append(_get_ship_position(step))

return positions
2 changes: 1 addition & 1 deletion scripts/literals.gd
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ class EnemyGun:
## Information on enemy waves
class Waves:
const FORMATIONS := "formations"
const STEPS := "steps"
const UNITS := "units"
const ODDS := "odds"

0 comments on commit a3dd4df

Please sign in to comment.