-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f972cd
commit 8c646ac
Showing
7 changed files
with
103 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
( | ||
actors: { | ||
"lucy": Actor( | ||
name: "Lucy", | ||
texture_path: "image/lucy.png", | ||
texture_atlas_grid: TextureAtlasGrid( | ||
tile_size: UVec2(8, 8), | ||
columns: 5, | ||
rows: 1, | ||
), | ||
), | ||
}, | ||
player: "lucy", | ||
) |
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.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use bevy::ecs::system::EntityCommand; | ||
use bevy::ecs::system::SystemState; | ||
use bevy::prelude::*; | ||
use bevy::utils::HashMap; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
|
||
use crate::util::prelude::*; | ||
|
||
pub(super) fn plugin(app: &mut App) { | ||
app.configure::<ConfigHandle<ActorConfig>>(); | ||
} | ||
|
||
// TODO: Require all actor assets loaded before continuing to `Screen::Playing`. | ||
#[derive(Asset, Reflect, Serialize, Deserialize)] | ||
struct ActorConfig { | ||
actors: HashMap<String, Actor>, | ||
player: String, | ||
} | ||
|
||
impl Config for ActorConfig { | ||
const PATH: &'static str = "config/actor.ron"; | ||
|
||
const EXTENSION: &'static str = "actor.ron"; | ||
|
||
fn on_load(&mut self, world: &mut World) { | ||
let mut system_state = | ||
SystemState::<(Res<AssetServer>, ResMut<Assets<TextureAtlasLayout>>)>::new(world); | ||
let (asset_server, mut layouts) = system_state.get_mut(world); | ||
|
||
for actor in self.actors.values_mut() { | ||
actor.texture = asset_server.load(&actor.texture_path); | ||
actor.texture_atlas_layout = layouts.add(&actor.texture_atlas_grid); | ||
} | ||
} | ||
} | ||
|
||
#[derive(Reflect, Serialize, Deserialize)] | ||
struct Actor { | ||
name: String, | ||
texture_path: String, | ||
#[serde(skip)] | ||
texture: Handle<Image>, | ||
texture_atlas_grid: TextureAtlasGrid, | ||
#[serde(skip)] | ||
texture_atlas_layout: Handle<TextureAtlasLayout>, | ||
} | ||
|
||
fn actor_helper(mut entity: EntityWorldMut, key: Option<String>) { | ||
let config_handle = entity.world().resource::<ConfigHandle<ActorConfig>>(); | ||
let config = r!(entity | ||
.world() | ||
.resource::<Assets<ActorConfig>>() | ||
.get(&config_handle.0)); | ||
let actor = r!(config.actors.get(key.as_ref().unwrap_or(&config.player))); | ||
|
||
entity.insert(( | ||
Name::new(actor.name.clone()), | ||
SpriteBundle { | ||
texture: actor.texture.clone_weak(), | ||
..default() | ||
}, | ||
TextureAtlas { | ||
layout: actor.texture_atlas_layout.clone_weak(), | ||
index: 0, | ||
}, | ||
)); | ||
} | ||
|
||
pub fn actor(key: impl Into<String>) -> impl EntityCommand<World> { | ||
let key = key.into(); | ||
move |entity: EntityWorldMut| { | ||
actor_helper(entity, Some(key)); | ||
} | ||
} | ||
|
||
pub fn player(entity: EntityWorldMut) { | ||
actor_helper(entity, None); | ||
} |
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