Skip to content

Commit

Permalink
Implement barebones actors
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Jul 21, 2024
1 parent 7f972cd commit 8c646ac
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 1 deletion.
14 changes: 14 additions & 0 deletions assets/config/actor.ron
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 added assets/image/lucy.aseprite
Binary file not shown.
Binary file added assets/image/lucy.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 removed assets/mockup/blobo.aseprite
Binary file not shown.
3 changes: 2 additions & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Game mechanics and content

pub mod actor;
mod step;

use bevy::prelude::*;
Expand All @@ -9,7 +10,7 @@ use crate::util::prelude::*;
pub(super) fn plugin(app: &mut App) {
app.configure::<GameRoot>();

app.add_plugins(step::plugin);
app.add_plugins((actor::plugin, step::plugin));
}

#[derive(Resource, Reflect)]
Expand Down
79 changes: 79 additions & 0 deletions src/game/actor.rs
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);
}
8 changes: 8 additions & 0 deletions src/screen/playing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::math::vec3;
use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
use leafwing_input_manager::common_conditions::action_just_pressed;
Expand All @@ -6,6 +7,7 @@ use pyri_state::prelude::*;
use pyri_state::schedule::ResolveStateSet;

use crate::core::camera::CameraRoot;
use crate::game::actor;
use crate::screen::fade_in;
use crate::screen::Screen;
use crate::ui::prelude::*;
Expand Down Expand Up @@ -33,6 +35,12 @@ impl Configure for PlayingAssets {

fn enter_playing(mut commands: Commands) {
commands.spawn_with(fade_in);
commands.spawn_with(actor::player);
commands
.spawn_with(actor::actor("lucy"))
.insert(TransformBundle::from_transform(
Transform::from_translation(vec3(10.0, 0.0, 0.0)),
));
}

fn exit_playing(
Expand Down

0 comments on commit 8c646ac

Please sign in to comment.