-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added enemy spawner entity/components.
Added enemy entity/components. Generated a single spawner on our map. Removed dbg!'s. Updated readme with new screenshot.
- Loading branch information
StarArawn
authored and
StarArawn
committed
Apr 15, 2021
1 parent
c98dc49
commit 10a74fa
Showing
12 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use bevy::prelude::*; | ||
|
||
pub mod spawner; | ||
|
||
#[derive(Default)] | ||
pub struct Enemy { | ||
} | ||
|
||
pub fn spawn_map_enemy( | ||
commands: &mut Commands, | ||
asset_server: &Res<AssetServer>, | ||
materials: &mut ResMut<Assets<ColorMaterial>>, | ||
position: Vec2, | ||
) { | ||
let texture_handle: Handle<Texture> = asset_server.load("textures/spider_sprite.png"); | ||
let enemy_sprite_material = materials.add(texture_handle.into()); | ||
commands | ||
.spawn_bundle(SpriteBundle { | ||
material: enemy_sprite_material, | ||
transform: Transform::from_xyz(position.x, position.y, 12.0), | ||
..Default::default() | ||
}) | ||
.insert(Enemy::default()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use bevy::prelude::*; | ||
use rand::{Rng, prelude::ThreadRng, thread_rng}; | ||
|
||
use super::spawn_map_enemy; | ||
|
||
#[derive(Default)] | ||
pub struct Spawner { | ||
wait_time: f64, // How long until a enemy spawns | ||
last_time: f64, // The time since the last spawn | ||
limit: u32, // The maximum enemy's that can spawn from this spawner. | ||
current: u32, // The current count of spawned enemies. | ||
range: f32, // How far away from the spawner an enemy can spawn. | ||
} | ||
|
||
pub fn spawn( | ||
commands: &mut Commands, | ||
asset_server: &Res<AssetServer>, | ||
materials: &mut ResMut<Assets<ColorMaterial>>, | ||
position: Vec2, | ||
) { | ||
let texture_handle: Handle<Texture> = asset_server.load("textures/cave_sprite.png"); | ||
let cave_sprite_material = materials.add(texture_handle.into()); | ||
commands | ||
.spawn_bundle(SpriteBundle { | ||
material: cave_sprite_material, | ||
transform: Transform::from_xyz(position.x, position.y, 10.0), | ||
..Default::default() | ||
}) | ||
.insert(Spawner { | ||
wait_time: 10.0, | ||
last_time: 0.0, | ||
limit: 3, | ||
current: 0, | ||
range: 2.0 | ||
}); | ||
} | ||
|
||
pub fn tick( | ||
mut commands: Commands, | ||
asset_server: Res<AssetServer>, | ||
mut materials: ResMut<Assets<ColorMaterial>>, | ||
mut spawner_query: Query<(&Transform, &mut Spawner)>, | ||
time: Res<Time>, | ||
) { | ||
let current_time = time.seconds_since_startup(); | ||
|
||
let spawn_pos = vec![ | ||
Vec2::new(16.0, 0.0), | ||
Vec2::new(-16.0, 0.0), | ||
Vec2::new(0.0, 0.0), | ||
Vec2::new(0.0, 16.0), | ||
Vec2::new(0.0, -16.0), | ||
]; | ||
|
||
let mut random = thread_rng(); | ||
|
||
for (transform, mut spawner) in spawner_query.iter_mut() { | ||
let spawner_elapsed_time = current_time - spawner.last_time; | ||
if spawner_elapsed_time > spawner.wait_time && spawner.current < spawner.limit { | ||
let offset = spawn_pos[random.gen_range(0..4)]; | ||
spawn_map_enemy(&mut commands, &asset_server, &mut materials, Vec2::new(transform.translation.x, transform.translation.y) + offset); | ||
spawner.last_time = current_time; | ||
spawner.current += 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod player; | ||
pub mod enemy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters