Guest User

Untitled

a guest
May 24th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. #![allow(unused)]
  2. #![allow(dead_code)]
  3. extern crate amethyst;
  4.  
  5. use amethyst::assets::Loader;
  6. use amethyst::config::Config;
  7. use amethyst::controls::{ArcBallControlTag, ArcBallMovementSystem, FlyControlTag,
  8. FreeRotationSystem, MouseCenterLockSystem, MouseFocusUpdateSystem};
  9.  
  10. use amethyst::core::cgmath::{Deg, Vector3};
  11. use amethyst::core::frame_limiter::FrameRateLimitStrategy;
  12. use amethyst::core::transform::{GlobalTransform, Transform, TransformBundle};
  13. use amethyst::ecs::prelude::World;
  14. use amethyst::input::InputBundle;
  15. use amethyst::renderer::{AmbientColor, Camera, DisplayConfig, DrawShaded, ElementState, Event,
  16. KeyboardInput, Material, MaterialDefaults, MeshHandle, ObjFormat,
  17. Pipeline, PosNormTex, Projection, RenderBundle, Rgba, Stage,
  18. VirtualKeyCode, WindowEvent};
  19. use amethyst::{Application, Error, GameData, GameDataBuilder, State, StateData, Trans};
  20. use amethyst::core::bundle::{SystemBundle};
  21. use amethyst::core::bundle::Result as res_a;
  22. use amethyst::ecs::prelude::{DispatcherBuilder};
  23. use amethyst::ecs::prelude::Entity;
  24.  
  25. mod components;
  26. pub struct GameBundle;
  27. use components::player::PlayerSys;
  28.  
  29. impl <'a, 'b> SystemBundle<'a,'b> for GameBundle {
  30. fn build(self,builder: &mut DispatcherBuilder<'a,'b>) -> res_a<()> {
  31. builder.add(PlayerSys,"player_system", &["input_system"]);
  32. Ok(())
  33. }
  34.  
  35. }
  36. struct Game;
  37.  
  38. impl<'a, 'b> State<GameData<'a, 'b>> for Game {
  39. fn on_start(&mut self, data: StateData<GameData>) {
  40. let StateData { world, .. } = data;
  41. world.register::<components::player::Player>();
  42.  
  43. components::player::initialise_player(world);
  44.  
  45.  
  46. world.add_resource(AmbientColor(Rgba::from([0.1; 3])));
  47. }
  48.  
  49. fn handle_event(&mut self, _: StateData<GameData>, event: Event) -> Trans<GameData<'a, 'b>> {
  50. match event {
  51. Event::WindowEvent { event, .. } => match event {
  52. WindowEvent::KeyboardInput {
  53. input:
  54. KeyboardInput {
  55. virtual_keycode,
  56. state: ElementState::Pressed,
  57. ..
  58. },
  59. ..
  60. } => match virtual_keycode {
  61. Some(VirtualKeyCode::Escape) => return Trans::Quit,
  62. _ => (),
  63. },
  64. _ => (),
  65. },
  66. _ => (),
  67. }
  68. Trans::None
  69. }
  70.  
  71. fn update(&mut self, data: StateData<GameData>) -> Trans<GameData<'a, 'b>> {
  72. data.data.update(&data.world);
  73. Trans::None
  74. }
  75. }
  76.  
  77. struct Assets {
  78. cube: MeshHandle,
  79. red: Material,
  80. }
  81.  
  82. fn load_assets(world: &World) -> Assets {
  83. let mesh_storage = world.read_resource();
  84. let tex_storage = world.read_resource();
  85. let mat_defaults = world.read_resource::<MaterialDefaults>();
  86. let loader = world.read_resource::<Loader>();
  87.  
  88. let red = loader.load_from_data([1.0, 0.0, 0.0, 1.0].into(), (), &tex_storage);
  89. let red = Material {
  90. albedo: red,
  91. ..mat_defaults.0.clone()
  92. };
  93.  
  94. let cube = loader.load("cube.obj", ObjFormat, (), (), &mesh_storage);
  95.  
  96. Assets { cube, red }
  97. }
  98.  
  99. fn main() {
  100. if let Err(error) = run() {
  101. eprintln!("Could not run game!");
  102. eprintln!("{}", error);
  103. ::std::process::exit(1);
  104. }
  105. }
  106.  
  107. /// Wrapper around the main, so we can return errors easily.
  108. fn run() -> Result<(), Error> {
  109. let resources_directory = format!("{}/assets", env!("CARGO_MANIFEST_DIR"));
  110.  
  111. let display_config_path = format!(
  112. "{}/resources/display_config.ron",
  113. env!("CARGO_MANIFEST_DIR")
  114. );
  115.  
  116. let display_config = DisplayConfig::load(display_config_path);
  117.  
  118. let key_bindings_path = format!(
  119. "{}/resources/input.ron",
  120. env!("CARGO_MANIFEST_DIR")
  121. );
  122.  
  123. let pipeline_builder = Pipeline::build().with_stage(
  124. Stage::with_backbuffer()
  125. .clear_target([0.1, 0.0, 1., 1.0], 1.0)
  126. .with_pass(DrawShaded::<PosNormTex>::new()),
  127. );
  128. let game_data = GameDataBuilder::default()
  129. .with_bundle(
  130. InputBundle::<String, String>::new().with_bindings_from_file(&key_bindings_path),
  131. )?
  132.  
  133. .with_bundle(GameBundle)?
  134. .with(MouseFocusUpdateSystem::new(),"mouse_focus", &[])
  135. .with(MouseCenterLockSystem, "mouse_lock", &["mouse_focus"])
  136. .with(ArcBallMovementSystem {}, "arc_ball_movement_system", &[])
  137. // This system manage the orientation of camera in accord to mouse input
  138. .with(
  139. FreeRotationSystem::<String, String>::new(1., 1.),
  140. "free_rotation_system",
  141. &[],
  142. )
  143. .with_bundle(RenderBundle::new(pipeline_builder, Some(display_config)))?;
  144. let mut game = Application::build(resources_directory, Game)?
  145. .with_frame_limit(FrameRateLimitStrategy::Unlimited, 0)
  146. .build(game_data)?;
  147. game.run();
  148. Ok(())
  149. }
Add Comment
Please, Sign In to add comment