Skip to content

Commit

Permalink
Multitile objects are now connected and share data, WIP on cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Lloyd committed Jan 20, 2023
1 parent 52a80f1 commit bfccbe3
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 85 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ edition = "2021"
bevy = "0.9"
bevy_asset_loader = { version = "0.14", features = ["2d", "stageless"]}
bevy_ecs_tilemap = { version = "0.9", features = ["atlas"] }
bevy_pixel_camera = "0.3"
iyes_loopless = "0.9.1"
rand = "0.8.5"
bracket-noise = "0.8"
Expand Down
6 changes: 6 additions & 0 deletions docs/planning.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@
## Ideas
- A flowing ocean with animated tiles?
- Fishing
- Mining ideas, multi block hit so you can hit multiple blocks
- Certain terrain could require upgraded materials

## MultiTile problem
- we have a dest_tile we need to see if an interactable object exists there
- interactable objects can take from 1 to many tilepos
4 changes: 1 addition & 3 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
AppState,
};
use bevy::{input::mouse::MouseWheel, prelude::*};
use bevy_pixel_camera::PixelCameraBundle;
use iyes_loopless::prelude::*;

const CAM_Z: f32 = 100.; // Generally the highest depth and sprites past it will not be rendered
Expand All @@ -30,12 +29,11 @@ struct CamScrollLock(bool);

fn load_camera(mut commands: Commands) {
let _camera_entity = commands
// .spawn(PixelCameraBundle::from_resolution(160, 120));
.spawn((
Camera2dBundle {
transform: Transform::from_xyz(0.0, 0.0, CAM_Z),
projection: OrthographicProjection {
scale: 1.0,
scale: 0.5,
..default()
},
..default()
Expand Down
1 change: 1 addition & 0 deletions src/constants/world_obj_sprites.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Position of sprites inside the world_objs.png
#![allow(dead_code)]

pub const TREE_TOP: u32 = 0;
Expand Down
7 changes: 4 additions & 3 deletions src/interact.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::world_gen::Blocking;
use crate::world_gen::{Blocking, ObjectSize};
use bevy::prelude::*;
use bevy_ecs_tilemap::prelude::*;

Expand All @@ -12,11 +12,10 @@ impl Plugin for InteractPlugin {

/// Type of the interaction with relavent information
#[derive(Component)]
#[allow(dead_code)]
pub enum Interact {
Harvest(Health),
#[allow(dead_code)]
Pickup(),
#[allow(dead_code)]
Consume(),
}

Expand All @@ -36,11 +35,13 @@ impl Health {
}
}

//TODO: update all entities that relate to the tileobj
fn destroy_dead(
mut commands: Commands,
mut ev_killed: EventReader<HealthBelowZeroEvent>,
mut tile_storage_q: Query<&mut TileStorage, With<Blocking>>,
) {
//
for ev in ev_killed.iter() {
for mut tile_storage in tile_storage_q.iter_mut() {
tile_storage.remove(&ev.1);
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use player::PlayerPlugin;
use bevy::prelude::*;
use bevy::window::PresentMode;
use bevy_ecs_tilemap::TilemapPlugin;
use bevy_pixel_camera::PixelCameraPlugin;
use iyes_loopless::prelude::*;

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
Expand All @@ -31,7 +30,6 @@ fn main() {
App::new()
.add_loopless_state(AppState::AssetLoading) // Starting state which leads to the plugin doing its job first
.add_plugin(DefaultPluginsWithImage)
// .add_plugin(PixelCameraPlugin)
.add_plugin(AssetLoadPlugin)
.add_plugin(TilemapPlugin)
.add_plugin(WorldGenerationPlugin)
Expand Down
35 changes: 28 additions & 7 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
effects::lerp,
entity_tile_pos::EntityTilePos,
interact::{HealthBelowZeroEvent, Interact},
world_gen::{within_bounds, Blocking},
world_gen::{within_bounds, Blocking, ObjectSize},
AppState,
};

Expand Down Expand Up @@ -148,8 +148,10 @@ fn update_sprite_position<Type: Component>(mut entity_q: Query<(&mut Transform,
/// Checks to ensure dest tile is inbounds of the map
fn directional_input_handle(
mut player_q: Query<(Entity, &EntityTilePos, &mut Direction), With<Player>>,
interactables_q: Query<(Entity, &TilePos), With<Interact>>,
blocking_q: Query<&TilePos, With<Blocking>>,
blocking_interact_q: Query<(Entity, &TilePos), (With<Interact>, With<Blocking>)>,
obj_tiles_q: Query<(Entity, &ObjectSize, &TilePos)>,
tile_storage_q: Query<&TileStorage>,
keeb: Res<Input<KeyCode>>,
mut ev_moveplayer: EventWriter<MoveEvent>,
mut ev_interact: EventWriter<Interaction>,
Expand Down Expand Up @@ -187,16 +189,32 @@ fn directional_input_handle(
y: dest_tile.y as u32,
};

// if there are any in here then we cant move
if let Some((interactable_entity, _)) = interactables_q.iter().find(|(_, elem)| dest_tile.eq(elem)) {
ev_interact.send(Interaction { sender: player_entity, reciever: interactable_entity });
}
else if let Some(_) = blocking_q.iter().find(|elem| dest_tile.eq(elem)) {
// check if the tile is an interactable
// is the dest_tile part of a multi tile -> get owner entity
// is the owner entity in the interactable query -> get entity with components
// give entity to the interact system
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});
},
ObjectSize::Multi(owner) => {
if let Ok(_) = blocking_interact_q.get(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 {
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 });
// }
}

fn player_interact_handler(
Expand All @@ -206,15 +224,18 @@ fn player_interact_handler(
mut ev_killed: EventWriter<HealthBelowZeroEvent>,
) {
for ev in ev_interact.iter() {
//TODO: remove this if not necessary
let _ = match player_q.get_mut(ev.sender) {
Ok(player_entity) => player_entity,
Err(_) => return,
};

let mut interact = match interactables_q.get_mut(ev.reciever) {
Ok(interact) => interact,
Err(_) => return,
};
match &mut *interact.0 {
// TODO: this should be moved into their own interact fns
Interact::Harvest(health) => {
if health.hp <= 0 {
return;
Expand Down
Loading

0 comments on commit bfccbe3

Please sign in to comment.