Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
64kramsystem committed Jul 22, 2022
1 parent f063b10 commit ae35d77
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 2 deletions.
4 changes: 4 additions & 0 deletions assets/beach/beach.level.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ enemies:
location: [200, -10, 0]
- fighter: *bandit
location: [250, -50, 0]

items:
- item: &bottle /items/bottle/bottle.item.yaml
location: [275, 0, 0]
5 changes: 5 additions & 0 deletions assets/items/bottle/bottle.item.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Bottle

image:
image: bottle.png
image_size: [11, 31]
Binary file added assets/items/bottle/bottle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub fn register(app: &mut bevy::prelude::App) {
.add_asset_loader(LevelMetaLoader)
.add_asset::<FighterMeta>()
.add_asset_loader(FighterLoader)
.add_asset::<ItemMeta>()
.add_asset_loader(ItemLoader)
.add_asset::<EguiFont>()
.add_asset_loader(EguiFontLoader);
}
Expand Down Expand Up @@ -256,6 +258,38 @@ impl AssetLoader for FighterLoader {
}
}

pub struct ItemLoader;

impl AssetLoader for ItemLoader {
fn load<'a>(
&'a self,
bytes: &'a [u8],
load_context: &'a mut bevy::asset::LoadContext,
) -> bevy::utils::BoxedFuture<'a, Result<(), anyhow::Error>> {
Box::pin(async move {
let mut meta: ItemMeta = serde_yaml::from_slice(bytes)?;
trace!(?meta, "Loaded item asset");

let self_path = load_context.path();
let mut dependencies = Vec::new();

let image_path = relative_asset_path(self_path, &meta.image.image);
let image_path = AssetPath::new(image_path, None);
let image_handle = load_context.get_handle(image_path.clone());
dependencies.push(image_path);
meta.image.image_handle = image_handle;

load_context.set_default_asset(LoadedAsset::new(meta).with_dependencies(dependencies));

Ok(())
})
}

fn extensions(&self) -> &[&str] {
&["item.yml", "item.yaml"]
}
}

#[derive(Debug, Clone, TypeUuid)]
#[uuid = "da277340-574f-4069-907c-7571b8756200"]
pub struct EguiFont(pub egui::FontData);
Expand Down
23 changes: 22 additions & 1 deletion src/item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use bevy::{
math::Vec2,
prelude::{default, AssetServer, Commands, Component, EventReader, Res, Transform},
prelude::{
default, AssetServer, Bundle, Commands, Component, EventReader, Handle, Res, Transform,
},
sprite::SpriteBundle,
};
use bevy_rapier2d::prelude::*;
Expand All @@ -10,12 +12,31 @@ use crate::{
attack::Attack,
collisions::BodyLayers,
consts::{self, ITEM_HEIGHT, ITEM_LAYER, ITEM_WIDTH},
metadata::{ItemMeta, ItemSpawnMeta},
movement::{MoveInArc, Rotate},
};

#[derive(Component)]
pub struct Item;

#[derive(Bundle)]
pub struct ItemSpawnBundle {
item_meta_handle: Handle<ItemMeta>,
location: Transform,
}

impl ItemSpawnBundle {
pub fn new(item_spawn_meta: &ItemSpawnMeta) -> Self {
let item_meta_handle = item_spawn_meta.item_handle.clone();
let location = Transform::from_translation(item_spawn_meta.location);

Self {
item_meta_handle,
location,
}
}
}

pub struct ThrowItemEvent {
pub position: Vec2,
pub facing: Facing,
Expand Down
26 changes: 25 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use audio::*;
use camera::*;
use collisions::*;
use item::{spawn_throwable_items, ThrowItemEvent};
use metadata::{FighterMeta, GameMeta, LevelMeta};
use metadata::{FighterMeta, GameMeta, ItemMeta, LevelMeta};
use movement::*;
use serde::Deserialize;
use state::{State, StatePlugin};
Expand All @@ -65,6 +65,7 @@ use crate::{
attack::{attack_cleanup, attack_tick},
config::EngineConfig,
input::PlayerAction,
item::ItemSpawnBundle,
metadata::Settings,
};

Expand Down Expand Up @@ -225,6 +226,7 @@ fn main() {
ConditionSet::new()
.run_in_state(GameState::InGame)
.with_system(load_fighters)
.with_system(load_items)
.with_system(spawn_throwable_items)
.with_system(player_controller)
.with_system(y_sort)
Expand Down Expand Up @@ -362,6 +364,11 @@ fn load_level(
commands.spawn_bundle(EnemyBundle::new(enemy));
}

// Spawn the items
for item_spawn_meta in &level.items {
commands.spawn_bundle(ItemSpawnBundle::new(item_spawn_meta));
}

commands.insert_resource(level.clone());
commands.insert_resource(NextState(GameState::InGame));
} else {
Expand Down Expand Up @@ -397,6 +404,23 @@ fn hot_reload_level(
}
}

fn load_items(
mut commands: Commands,
item_spawns: Query<(Entity, &Transform, &Handle<ItemMeta>), Without<Sprite>>,
item_assets: Res<Assets<ItemMeta>>,
asset_server: Res<AssetServer>,
) {
for (entity, location, item_handle) in item_spawns.iter() {
if let Some(item_meta) = item_assets.get(item_handle) {
commands.entity(entity).insert_bundle(SpriteBundle {
texture: asset_server.load(&item_meta.image.image),
transform: *location,
..default()
});
}
}
}

/// Load all fighters that have their handles spawned.
///
/// Fighters are spawned as "stubs" that only contain a transform, a marker component, and a
Expand Down
20 changes: 20 additions & 0 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub struct LevelMeta {
pub players: Vec<FighterSpawnMeta>,
#[serde(default)]
pub enemies: Vec<FighterSpawnMeta>,
#[serde(default)]
pub items: Vec<ItemSpawnMeta>,
pub music: String,
#[serde(skip)]
pub music_handle: Handle<AudioSource>,
Expand All @@ -88,6 +90,14 @@ pub struct FighterMeta {
pub audio: AudioMeta,
}

#[derive(TypeUuid, Deserialize, Clone, Debug, Component)]
#[serde(deny_unknown_fields)]
#[uuid = "5e2db270-ec2e-013a-92a8-2cf05d71216b"]
pub struct ItemMeta {
pub name: String,
pub image: ImageMeta,
}

#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct FighterHudMeta {
Expand Down Expand Up @@ -124,6 +134,16 @@ pub struct FighterSpawnMeta {
pub location: Vec3,
}

#[derive(TypeUuid, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
#[uuid = "f5092550-ec30-013a-92a9-2cf05d71216b"]
pub struct ItemSpawnMeta {
pub item: String,
#[serde(skip)]
pub item_handle: Handle<ItemMeta>,
pub location: Vec3,
}

#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct ParallaxMeta {
Expand Down

0 comments on commit ae35d77

Please sign in to comment.