main.rs 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. use std::cmp;
  2. use std::collections::HashMap;
  3. use std::env;
  4. use std::path;
  5. use ggez;
  6. use ggez::{event, input};
  7. use ggez::{Context, GameResult};
  8. use ggez::event::{KeyCode, MouseButton};
  9. use ggez::graphics;
  10. use ggez::graphics::{Color, DrawMode, FillOptions, MeshBuilder, StrokeOptions};
  11. use ggez::timer::check_update_time;
  12. use glam::Vec2;
  13. use behavior::ItemBehavior;
  14. use physics::{MetaEvent, PhysicEvent, util};
  15. use scene::item::{ItemState, SceneItem, SceneItemSpriteInfo, SceneItemType};
  16. use scene::main::MainState;
  17. use scene::SpriteType;
  18. use ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
  19. use ui::scene_item_menu::SceneItemMenuItem;
  20. use crate::physics::position::GridPosition;
  21. mod physics;
  22. mod ui;
  23. mod scene;
  24. mod behavior;
  25. // TODO: create a ScenePosition and a WindowPosition to be more explicit
  26. type Point2 = Vec2;
  27. type Vector2 = Vec2;
  28. const TARGET_FPS: u32 = 60; // execute update code 60x per seconds
  29. const META_EACH: u32 = 20; // execute meta code each 20 frames
  30. const PHYSICS_EACH: u32 = 10; // execute physics code each 10 frames
  31. const ANIMATE_EACH: u32 = 60; // execute animate code each 30 frames
  32. const SPRITE_EACH: u32 = 10; // change sprite animation tile 30 frames
  33. const MAX_FRAME_I: u32 = 4294967295; // max of frame_i used to calculate ticks
  34. const DISPLAY_OFFSET_BY: f32 = 3.0; // pixel offset by tick when player move screen display
  35. const DISPLAY_OFFSET_BY_SPEED: f32 = 10.0; // pixel offset by tick when player move screen display with speed
  36. const SCENE_ITEMS_SPRITE_SHEET_WIDTH: f32 = 800.0; // Width of sprite sheet
  37. const SCENE_ITEMS_SPRITE_SHEET_HEIGHT: f32 = 600.0; // Height of sprite sheet
  38. const UI_SPRITE_SHEET_WIDTH: f32 = 800.0; // Width of sprite sheet
  39. const UI_SPRITE_SHEET_HEIGHT: f32 = 600.0; // Height of sprite sheet
  40. const GRID_TILE_WIDTH: f32 = 5.0; // Width of one grid tile
  41. const GRID_TILE_HEIGHT: f32 = 5.0; // Height of one grid tile
  42. const DEFAULT_SELECTED_SQUARE_SIDE: f32 = 14.0;
  43. const DEFAULT_SELECTED_SQUARE_SIDE_HALF: f32 = DEFAULT_SELECTED_SQUARE_SIDE / 2.0;
  44. const SCENE_ITEMS_CHANGE_ERR_MSG: &str = "scene_items content change !";
  45. pub fn main() -> GameResult {
  46. let resource_dir = if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
  47. let mut path = path::PathBuf::from(manifest_dir);
  48. path.push("resources");
  49. path
  50. } else {
  51. path::PathBuf::from("./resources")
  52. };
  53. let cb = ggez::ContextBuilder::new("oc", "bux")
  54. .add_resource_path(resource_dir)
  55. .window_mode(ggez::conf::WindowMode::default().dimensions(800.0, 600.0));
  56. let (mut ctx, event_loop) = cb.build()?;
  57. let state = MainState::new(&mut ctx)?;
  58. event::run(ctx, event_loop, state)
  59. }