Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(unused)]
- #![allow(dead_code)]
- extern crate amethyst;
- use amethyst::assets::Loader;
- use amethyst::config::Config;
- use amethyst::controls::{ArcBallControlTag, ArcBallMovementSystem, FlyControlTag,
- FreeRotationSystem, MouseCenterLockSystem, MouseFocusUpdateSystem};
- use amethyst::core::cgmath::{Deg, Vector3};
- use amethyst::core::frame_limiter::FrameRateLimitStrategy;
- use amethyst::core::transform::{GlobalTransform, Transform, TransformBundle};
- use amethyst::ecs::prelude::World;
- use amethyst::input::InputBundle;
- use amethyst::renderer::{AmbientColor, Camera, DisplayConfig, DrawShaded, ElementState, Event,
- KeyboardInput, Material, MaterialDefaults, MeshHandle, ObjFormat,
- Pipeline, PosNormTex, Projection, RenderBundle, Rgba, Stage,
- VirtualKeyCode, WindowEvent};
- use amethyst::{Application, Error, GameData, GameDataBuilder, State, StateData, Trans};
- use amethyst::core::bundle::{SystemBundle};
- use amethyst::core::bundle::Result as res_a;
- use amethyst::ecs::prelude::{DispatcherBuilder};
- use amethyst::ecs::prelude::Entity;
- mod components;
- pub struct GameBundle;
- use components::player::PlayerSys;
- impl <'a, 'b> SystemBundle<'a,'b> for GameBundle {
- fn build(self,builder: &mut DispatcherBuilder<'a,'b>) -> res_a<()> {
- builder.add(PlayerSys,"player_system", &["input_system"]);
- Ok(())
- }
- }
- struct Game;
- impl<'a, 'b> State<GameData<'a, 'b>> for Game {
- fn on_start(&mut self, data: StateData<GameData>) {
- let StateData { world, .. } = data;
- world.register::<components::player::Player>();
- components::player::initialise_player(world);
- world.add_resource(AmbientColor(Rgba::from([0.1; 3])));
- }
- fn handle_event(&mut self, _: StateData<GameData>, event: Event) -> Trans<GameData<'a, 'b>> {
- match event {
- Event::WindowEvent { event, .. } => match event {
- WindowEvent::KeyboardInput {
- input:
- KeyboardInput {
- virtual_keycode,
- state: ElementState::Pressed,
- ..
- },
- ..
- } => match virtual_keycode {
- Some(VirtualKeyCode::Escape) => return Trans::Quit,
- _ => (),
- },
- _ => (),
- },
- _ => (),
- }
- Trans::None
- }
- fn update(&mut self, data: StateData<GameData>) -> Trans<GameData<'a, 'b>> {
- data.data.update(&data.world);
- Trans::None
- }
- }
- struct Assets {
- cube: MeshHandle,
- red: Material,
- }
- fn load_assets(world: &World) -> Assets {
- let mesh_storage = world.read_resource();
- let tex_storage = world.read_resource();
- let mat_defaults = world.read_resource::<MaterialDefaults>();
- let loader = world.read_resource::<Loader>();
- let red = loader.load_from_data([1.0, 0.0, 0.0, 1.0].into(), (), &tex_storage);
- let red = Material {
- albedo: red,
- ..mat_defaults.0.clone()
- };
- let cube = loader.load("cube.obj", ObjFormat, (), (), &mesh_storage);
- Assets { cube, red }
- }
- fn main() {
- if let Err(error) = run() {
- eprintln!("Could not run game!");
- eprintln!("{}", error);
- ::std::process::exit(1);
- }
- }
- /// Wrapper around the main, so we can return errors easily.
- fn run() -> Result<(), Error> {
- let resources_directory = format!("{}/assets", env!("CARGO_MANIFEST_DIR"));
- let display_config_path = format!(
- "{}/resources/display_config.ron",
- env!("CARGO_MANIFEST_DIR")
- );
- let display_config = DisplayConfig::load(display_config_path);
- let key_bindings_path = format!(
- "{}/resources/input.ron",
- env!("CARGO_MANIFEST_DIR")
- );
- let pipeline_builder = Pipeline::build().with_stage(
- Stage::with_backbuffer()
- .clear_target([0.1, 0.0, 1., 1.0], 1.0)
- .with_pass(DrawShaded::<PosNormTex>::new()),
- );
- let game_data = GameDataBuilder::default()
- .with_bundle(
- InputBundle::<String, String>::new().with_bindings_from_file(&key_bindings_path),
- )?
- .with_bundle(GameBundle)?
- .with(MouseFocusUpdateSystem::new(),"mouse_focus", &[])
- .with(MouseCenterLockSystem, "mouse_lock", &["mouse_focus"])
- .with(ArcBallMovementSystem {}, "arc_ball_movement_system", &[])
- // This system manage the orientation of camera in accord to mouse input
- .with(
- FreeRotationSystem::<String, String>::new(1., 1.),
- "free_rotation_system",
- &[],
- )
- .with_bundle(RenderBundle::new(pipeline_builder, Some(display_config)))?;
- let mut game = Application::build(resources_directory, Game)?
- .with_frame_limit(FrameRateLimitStrategy::Unlimited, 0)
- .build(game_data)?;
- game.run();
- Ok(())
- }
Add Comment
Please, Sign In to add comment