|
| 1 | +use super::Game; |
| 2 | +use super::GameState; |
| 3 | +use ggez::event::{EventHandler, KeyCode}; |
| 4 | +use ggez::graphics::{self, Color, Text}; |
| 5 | +use ggez::input::keyboard::KeyMods; |
| 6 | +use ggez::timer; |
| 7 | +use ggez::GameError; |
| 8 | +use ggez::{Context, GameResult}; |
| 9 | + |
| 10 | +impl EventHandler<GameError> for GameState { |
| 11 | + fn update(&mut self, ctx: &mut Context) -> GameResult<()> { |
| 12 | + match self.state { |
| 13 | + Game::Playing => { |
| 14 | + self.bird_velocity += 0.5; |
| 15 | + self.bird_pos[1] += self.bird_velocity; |
| 16 | + |
| 17 | + if self.check_collision(ctx) { |
| 18 | + self.game_over(); |
| 19 | + } |
| 20 | + |
| 21 | + self.obstacle_spawn_timer += timer::delta(ctx).as_secs_f32(); |
| 22 | + if self.obstacle_spawn_timer > 1.5 { |
| 23 | + self.obstacle_spawn_timer = 0.0; |
| 24 | + self.add_obstacle(ctx); |
| 25 | + } |
| 26 | + |
| 27 | + for obstacle in &mut self.obstacles { |
| 28 | + obstacle.top.x -= 2.0; |
| 29 | + obstacle.bottom.x -= 2.0; |
| 30 | + } |
| 31 | + |
| 32 | + self.obstacles |
| 33 | + .retain(|obstacle| obstacle.top.x + obstacle.top.w > 0.0); |
| 34 | + |
| 35 | + self.score(); |
| 36 | + } |
| 37 | + _ => {} |
| 38 | + } |
| 39 | + |
| 40 | + Ok(()) |
| 41 | + } |
| 42 | + |
| 43 | + fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { |
| 44 | + graphics::clear(ctx, Color::WHITE); |
| 45 | + |
| 46 | + let score_text = Text::new(format!("Score: {}", self.score)); |
| 47 | + let score_pos = [20.0, 20.0]; |
| 48 | + graphics::draw(ctx, &score_text, (score_pos, 0.0, Color::BLACK))?; |
| 49 | + |
| 50 | + let bird_scale = graphics::DrawParam::default() |
| 51 | + .scale([0.15, 0.15]) |
| 52 | + .dest([self.bird_pos[0], self.bird_pos[1]]); |
| 53 | + graphics::draw(ctx, &self.bird_image, bird_scale)?; |
| 54 | + |
| 55 | + for obstacle in &self.obstacles { |
| 56 | + let top_rect = graphics::Mesh::new_rectangle( |
| 57 | + ctx, |
| 58 | + graphics::DrawMode::fill(), |
| 59 | + obstacle.top, |
| 60 | + Color::GREEN, |
| 61 | + )?; |
| 62 | + graphics::draw(ctx, &top_rect, graphics::DrawParam::default())?; |
| 63 | + |
| 64 | + let bottom_rect = graphics::Mesh::new_rectangle( |
| 65 | + ctx, |
| 66 | + graphics::DrawMode::fill(), |
| 67 | + obstacle.bottom, |
| 68 | + Color::GREEN, |
| 69 | + )?; |
| 70 | + graphics::draw(ctx, &bottom_rect, graphics::DrawParam::default())?; |
| 71 | + } |
| 72 | + |
| 73 | + if self.state == Game::Menu { |
| 74 | + let start_text = Text::new("Press Enter to start"); |
| 75 | + let start_pos = [320.0, 300.0]; |
| 76 | + graphics::draw(ctx, &start_text, (start_pos, 0.0, Color::BLACK))?; |
| 77 | + } |
| 78 | + |
| 79 | + if self.state == Game::GameOver { |
| 80 | + let start_text = Text::new("Press R to restart"); |
| 81 | + let start_pos = [320.0, 300.0]; |
| 82 | + graphics::draw(ctx, &start_text, (start_pos, 0.0, Color::BLACK))?; |
| 83 | + } |
| 84 | + |
| 85 | + graphics::present(ctx)?; |
| 86 | + Ok(()) |
| 87 | + } |
| 88 | + |
| 89 | + fn key_down_event( |
| 90 | + &mut self, |
| 91 | + _ctx: &mut Context, |
| 92 | + keycode: KeyCode, |
| 93 | + _keymods: KeyMods, |
| 94 | + _repeat: bool, |
| 95 | + ) { |
| 96 | + match self.state { |
| 97 | + Game::Menu => { |
| 98 | + if keycode == KeyCode::Return { |
| 99 | + self.state = Game::Playing; |
| 100 | + } |
| 101 | + } |
| 102 | + Game::Playing => { |
| 103 | + if keycode == KeyCode::Space { |
| 104 | + self.bird_velocity = -06.0; |
| 105 | + } |
| 106 | + } |
| 107 | + Game::GameOver => { |
| 108 | + if keycode == KeyCode::R { |
| 109 | + self.restart(_ctx).expect("Falha ao reiniciar o jogo") |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments