Skip to content

Commit

Permalink
Added enemy spawner entity/components.
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ A rogue-like game built in bevy and rust.
### Screenshots:
![island](images/island.png)
![zoom-island](images/zoom-island.png)
![spider-spawner](images/spider-spawn.png)

### Credits:
- `Toen's Medieval Strategy Sprite Pack` - Created by Andre Mari Coppola (http://toen.itch.io/toens-medieval-strategy)
Expand Down
Binary file added assets/textures/cave_sprite.aseprite
Binary file not shown.
Binary file added assets/textures/cave_sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/textures/spider_sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/spider_sprite.png~
Binary file not shown.
Binary file added images/spider-spawn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions src/game/gameplay/enemy/mod.rs
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());
}
66 changes: 66 additions & 0 deletions src/game/gameplay/enemy/spawner.rs
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;
}
}
}
1 change: 1 addition & 0 deletions src/game/gameplay/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod player;
pub mod enemy;
1 change: 0 additions & 1 deletion src/game/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub fn loading(
tilemap_atlas_handles: Res<TilemapAtlasHandles>,
) {
if get_has_map_assets(asset_server, tilemap_atlas_handles) {
dbg!("Changing state to generating");
game_state.set(GameState::Generating).unwrap();
}
}
10 changes: 8 additions & 2 deletions src/game/map/generate_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::{prelude::*};
use noise::{Fbm, MultiFractal, NoiseFn, Seedable};
use pathfinding::prelude::{absdiff, astar};
use rand::{Rng, prelude::ThreadRng, thread_rng};
use crate::game::GameState;
use crate::game::{GameState, gameplay::enemy::spawner};
use super::Map;

fn get_island_shape(x: f64, y: f64) -> f64 {
Expand Down Expand Up @@ -44,7 +44,6 @@ fn generate_road(tilemap: &mut Tilemap, random: &mut ThreadRng) -> Vec<Vec2> {
Some(tile) => {
if tile.index == 19 {
let range = 30..(ray_index - 2);
dbg!(&range);
let random_ray_position = random.gen_range(range);
let road_position: Vec2 = current_direction * (random_ray_position as f32);
road_points.push(road_position);
Expand Down Expand Up @@ -163,6 +162,9 @@ pub fn find_road_path(road_points: &Vec<Vec2>, tilemap: &mut Tilemap) -> Vec<(i3
}

pub fn generate_map(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut game_state: ResMut<State<GameState>>,
mut map: ResMut<Map>,
mut tilemap_query: Query<&mut Tilemap>
Expand Down Expand Up @@ -232,6 +234,10 @@ pub fn generate_map(
let road_path = find_road_path(&road_points, &mut tilemap);
map.road_path = road_path;

let random_road_index = random.gen_range(0..map.road_path.len() - 1);
let road_point = map.road_path[random_road_index];
spawner::spawn(&mut commands, &asset_server, &mut materials, Vec2::new(road_point.0 as f32 * 16.0, road_point.1 as f32 * 16.0) + Vec2::new(8.0, 8.0));

for x in -half_map_width..half_map_width {
for y in -half_map_height..half_map_height {
tilemap.spawn_chunk((x, y)).unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl Plugin for GamePlugin {
SystemSet::on_update(GameState::Playing)
.label("realtime_update")
.with_system(camera::camera_movement.system())
.with_system(gameplay::enemy::spawner::tick.system())
)
.add_system_to_stage(
CoreStage::PostUpdate,
Expand Down

0 comments on commit 10a74fa

Please sign in to comment.