Skip to content

Commit

Permalink
spawn enemies at random distance and direction from player
Browse files Browse the repository at this point in the history
  • Loading branch information
thombruce committed Nov 18, 2023
1 parent 5dd1e55 commit c7e446c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Spawn enemies at random distance and direction from player
- Decrease static orbit scale (planets are now closer)
- Clamp orbit distance (prevents Moon orbiting inside of Earth sprite)
- Indicators spawn on update allowing new entities to be pointed to
Expand Down
16 changes: 14 additions & 2 deletions src/ships/enemy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
use rand::Rng;

use crate::core::resources::assets::SpriteAssets;
use crate::systems::events::BulletSpawnEvent;
Expand Down Expand Up @@ -84,11 +85,23 @@ pub(crate) fn spawn_enemies(
mut commands: Commands,
sprites: Res<SpriteAssets>,
mut spawn_timer: ResMut<SpawnTimer>,
player_position: Query<&Transform, With<Player>>,
) {
// tick the timer
spawn_timer.0.tick(time.delta());

if spawn_timer.0.finished() {
let Ok(from) = player_position.get_single() else {
return;
};

let random_x = rand::thread_rng().gen_range(-25_000.0..25_000.0);
let random_y = rand::thread_rng().gen_range(-25_000.0..25_000.0);

let new_pos = (from.translation.truncate()
+ Vec2::new(random_x, random_y).clamp_length_min(5_000.0))
.extend(100.0);

// Spawns enemy ships
commands.spawn((
Enemy,
Expand All @@ -103,8 +116,7 @@ pub(crate) fn spawn_enemies(
SpriteBundle {
texture: sprites.enemy_ship.clone(),
transform: Transform {
// TODO: Randomise spawn location
translation: Vec3::new(25_000.0, 5_000.0, 100.0),
translation: new_pos,
scale: Vec3::splat(0.5),
..default()
},
Expand Down

0 comments on commit c7e446c

Please sign in to comment.