You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.0KB

  1. use log;
  2. use Config;
  3. use controller::{Controller, ControllerButton, ControllerAction};
  4. use game::Game;
  5. use geometry::Geometry;
  6. #[derive(PartialEq)]
  7. pub enum GameMode {
  8. Playing,
  9. Paused,
  10. }
  11. // harbour - a place of refuge.
  12. //
  13. pub struct Harbour {
  14. delta: f32,
  15. mode: GameMode,
  16. resume_from_paused: bool,
  17. controller: Controller,
  18. game: Game,
  19. geometry: Geometry,
  20. }
  21. impl Harbour {
  22. pub fn new(config: &Config) -> Harbour {
  23. Harbour {
  24. delta: 0.0,
  25. mode: GameMode::Playing,
  26. resume_from_paused: false,
  27. controller: Controller::new(),
  28. game: Game::new(config),
  29. geometry: Geometry::new(config),
  30. }
  31. }
  32. pub fn reconfigure(&mut self, config: &Config) {
  33. self.game.reconfigure(config);
  34. self.geometry.reconfigure(config);
  35. }
  36. pub fn init(&mut self, random: f32) {
  37. self.geometry.init();
  38. match self.game.init(random) {
  39. Ok(_) => (),
  40. Err(_) => log("harbour::init errored")
  41. }
  42. }
  43. pub fn resize_event(&mut self, width: i32, height: i32) {
  44. self.geometry.resize(width, height);
  45. }
  46. // return true if we need to call tick after receiving an input event
  47. // (only true when we resume playing after being paused)
  48. pub fn input(&mut self, button: ControllerButton, action: ControllerAction) -> bool {
  49. self.controller.input(button, action);
  50. if button == ControllerButton::Start && action == ControllerAction::Up {
  51. if self.mode == GameMode::Playing {
  52. log("Paused");
  53. self.mode = GameMode::Paused;
  54. } else {
  55. log("Playing");
  56. self.mode = GameMode::Playing;
  57. self.resume_from_paused = true;
  58. return true;
  59. }
  60. }
  61. false
  62. }
  63. pub fn tick(&mut self, delta: f32, random: f32) -> bool {
  64. // always followed by a render
  65. // returns true if tick should be called again
  66. //
  67. // prevent a crazy delta value if we're resuming from a paused state
  68. if self.resume_from_paused {
  69. self.resume_from_paused = false;
  70. self.delta = 0.0;
  71. } else {
  72. self.delta = delta;
  73. }
  74. match self.mode {
  75. GameMode::Playing => {
  76. match self.game.tick_playing(&self.controller, delta, random) {
  77. Ok(res) => res,
  78. Err(_) => false,
  79. }
  80. },
  81. GameMode::Paused => false,
  82. }
  83. }
  84. // use the current gamestate to update geometry
  85. pub fn update_geometry(&mut self) {
  86. self.game.update_geometry(&mut self.geometry);
  87. }
  88. pub fn geo_len(&self) -> usize {
  89. self.geometry.geo.len()
  90. }
  91. pub fn geo_ptr(&self) -> *const f32 {
  92. self.geometry.geo.as_ptr() as *const f32
  93. }
  94. pub fn crt_len(&self) -> usize {
  95. self.geometry.crt.len()
  96. }
  97. pub fn crt_ptr(&self) -> *const f32 {
  98. self.geometry.crt.as_ptr() as *const f32
  99. }
  100. }