Skip to content

Instantly share code, notes, and snippets.

@vessd
Last active March 27, 2019 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vessd/2e38a0dc9c38705958ca807886fb00d8 to your computer and use it in GitHub Desktop.
Save vessd/2e38a0dc9c38705958ca807886fb00d8 to your computer and use it in GitHub Desktop.
use amethyst::{
assets::{
AssetPrefab, Handle, Prefab, PrefabData, PrefabLoader, PrefabLoaderSystem, ProgressCounter,
RonFormat,
},
config::Config,
core::ecs::prelude::*,
core::transform::{Transform, TransformBundle},
derive::PrefabData,
error::Error,
prelude::{Builder, World},
renderer::{
Camera, DisplayConfig, DrawFlat2D, Pipeline, Projection, RenderBundle, Stage, Texture,
TextureFormat,
},
utils::application_root_dir,
Application, GameData, GameDataBuilder, SimpleState, StateData,
};
use rand::Rng;
use serde::{Deserialize, Serialize};
#[derive(Clone, Deserialize, Serialize, PrefabData)]
struct EnemyPrefabData {
texture: AssetPrefab<Texture, TextureFormat>,
}
struct Example;
impl SimpleState for Example {
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let StateData { world, .. } = data;
initialise_camera(world);
let handle = world.exec(|loader: PrefabLoader<EnemyPrefabData>| {
loader.load("prefab.ron", RonFormat, (), ())
});
world.write_resource::<EnemyPrefabDataSet>().0.push(handle);
}
}
fn initialise_camera(world: &mut World) {
let mut transform = Transform::default();
transform.set_translation_z(1.0);
world
.create_entity()
.with(transform)
.with(Camera::from(Projection::orthographic(
0.0, 800.0, 0.0, 600.0,
)))
.build();
}
#[derive(Default)]
struct EnemyPrefabDataSet(pub Vec<Handle<Prefab<EnemyPrefabData>>>);
struct EnemySpawnerSystem;
impl<'s> System<'s> for EnemySpawnerSystem {
type SystemData = (
WriteStorage<'s, Transform>,
Entities<'s>,
WriteStorage<'s, Handle<Prefab<EnemyPrefabData>>>,
Read<'s, EnemyPrefabDataSet>,
);
fn run(
&mut self,
(mut transforms, entities, mut prefab_handles, enemy_prefab_set): Self::SystemData,
) {
let enemy_prefab = enemy_prefab_set.0[0].clone();
// Set new entity position
let mut transform = Transform::default();
let x = rand::thread_rng().gen_range(0.0, 800.0);
let y = rand::thread_rng().gen_range(0.0, 600.0);
transform.set_translation_xyz(x, y, 0.0);
entities
.build_entity()
.with(enemy_prefab, &mut prefab_handles)
.with(transform, &mut transforms)
.build();
}
}
fn main() -> amethyst::Result<()> {
amethyst::start_logger(Default::default());
let app_root = application_root_dir()?;
let display_conf_path = app_root.join("display_config.ron");
let display_config = DisplayConfig::load(display_conf_path);
let pipe = Pipeline::build().with_stage(
Stage::with_backbuffer()
.clear_target([0.1, 0.1, 0.1, 1.0], 1.0)
.with_pass(DrawFlat2D::new()),
);
let game_data = GameDataBuilder::default()
.with(PrefabLoaderSystem::<EnemyPrefabData>::default(), "", &[])
.with(EnemySpawnerSystem, "", &[])
.with_bundle(TransformBundle::new())?
.with_bundle(RenderBundle::new(pipe, Some(display_config)).with_sprite_sheet_processor())?;
let mut game = Application::new("./", Example, game_data)?;
game.run();
Ok(())
}
#![enable(implicit_some)]
Prefab(
entities: [
(
data: (
texture: File(
"square.png",
Png,
(channel: Srgb),
),
),
),
],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment