Created
August 3, 2016 22:35
-
-
Save LucioFranco/df98c1504bf68d700db04303078912e3 to your computer and use it in GitHub Desktop.
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
// components.rs | |
use amethyst::ecs::{Component, VecStorage}; | |
#[derive(Debug)] | |
pub struct Position { | |
pub x: f32, | |
pub y: f32, | |
pub z: f32, | |
} | |
impl Component for Position { | |
type Storage = VecStorage<Position>; | |
} | |
#[derive(Debug)] | |
pub struct Speed(pub f32); | |
impl Component for Speed { | |
type Storage = VecStorage<Speed>; | |
} | |
pub struct Sphere { | |
mesh: String, | |
ka: String, | |
kd: String | |
} | |
impl Sphere { | |
} |
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
// main.rs | |
extern crate amethyst; | |
extern crate cgmath; | |
use std::cell::RefCell; | |
use std::rc::Rc; | |
use amethyst::engine::{Application, State, Trans}; | |
use amethyst::context::Context; | |
use amethyst::config::Element; | |
use amethyst::ecs::{World, Entity}; | |
mod systems; | |
mod components; | |
struct Example; | |
impl State for Example { | |
fn handle_events(&mut self, events: Vec<Entity>, context: &mut Context, _: &mut World) -> Trans { | |
use amethyst::context::event::{EngineEvent, Event, VirtualKeyCode}; | |
let mut trans = Trans::None; | |
let storage = context.broadcaster.read::<EngineEvent>(); | |
for _event in events { | |
let event = storage.get(_event).unwrap(); | |
let event = &event.payload; | |
match *event { | |
Event::KeyboardInput(_, _, Some(VirtualKeyCode::Escape)) => trans = Trans::Quit, | |
Event::Closed => trans = Trans::Quit, | |
_ => (), | |
} | |
} | |
trans | |
} | |
fn on_start(&mut self, context: &mut Context, world: &mut World) { | |
use amethyst::renderer::pass::{Clear, DrawShaded}; | |
use amethyst::renderer::Layer; | |
use amethyst::renderer::Light; | |
use cgmath::Vector3; | |
world.register::<components::Position>(); | |
world.register::<components::Speed>(); | |
world | |
.create_now() | |
.with(components::Position { x: 0., y: 0., z: 0.}) | |
.with(components::Speed(0.2)) | |
.build(); | |
create_camera(context); | |
context.asset_manager.create_constant_texture("dark_blue", [0.0, 0.0, 0.01, 1.]); | |
context.asset_manager.create_constant_texture("green", [0.0, 1.0, 0.0, 1.]); | |
context.asset_manager.gen_sphere("sphere", 32, 32); | |
let translation = Vector3::new(0., 0., 0.); | |
let transform: [[f32; 4]; 4] = cgmath::Matrix4::from_translation(translation).into(); | |
let fragment = context.asset_manager.get_fragment("sphere", "dark_blue", "green", transform).unwrap(); | |
context.renderer.add_fragment("main", fragment); | |
let light = Light { | |
color: [1., 1., 1., 1.], | |
radius: 1., | |
center: [2., 2., 2.], | |
propagation_constant: 0., | |
propagation_linear: 0., | |
propagation_r_square: 1., | |
}; | |
context.renderer.add_light("main", light); | |
let clear_layer = | |
Layer::new("main", | |
vec![ | |
Clear::new([0., 0., 0., 1.]), | |
DrawShaded::new("main", "main"), | |
]); | |
let pipeline = vec![clear_layer]; | |
context.renderer.set_pipeline(pipeline); | |
} | |
fn update(&mut self, context: &mut Context, _: &mut World) -> Trans { | |
context.renderer.submit(); | |
Trans::None | |
} | |
} | |
fn main() { | |
use amethyst::context::Config; | |
let config = Config::from_file("resources/config.yml").unwrap(); | |
let mut game = Application::build(Example, config) | |
.with(systems::Render { frame_count: 0 }, "Render", 1000) | |
.done(); | |
game.run(); | |
} | |
fn create_camera(context: &mut Context) { | |
use amethyst::renderer::Camera; | |
let (w, h) = context.renderer.get_dimensions().unwrap(); | |
let proj = Camera::perspective(60.0, w as f32 / h as f32, 1.0, 100.0); | |
let eye = [0., 5., 0.]; | |
let target = [0., 0., 0.]; | |
let up = [0., 0., 1.]; | |
let view = Camera::look_at(eye, target, up); | |
let camera = Camera::new(proj, view); | |
context.renderer.add_scene("main"); | |
context.renderer.add_camera(camera, "main"); | |
} |
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
// systems.rs | |
use amethyst::ecs::{Processor, RunArg, Join}; | |
use ::components::{Position, Speed}; | |
use amethyst::context::timing::Duration; | |
use amethyst::context::Context; | |
use std::sync::{Arc, Mutex}; | |
pub struct Render { | |
pub frame_count: u32, | |
} | |
impl Processor<Arc<Mutex<Context>>> for Render { | |
fn run(&mut self, _: RunArg, context: Arc<Mutex<Context>>) { | |
let context = context.clone().lock().unwrap(); | |
self.frame_count += 1; | |
println!("Frames: {}", self.frame_count); | |
} | |
} | |
pub struct Transform; | |
impl Processor<Arc<Mutex<Context>>> for Transform { | |
fn run(&mut self, arg: RunArg, _: Arc<Mutex<Context>>) { | |
let (mut p, s) = arg.fetch(|w| (w.write::<Position>(), w.read::<Speed>())); | |
for (p, s) in (&mut p, &s).iter() { | |
p.x += s.0; | |
p.y += s.0; | |
p.z += s.0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment