Print joystick power level when pressing start
[kaka/rust-sdl-test.git] / src / core / game.rs
CommitLineData
bf7b5671 1use AppState;
dbf33b0d 2use sdl2::joystick::PowerLevel;
bf7b5671
TW
3use common::Point2D;
4use core::controller::Controller;
5use core::controller::ControllerManager;
6use point;
b0566120 7use sdl2::event::Event;
bf7b5671 8use sdl2::rect::Rect;
b0566120
TW
9use sdl2::render::Canvas;
10use sdl2::video::Window;
bf7b5671
TW
11use sprites::SpriteManager;
12use std::cell::RefCell;
13use std::rc::Rc;
902b2b31 14use time::Duration;
b0566120
TW
15
16////////// GAMESTATE ///////////////////////////////////////////////////////////
17
18#[derive(Default)]
19pub struct GameState {
20 world: World,
21}
22
23impl GameState {
24 pub fn new() -> Self {
25 GameState {
26 world: World::new(),
27 }
28 }
29}
30
31impl AppState for GameState {
a82a4d23 32 fn enter(&mut self, ctrl_man: &ControllerManager) {
b0566120
TW
33 if let Some(ctrl) = ctrl_man.controllers.get(&0) {
34 self.world.add(Box::new(Character::new(ctrl.clone())));
35 }
36 }
37
38 fn leave(&mut self) {}
39
902b2b31 40 fn update(&mut self, dt: Duration) {
b0566120
TW
41 self.world.update(dt);
42 }
43
a82a4d23 44 fn render(&mut self, canvas: &mut Canvas<Window>, sprites: &SpriteManager) {
b0566120
TW
45 self.world.render(canvas, sprites);
46 }
47
48 fn handle_event(&mut self, _event: Event) {}
49}
50
51////////// WORLD ///////////////////////////////////////////////////////////////
52
53#[derive(Default)]
54pub struct World {
55 level: Level,
3583c453 56 objects: Objects,
b0566120
TW
57}
58
59impl World {
60 pub fn new() -> Self {
61 World {
62 level: Level {
63 gravity: point!(0.0, 0.1),
64 ground: 600.0,
65 },
66 ..Default::default()
67 }
68 }
69
902b2b31 70 pub fn update(&mut self, dt: Duration) {
3583c453
TW
71 let mut breeding_ground = vec!();
72
73 for i in (0..self.objects.len()).rev() {
74 if self.objects[i].update(&mut breeding_ground, &self.level, dt) == Dead {
75 self.objects.remove(i); // swap_remove is more efficient, but changes the order of the array
76 }
77 }
78
79 for o in breeding_ground {
80 self.add(o);
b0566120
TW
81 }
82 }
83
a82a4d23 84 pub fn render(&mut self, canvas: &mut Canvas<Window>, sprites: &SpriteManager) {
b0566120
TW
85 self.level.render(canvas, sprites);
86 for o in &mut self.objects {
87 o.render(canvas, sprites);
88 }
89 }
90
91 pub fn add(&mut self, object: Box<dyn Object>) {
92 self.objects.push(object);
93 }
94}
95
96////////// LEVEL ///////////////////////////////////////////////////////////////
97
98#[derive(Default)]
99pub struct Level {
100 gravity: Point2D<f64>,
101 ground: f64, // just to have something
102}
103
104impl Level {
a82a4d23 105 pub fn render(&mut self, canvas: &mut Canvas<Window>, _sprites: &SpriteManager) {
b0566120
TW
106 let w = canvas.viewport().width() as i32;
107 for i in 1..11 {
108 let y = (i * i - 1) as i32 + self.ground as i32;
109 canvas.set_draw_color((255 - i * 20, 255 - i * 20, 0));
110 canvas.draw_line((0, y), (w, y)).unwrap();
111 }
112 }
113}
114
115////////// OBJECT //////////////////////////////////////////////////////////////
116
3583c453
TW
117type Objects = Vec<Box<dyn Object>>;
118
b0566120 119pub trait Object {
902b2b31 120 fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Duration) -> ObjectState;
a82a4d23 121 fn render(&self, _canvas: &mut Canvas<Window>, _sprites: &SpriteManager) {}
b0566120
TW
122}
123
3583c453
TW
124#[derive(PartialEq)]
125pub enum ObjectState { Alive, Dead }
126use self::ObjectState::*;
127
128
b0566120
TW
129pub trait Physical {}
130pub trait Drawable {}
131
132////////// CHARACTER ///////////////////////////////////////////////////////////
133
134pub struct Character {
135 ctrl: Rc<RefCell<Controller>>,
136 pos: Point2D<f64>,
137 vel: Point2D<f64>,
138}
139
140impl Character {
141 pub fn new(ctrl: Rc<RefCell<Controller>>) -> Self {
142 Character {
143 ctrl,
144 pos: point!(100.0, 100.0),
145 vel: point!(0.0, 0.0),
146 }
147 }
148}
149
150impl Object for Character {
902b2b31 151 fn update(&mut self, objects: &mut Objects, lvl: &Level, dt: Duration) -> ObjectState {
b0566120 152 self.vel += lvl.gravity;
2836f506 153 self.pos += self.vel;
b0566120 154
bf7b5671 155 let ctrl = self.ctrl.borrow();
b0566120
TW
156
157 if self.pos.y >= lvl.ground {
158 self.pos.y = lvl.ground;
159 self.vel.y = 0.0;
160 self.vel.x *= 0.9;
161
bf7b5671
TW
162 if ctrl.jump.is_pressed {
163 self.vel = ctrl.aim.to_point() * 5.0;
3583c453
TW
164 }
165 }
166
bf7b5671 167 if ctrl.shoot.is_pressed {
3583c453
TW
168 use rand::distributions::{Distribution, Normal};
169 let normal = Normal::new(0.0, 0.1);
170 for _i in 0..100 {
171 objects.push(Box::new(Boll {
172 pos: self.pos,
bf7b5671 173 vel: ctrl.aim.to_adjusted_point() * (3.0 + rand::random::<f64>()) + point!(normal.sample(&mut rand::thread_rng()), normal.sample(&mut rand::thread_rng())) + self.vel,
3583c453
TW
174 bounces: 2,
175 }));
b0566120 176 }
902b2b31 177 ctrl.rumble(1.0, dt);
b0566120
TW
178 }
179
dbf33b0d
TW
180 if ctrl.start.is_pressed && !ctrl.start.was_pressed {
181 match ctrl.device.power_level() {
182 Ok(PowerLevel::Unknown) => { println!("power level unknown"); }
183 Ok(PowerLevel::Empty) => { println!("power level empty"); }
184 Ok(PowerLevel::Low) => { println!("power level low"); }
185 Ok(PowerLevel::Medium) => { println!("power level medium"); }
186 Ok(PowerLevel::Full) => { println!("power level full"); }
187 Ok(PowerLevel::Wired) => { println!("power level wired"); }
188 Err(_) => {}
189 };
190 }
191
bf7b5671 192 match ctrl.mov.x {
b0566120
TW
193 v if v < -0.9 => { self.vel.x -= 0.5 }
194 v if v > 0.9 => { self.vel.x += 0.5 }
195 _ => {}
196 }
3583c453
TW
197
198 Alive
b0566120
TW
199 }
200
a82a4d23 201 fn render(&self, canvas: &mut Canvas<Window>, sprites: &SpriteManager) {
b0566120
TW
202 let block = sprites.get("mario");
203 let size = 32;
3583c453 204 canvas.copy(block, None, Rect::new(self.pos.x as i32 - size as i32 / 2, self.pos.y as i32 - size as i32, size, size)).unwrap();
bf7b5671
TW
205
206 let ctrl = &self.ctrl.borrow();
207 let l = 300.0;
208 let pos = (self.pos.x as i32, self.pos.y as i32);
209 // axis values
210 let p = (self.pos + ctrl.aim.to_point() * l).to_i32().into();
3583c453 211 canvas.set_draw_color((0, 255, 0));
bf7b5671
TW
212 canvas.draw_line(pos, p).unwrap();
213 draw_cross(canvas, p);
214 // adjusted values
215 let p = (self.pos + ctrl.aim.to_adjusted_point() * l).to_i32().into();
216 canvas.set_draw_color((255, 0, 0));
217 canvas.draw_line(pos, p).unwrap();
218 draw_cross(canvas, p);
219 // circle values
220 let p = (self.pos + Point2D::from(ctrl.aim.a) * l).to_i32().into();
221 canvas.set_draw_color((0, 0, 255));
222 canvas.draw_line(pos, p).unwrap();
223 draw_cross(canvas, p);
3583c453
TW
224 }
225}
226
bf7b5671
TW
227fn draw_cross(canvas: &mut Canvas<Window>, p: (i32, i32)) {
228 canvas.draw_line((p.0 - 5, p.1), (p.0 + 5, p.1)).unwrap();
229 canvas.draw_line((p.0, p.1 - 5), (p.0, p.1 + 5)).unwrap();
230}
231
a82a4d23
TW
232////////// BOLL ////////////////////////////////////////////////////////////////
233
3583c453
TW
234pub struct Boll {
235 pos: Point2D<f64>,
236 vel: Point2D<f64>,
237 bounces: u8,
238}
239
240impl Object for Boll {
902b2b31 241 fn update(&mut self, _objects: &mut Objects, lvl: &Level, _dt: Duration) -> ObjectState {
3583c453
TW
242 self.vel += lvl.gravity;
243 self.pos += self.vel;
244
245 if self.pos.y >= lvl.ground {
246 if self.bounces == 0 {
247 return Dead
248 } else {
249 self.bounces -= 1;
250 self.pos.y = lvl.ground;
251 self.vel.y = -self.vel.y;
252 }
253 }
254
255 Alive
256 }
257
a82a4d23 258 fn render(&self, canvas: &mut Canvas<Window>, _sprites: &SpriteManager) {
3583c453
TW
259 let block = _sprites.get("block");
260 let size = 4 + self.bounces * 6;
261 canvas.copy(block, None, Rect::new(self.pos.x as i32 - size as i32 / 2, self.pos.y as i32 - size as i32 / 2, size as u32, size as u32)).unwrap();
262 // canvas.set_draw_color((0, self.bounces * 100, 255));
263 // canvas.draw_point((self.pos.x as i32, self.pos.y as i32)).unwrap();
b0566120
TW
264 }
265}