|
1 | 1 | extern crate erlking;
|
2 | 2 |
|
3 |
| -use erlking::app::App; |
4 |
| -use winit::event_loop::EventLoop; |
| 3 | +use cgmath::{Deg, Quaternion, Rotation3, Vector3}; |
| 4 | +use erlking::{ |
| 5 | + app::App, camera::ParallaxCamera, ActiveCamera, Game, KeyboardInput, Position, Rotation, Scale, |
| 6 | + Sprite, |
| 7 | +}; |
| 8 | +use glam::Vec3; |
| 9 | +use hecs::World; |
| 10 | +use std::time::Duration; |
| 11 | +use winit::{ |
| 12 | + event::{ElementState, VirtualKeyCode}, |
| 13 | + event_loop::EventLoop, |
| 14 | +}; |
| 15 | + |
| 16 | +#[derive(Clone, Copy)] |
| 17 | +struct MoveSpeed(f32); |
5 | 18 |
|
6 | 19 | fn main() {
|
7 | 20 | let event_loop = EventLoop::new();
|
8 |
| - let app = futures::executor::block_on(App::new("erlking", &event_loop)); |
9 |
| - app.run(event_loop); |
| 21 | + let app = futures::executor::block_on(App::new("parallax-demo", &event_loop)); |
| 22 | + let mut parallax_demo = Game::new(); |
| 23 | + |
| 24 | + let movespeed = MoveSpeed(10.0); |
| 25 | + |
| 26 | + let player = ( |
| 27 | + Position(cgmath::Vector3::new(0.0, 0.0, 20.0)), |
| 28 | + Rotation(Quaternion::from_axis_angle( |
| 29 | + Vector3::new(0.0, 1.0, 0.0), |
| 30 | + Deg(0.0), |
| 31 | + )), |
| 32 | + Scale(1), |
| 33 | + KeyboardInput(None), |
| 34 | + Sprite("player".to_string()), |
| 35 | + movespeed, |
| 36 | + ); |
| 37 | + |
| 38 | + let camera = ( |
| 39 | + ParallaxCamera::new( |
| 40 | + Vec3::new(0.0, 3.0, 0.0), |
| 41 | + Vec3::new(0.0, 0.0, 1.0), |
| 42 | + 1.0, |
| 43 | + 0.1, |
| 44 | + 500.0, |
| 45 | + ), |
| 46 | + KeyboardInput(None), |
| 47 | + ActiveCamera, |
| 48 | + movespeed, |
| 49 | + ); |
| 50 | + |
| 51 | + let apple = ( |
| 52 | + Position(cgmath::Vector3::new(-2.0, 0.0, 30.0)), |
| 53 | + Rotation(Quaternion::from_axis_angle( |
| 54 | + Vector3::new(0.0, 1.0, 0.0), |
| 55 | + Deg(0.0), |
| 56 | + )), |
| 57 | + Scale(1), |
| 58 | + Sprite("apple".to_string()), |
| 59 | + ); |
| 60 | + |
| 61 | + let ashberry = ( |
| 62 | + Position(cgmath::Vector3::new(2.0, 0.0, 30.0)), |
| 63 | + Rotation(Quaternion::from_axis_angle( |
| 64 | + Vector3::new(0.0, 1.0, 0.0), |
| 65 | + Deg(0.0), |
| 66 | + )), |
| 67 | + Scale(1), |
| 68 | + Sprite("ashberry".to_string()), |
| 69 | + ); |
| 70 | + |
| 71 | + let baobab = ( |
| 72 | + Position(cgmath::Vector3::new(3.0, 0.0, 55.0)), |
| 73 | + Rotation(Quaternion::from_axis_angle( |
| 74 | + Vector3::new(0.0, 1.0, 0.0), |
| 75 | + Deg(0.0), |
| 76 | + )), |
| 77 | + Scale(1), |
| 78 | + Sprite("baobab".to_string()), |
| 79 | + ); |
| 80 | + |
| 81 | + let beech = ( |
| 82 | + Position(cgmath::Vector3::new(-3.5, 0.0, 95.0)), |
| 83 | + Rotation(Quaternion::from_axis_angle( |
| 84 | + Vector3::new(0.0, 1.0, 0.0), |
| 85 | + Deg(0.0), |
| 86 | + )), |
| 87 | + Scale(1), |
| 88 | + Sprite("beech".to_string()), |
| 89 | + ); |
| 90 | + parallax_demo.spawn_entity(player); |
| 91 | + parallax_demo.spawn_entity(apple); |
| 92 | + parallax_demo.spawn_entity(ashberry); |
| 93 | + parallax_demo.spawn_entity(baobab); |
| 94 | + parallax_demo.spawn_entity(beech); |
| 95 | + parallax_demo.spawn_entity(camera); |
| 96 | + |
| 97 | + parallax_demo.add_system(&move_player); |
| 98 | + |
| 99 | + app.run(event_loop, parallax_demo); |
| 100 | +} |
| 101 | + |
| 102 | +fn move_player(world: &World, dt: Duration) { |
| 103 | + let mut q = world.query::<(&KeyboardInput, &mut Position, &MoveSpeed)>(); |
| 104 | + |
| 105 | + for (_, (key, pos, speed)) in q.iter() { |
| 106 | + if let Some(input) = key.0 { |
| 107 | + let dx = Vector3::new(speed.0 * dt.as_secs_f32(), 0.0, 0.0); |
| 108 | + match input { |
| 109 | + winit::event::KeyboardInput { |
| 110 | + state: ElementState::Pressed, |
| 111 | + virtual_keycode: Some(VirtualKeyCode::Left), |
| 112 | + .. |
| 113 | + } => { |
| 114 | + pos.0 -= dx; |
| 115 | + } |
| 116 | + winit::event::KeyboardInput { |
| 117 | + state: ElementState::Pressed, |
| 118 | + virtual_keycode: Some(VirtualKeyCode::Right), |
| 119 | + .. |
| 120 | + } => { |
| 121 | + pos.0 += dx; |
| 122 | + } |
| 123 | + _ => (), |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + let mut q = world.query::<( |
| 128 | + &ActiveCamera, |
| 129 | + &mut ParallaxCamera, |
| 130 | + &KeyboardInput, |
| 131 | + &MoveSpeed, |
| 132 | + )>(); |
| 133 | + |
| 134 | + let (_, (_, cam, key, speed)) = q.iter().next().expect("active camera is preset"); |
| 135 | + if let Some(input) = key.0 { |
| 136 | + let dx = Vec3::new(speed.0 * dt.as_secs_f32(), 0.0, 0.0); |
| 137 | + match input { |
| 138 | + winit::event::KeyboardInput { |
| 139 | + state: ElementState::Pressed, |
| 140 | + virtual_keycode: Some(VirtualKeyCode::Left), |
| 141 | + .. |
| 142 | + } => { |
| 143 | + cam.eye -= dx; |
| 144 | + } |
| 145 | + winit::event::KeyboardInput { |
| 146 | + state: ElementState::Pressed, |
| 147 | + virtual_keycode: Some(VirtualKeyCode::Right), |
| 148 | + .. |
| 149 | + } => { |
| 150 | + cam.eye += dx; |
| 151 | + } |
| 152 | + _ => (), |
| 153 | + } |
| 154 | + } |
10 | 155 | }
|
0 commit comments