main.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. extern crate rltk;
  2. use rltk::{Console, GameState, Rltk, RGB, Point};
  3. extern crate specs;
  4. use specs::prelude::*;
  5. #[macro_use]
  6. extern crate specs_derive;
  7. mod components;
  8. pub use components::*;
  9. mod map;
  10. pub use map::*;
  11. mod player;
  12. use player::*;
  13. mod rect;
  14. pub use rect::Rect;
  15. mod visibility_system;
  16. use visibility_system::VisibilitySystem;
  17. mod monster_ai_system;
  18. use monster_ai_system::MonsterAI;
  19. mod map_indexing_system;
  20. use map_indexing_system::MapIndexingSystem;
  21. mod melee_combat_system;
  22. use melee_combat_system::MeleeCombatSystem;
  23. mod damage_system;
  24. use damage_system::DamageSystem;
  25. mod gui;
  26. mod gamelog;
  27. #[derive(PartialEq, Copy, Clone)]
  28. pub enum RunState { AwaitingInput, PreRun, PlayerTurn, MonsterTurn }
  29. pub struct State {
  30. pub ecs: World,
  31. pub systems: Dispatcher<'static, 'static>
  32. }
  33. impl GameState for State {
  34. fn tick(&mut self, ctx : &mut Rltk) {
  35. ctx.cls();
  36. let mut newrunstate;
  37. {
  38. let runstate = self.ecs.fetch::<RunState>();
  39. newrunstate = *runstate;
  40. }
  41. match newrunstate {
  42. RunState::PreRun => {
  43. self.systems.dispatch(&self.ecs);
  44. newrunstate = RunState::AwaitingInput;
  45. }
  46. RunState::AwaitingInput => {
  47. newrunstate = player_input(self, ctx);
  48. }
  49. RunState::PlayerTurn => {
  50. self.systems.dispatch(&self.ecs);
  51. newrunstate = RunState::MonsterTurn;
  52. }
  53. RunState::MonsterTurn => {
  54. self.systems.dispatch(&self.ecs);
  55. newrunstate = RunState::AwaitingInput;
  56. }
  57. }
  58. {
  59. let mut runwriter = self.ecs.write_resource::<RunState>();
  60. *runwriter = newrunstate;
  61. }
  62. damage_system::delete_the_dead(&mut self.ecs);
  63. draw_map(&self.ecs, ctx);
  64. let positions = self.ecs.read_storage::<Position>();
  65. let renderables = self.ecs.read_storage::<Renderable>();
  66. let map = self.ecs.fetch::<Map>();
  67. for (pos, render) in (&positions, &renderables).join() {
  68. let idx = map.xy_idx(pos.x, pos.y);
  69. if map.visible_tiles[idx] { ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph) }
  70. }
  71. gui::draw_ui(&self.ecs, ctx);
  72. }
  73. }
  74. fn main() {
  75. let mut context = Rltk::init_simple8x8(80, 50, "Hello Rust World", "../resources");
  76. context.with_post_scanlines(true);
  77. let mut gs = State {
  78. ecs: World::new(),
  79. systems : DispatcherBuilder::new()
  80. .with(MapIndexingSystem{}, "map_indexing_system", &[])
  81. .with(VisibilitySystem{}, "visibility_system", &[])
  82. .with(MonsterAI{}, "monster_ai", &["visibility_system", "map_indexing_system"])
  83. .with(MeleeCombatSystem{}, "melee_combat", &["monster_ai"])
  84. .with(DamageSystem{}, "damage", &["melee_combat"])
  85. .build(),
  86. };
  87. gs.ecs.register::<Position>();
  88. gs.ecs.register::<Renderable>();
  89. gs.ecs.register::<Player>();
  90. gs.ecs.register::<Viewshed>();
  91. gs.ecs.register::<Monster>();
  92. gs.ecs.register::<Name>();
  93. gs.ecs.register::<BlocksTile>();
  94. gs.ecs.register::<CombatStats>();
  95. gs.ecs.register::<WantsToMelee>();
  96. gs.ecs.register::<SufferDamage>();
  97. let map : Map = Map::new_map_rooms_and_corridors();
  98. let (player_x, player_y) = map.rooms[0].center();
  99. let player_entity = gs.ecs
  100. .create_entity()
  101. .with(Position { x: player_x, y: player_y })
  102. .with(Renderable {
  103. glyph: rltk::to_cp437('@'),
  104. fg: RGB::named(rltk::YELLOW),
  105. bg: RGB::named(rltk::BLACK),
  106. })
  107. .with(Player{})
  108. .with(Viewshed{ visible_tiles : Vec::new(), range: 8, dirty: true })
  109. .with(Name{name: "Player".to_string() })
  110. .with(CombatStats{ max_hp: 30, hp: 30, defense: 2, power: 5 })
  111. .build();
  112. let mut rng = rltk::RandomNumberGenerator::new();
  113. for (i,room) in map.rooms.iter().skip(1).enumerate() {
  114. let (x,y) = room.center();
  115. let glyph : u8;
  116. let name : String;
  117. let roll = rng.roll_dice(1, 2);
  118. match roll {
  119. 1 => { glyph = rltk::to_cp437('g'); name = "Goblin".to_string(); }
  120. _ => { glyph = rltk::to_cp437('o'); name = "Orc".to_string(); }
  121. }
  122. gs.ecs.create_entity()
  123. .with(Position{ x, y })
  124. .with(Renderable{
  125. glyph,
  126. fg: RGB::named(rltk::RED),
  127. bg: RGB::named(rltk::BLACK),
  128. })
  129. .with(Viewshed{ visible_tiles : Vec::new(), range: 8, dirty: true })
  130. .with(Monster{})
  131. .with(Name{ name: format!("{} #{}", &name, i) })
  132. .with(BlocksTile{})
  133. .with(CombatStats{ max_hp: 16, hp: 16, defense: 1, power: 4 })
  134. .build();
  135. }
  136. gs.ecs.insert(map);
  137. gs.ecs.insert(Point::new(player_x, player_y));
  138. gs.ecs.insert(player_entity);
  139. gs.ecs.insert(RunState::PreRun);
  140. gs.ecs.insert(gamelog::GameLog{ entries : vec!["Welcome to Rusty Roguelike".to_string()] });
  141. rltk::main_loop(context, gs);
  142. }