Skip to content

Commit

Permalink
Multitile world objs are now updated and kept in sync with their owne…
Browse files Browse the repository at this point in the history
…r entity
  • Loading branch information
benlloyd50 committed Jan 21, 2023
1 parent bfccbe3 commit 36d14fd
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 51 deletions.
8 changes: 4 additions & 4 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
effects::lerp,
player::{Player, SystemOrder},
AppState,
AppState,
};
use bevy::{input::mouse::MouseWheel, prelude::*};
use iyes_loopless::prelude::*;
Expand All @@ -12,8 +12,8 @@ pub struct CameraPlugin;

impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) {
app.add_enter_system(AppState::GameLoading, load_camera)
.add_system_set( ConditionSet::new()
app.add_enter_system(AppState::GameLoading, load_camera).add_system_set(
ConditionSet::new()
.run_in_state(AppState::Running)
.label("graphicDelay")
.after(SystemOrder::Graphic)
Expand Down Expand Up @@ -52,7 +52,7 @@ fn camera_follow_player(
let player_pos = player_q.single();

let lerped_pos = lerp(cam_pos.translation.truncate(), player_pos.translation.truncate(), 0.15);

// Bound the position by the map so we dont see what's past it
// let x_offset = cam_pos.scale.x * 39.5 * TILE_PIXELS_X;
// let y_offset = cam_pos.scale.x * 39.5 * TILE_PIXELS_Y;
Expand Down
3 changes: 1 addition & 2 deletions src/comfort_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::Deserialize;
use std::error::Error;
use std::fs;
use serde::Deserialize;

#[derive(Deserialize)]
pub struct ComfortConfig {
Expand All @@ -25,4 +25,3 @@ pub fn load_settings(preset: &str) -> Result<FractalSettings, Box<dyn Error>> {
_ => Ok(decoded.terrainperlin),
}
}

29 changes: 22 additions & 7 deletions src/interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct InteractPlugin;

impl Plugin for InteractPlugin {
fn build(&self, app: &mut App) {
app.add_event::<HealthBelowZeroEvent>().add_system(destroy_dead);
app.add_event::<HealthBelowZeroEvent>().add_system(cleanup_world_objs);
}
}

Expand Down Expand Up @@ -35,17 +35,32 @@ impl Health {
}
}

//TODO: update all entities that relate to the tileobj
fn destroy_dead(
// Removes dead world objs that are sent via the HealthBelowZeroEvent
fn cleanup_world_objs(
mut commands: Commands,
mut ev_killed: EventReader<HealthBelowZeroEvent>,
mut tile_storage_q: Query<&mut TileStorage, With<Blocking>>,
world_objs_q: Query<(Entity, &ObjectSize, &TilePos)>,
) {
//
for ev in ev_killed.iter() {
for mut tile_storage in tile_storage_q.iter_mut() {
tile_storage.remove(&ev.1);
for (obj, obj_size, obj_pos) in world_objs_q.iter() {
match obj_size {
ObjectSize::Single => {
for mut tile_storage in tile_storage_q.iter_mut() {
tile_storage.remove(&ev.1);
}
commands.entity(ev.0).despawn_recursive();
}
ObjectSize::Multi(owner) => {
if *owner == ev.0 {
// will remove the owner and the tiles associated with the owner
for mut tile_storage in tile_storage_q.iter_mut() {
tile_storage.remove(obj_pos);
}
commands.entity(obj).despawn_recursive();
}
}
}
}
commands.entity(ev.0).despawn_recursive();
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod comfort_config;
mod constants;
mod effects;
mod entity_tile_pos;
mod comfort_config;

mod assets;
use assets::AssetLoadPlugin;
Expand Down
18 changes: 11 additions & 7 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,26 @@ fn directional_input_handle(
if let Some((dest_entity, size, _)) = obj_tiles_q.iter().find(|x| dest_tile.eq(x.2)) {
match *size {
ObjectSize::Single => {
ev_interact.send(Interaction { sender: player_entity, reciever: dest_entity});
},
ev_interact.send(Interaction {
sender: player_entity,
reciever: dest_entity,
});
}
ObjectSize::Multi(owner) => {
if let Ok(_) = blocking_interact_q.get(owner) {
ev_interact.send(Interaction { sender: player_entity, reciever: owner});
ev_interact.send(Interaction {
sender: player_entity,
reciever: owner,
});
};
},
}
}
} else if let Some(_) = blocking_q.iter().find(|elem| dest_tile.eq(elem)) {
return;
}
else {
} else {
ev_moveplayer.send(MoveEvent(player_entity, dest_tile));
}


// if let Some((interactable_entity, _)) = blocking_interact_q.iter().find(|(_, elem)| dest_tile.eq(elem)) {
// ev_interact.send(Interaction { sender: player_entity, reciever: interactable_entity });
// }
Expand Down
46 changes: 16 additions & 30 deletions src/world_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use bevy_ecs_tilemap::prelude::*;
use iyes_loopless::prelude::*;

use crate::{
assets::SpriteAssets, comfort_config::load_settings, constants::world_obj_sprites::*,
interact::*, AppState,
assets::SpriteAssets, comfort_config::load_settings, constants::world_obj_sprites::*, interact::*, AppState,
};

pub const MAP_SIZE_X: u32 = 128; // Size of map currently only supports square maps
Expand Down Expand Up @@ -74,18 +73,16 @@ fn create_world(mut commands: Commands, tiles: Res<SpriteAssets>) {
.spawn_trees(&mut commands)
.spawn_flowers(&mut commands);

commands
.entity(overworld.floor_tilemap)
.insert(TilemapBundle {
grid_size: tilegridsize_pixels(),
map_type: TilemapType::default(),
size: tilemap_size,
storage: overworld.floor_tiles,
texture: TilemapTexture::Single(tiles.terrain.clone()),
tile_size: tilemaptilesize_pixels(),
transform: Transform::from_translation(Vec3::new(0f32, 0f32, FLOOR_Z)),
..Default::default()
});
commands.entity(overworld.floor_tilemap).insert(TilemapBundle {
grid_size: tilegridsize_pixels(),
map_type: TilemapType::default(),
size: tilemap_size,
storage: overworld.floor_tiles,
texture: TilemapTexture::Single(tiles.terrain.clone()),
tile_size: tilemaptilesize_pixels(),
transform: Transform::from_translation(Vec3::new(0f32, 0f32, FLOOR_Z)),
..Default::default()
});
commands.entity(overworld.objs_tilemap).insert((
TilemapBundle {
grid_size: tilegridsize_pixels(),
Expand Down Expand Up @@ -181,9 +178,7 @@ impl GameWorld {
let tree_base_pos = TilePos { x, y };
let tree_top_pos = TilePos { x, y: y + 1 };

if self.blocked_tiles.contains(&tree_base_pos)
|| self.blocked_tiles.contains(&tree_top_pos)
{
if self.blocked_tiles.contains(&tree_base_pos) || self.blocked_tiles.contains(&tree_top_pos) {
continue;
}

Expand All @@ -192,8 +187,7 @@ impl GameWorld {

if perlin_value < 0.2f32 || perlin_value > 0.6f32 {
//spawn object
let (base_entity, top_entity) =
place_medium_tree(commands, &self.objs_tilemap, &tree_base_pos);
let (base_entity, top_entity) = place_medium_tree(commands, &self.objs_tilemap, &tree_base_pos);
self.objs_tiles.set(&tree_base_pos, base_entity);
self.objs_tiles.set(&tree_top_pos, top_entity);
}
Expand Down Expand Up @@ -229,11 +223,7 @@ impl GameWorld {
}
}

fn place_medium_tree(
commands: &mut Commands,
blocked_tilemap: &Entity,
tree_base_pos: &TilePos,
) -> (Entity, Entity) {
fn place_medium_tree(commands: &mut Commands, blocked_tilemap: &Entity, tree_base_pos: &TilePos) -> (Entity, Entity) {
let base_entity = commands.spawn_empty().id();
let top_entity = commands.spawn_empty().id();
let obj_size = ObjectSize::Multi(base_entity);
Expand Down Expand Up @@ -266,10 +256,7 @@ fn place_medium_tree(
(base_entity, top_entity)
}

fn stretch_tree(
mut tree_q: Query<(&mut Transform, &TilePos), With<Tree>>,
keeb: Res<Input<KeyCode>>,
) {
fn stretch_tree(mut tree_q: Query<(&mut Transform, &TilePos), With<Tree>>, keeb: Res<Input<KeyCode>>) {
if keeb.pressed(KeyCode::K) {
for (mut transform, _) in tree_q.iter_mut() {
transform.scale.x += 0.06;
Expand Down Expand Up @@ -328,8 +315,7 @@ fn tilemaptilesize_pixels() -> TilemapTileSize {

//====> World Data Components
// Marks a tile as being part of an object, the Entity will contain the data for the object
#[derive(Clone, Copy)]
#[derive(Component)]
#[derive(Clone, Copy, Component)]
pub enum ObjectSize {
Single,
Multi(Entity),
Expand Down

0 comments on commit 36d14fd

Please sign in to comment.