player.rs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. extern crate rltk;
  2. use rltk::{VirtualKeyCode, Rltk, Point};
  3. extern crate specs;
  4. use specs::prelude::*;
  5. use super::{Position, Player, Viewshed, State, Map, RunState, CombatStats, WantsToMelee, Item,
  6. gamelog::GameLog, WantsToPickupItem, TileType, Monster, HungerClock, HungerState};
  7. pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
  8. let mut positions = ecs.write_storage::<Position>();
  9. let players = ecs.read_storage::<Player>();
  10. let mut viewsheds = ecs.write_storage::<Viewshed>();
  11. let entities = ecs.entities();
  12. let combat_stats = ecs.read_storage::<CombatStats>();
  13. let map = ecs.fetch::<Map>();
  14. let mut wants_to_melee = ecs.write_storage::<WantsToMelee>();
  15. for (entity, _player, pos, viewshed) in (&entities, &players, &mut positions, &mut viewsheds).join() {
  16. let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
  17. for potential_target in map.tile_content[destination_idx].iter() {
  18. let target = combat_stats.get(*potential_target);
  19. if let Some(_target) = target {
  20. wants_to_melee.insert(entity, WantsToMelee{ target: *potential_target }).expect("Add target failed");
  21. return;
  22. }
  23. }
  24. if !map.blocked[destination_idx] {
  25. pos.x += delta_x;
  26. pos.y += delta_y;
  27. if pos.x < 0 { pos.x = 0; }
  28. if pos.x > 79 { pos.y = 79; }
  29. if pos.y < 0 { pos.y = 0; }
  30. if pos.y > 49 { pos.y = 49; }
  31. viewshed.dirty = true;
  32. let mut ppos = ecs.write_resource::<Point>();
  33. ppos.x = pos.x;
  34. ppos.y = pos.y;
  35. }
  36. }
  37. }
  38. pub fn try_next_level(ecs: &mut World) -> bool {
  39. let player_pos = ecs.fetch::<Point>();
  40. let map = ecs.fetch::<Map>();
  41. let player_idx = map.xy_idx(player_pos.x, player_pos.y);
  42. if map.tiles[player_idx] == TileType::DownStairs {
  43. true
  44. } else {
  45. let mut gamelog = ecs.fetch_mut::<GameLog>();
  46. gamelog.entries.insert(0, "There is no way down from here.".to_string());
  47. false
  48. }
  49. }
  50. fn get_item(ecs: &mut World) {
  51. let player_pos = ecs.fetch::<Point>();
  52. let player_entity = ecs.fetch::<Entity>();
  53. let entities = ecs.entities();
  54. let items = ecs.read_storage::<Item>();
  55. let positions = ecs.read_storage::<Position>();
  56. let mut gamelog = ecs.fetch_mut::<GameLog>();
  57. let mut target_item : Option<Entity> = None;
  58. for (item_entity, _item, position) in (&entities, &items, &positions).join() {
  59. if position.x == player_pos.x && position.y == player_pos.y {
  60. target_item = Some(item_entity);
  61. }
  62. }
  63. match target_item {
  64. None => gamelog.entries.insert(0, "There is nothing here to pick up.".to_string()),
  65. Some(item) => {
  66. let mut pickup = ecs.write_storage::<WantsToPickupItem>();
  67. pickup.insert(*player_entity, WantsToPickupItem{ collected_by: *player_entity, item }).expect("Unable to insert want to pickup");
  68. }
  69. }
  70. }
  71. fn skip_turn(ecs: &mut World) -> RunState {
  72. let player_entity = ecs.fetch::<Entity>();
  73. let viewshed_components = ecs.read_storage::<Viewshed>();
  74. let monsters = ecs.read_storage::<Monster>();
  75. let worldmap_resource = ecs.fetch::<Map>();
  76. let mut can_heal = true;
  77. let viewshed = viewshed_components.get(*player_entity).unwrap();
  78. for tile in viewshed.visible_tiles.iter() {
  79. let idx = worldmap_resource.xy_idx(tile.x, tile.y);
  80. for entity_id in worldmap_resource.tile_content[idx].iter() {
  81. let mob = monsters.get(*entity_id);
  82. match mob {
  83. None => {}
  84. Some(_) => { can_heal = false; }
  85. }
  86. }
  87. }
  88. let hunger_clocks = ecs.read_storage::<HungerClock>();
  89. let hc = hunger_clocks.get(*player_entity);
  90. if let Some(hc) = hc {
  91. match hc.state {
  92. HungerState::Hungry => can_heal = false,
  93. HungerState::Starving => can_heal = false,
  94. _ => {}
  95. }
  96. }
  97. if can_heal {
  98. let mut health_components = ecs.write_storage::<CombatStats>();
  99. let player_hp = health_components.get_mut(*player_entity).unwrap();
  100. player_hp.hp = i32::min(player_hp.hp + 1, player_hp.max_hp);
  101. }
  102. RunState::PlayerTurn
  103. }
  104. pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
  105. // Player movement
  106. match ctx.key {
  107. None => { return RunState::AwaitingInput } // Nothing happened
  108. Some(key) => match key {
  109. VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs),
  110. VirtualKeyCode::Numpad4 => try_move_player(-1, 0, &mut gs.ecs),
  111. VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs),
  112. VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs),
  113. VirtualKeyCode::Numpad6 => try_move_player(1, 0, &mut gs.ecs),
  114. VirtualKeyCode::L => try_move_player(1, 0, &mut gs.ecs),
  115. VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs),
  116. VirtualKeyCode::Numpad8 => try_move_player(0, -1, &mut gs.ecs),
  117. VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs),
  118. VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs),
  119. VirtualKeyCode::Numpad2 => try_move_player(0, 1, &mut gs.ecs),
  120. VirtualKeyCode::J => try_move_player(0, 1, &mut gs.ecs),
  121. // Diagonals
  122. VirtualKeyCode::Numpad9 => try_move_player(1, -1, &mut gs.ecs),
  123. VirtualKeyCode::Y => try_move_player(1, -1, &mut gs.ecs),
  124. VirtualKeyCode::Numpad7 => try_move_player(-1, -1, &mut gs.ecs),
  125. VirtualKeyCode::U => try_move_player(-1, -1, &mut gs.ecs),
  126. VirtualKeyCode::Numpad3 => try_move_player(1, 1, &mut gs.ecs),
  127. VirtualKeyCode::N => try_move_player(1, 1, &mut gs.ecs),
  128. VirtualKeyCode::Numpad1 => try_move_player(-1, 1, &mut gs.ecs),
  129. VirtualKeyCode::B => try_move_player(-1, 1, &mut gs.ecs),
  130. // Skip Turn
  131. VirtualKeyCode::Numpad5 => return skip_turn(&mut gs.ecs),
  132. VirtualKeyCode::Space => return skip_turn(&mut gs.ecs),
  133. // Level changes
  134. VirtualKeyCode::Period => {
  135. if try_next_level(&mut gs.ecs) {
  136. return RunState::NextLevel;
  137. }
  138. }
  139. // Picking up items
  140. VirtualKeyCode::G => get_item(&mut gs.ecs),
  141. VirtualKeyCode::I => return RunState::ShowInventory,
  142. VirtualKeyCode::D => return RunState::ShowDropItem,
  143. VirtualKeyCode::R => return RunState::ShowRemoveItem,
  144. // Save and Quit
  145. VirtualKeyCode::Escape => return RunState::SaveGame,
  146. _ => { return RunState::AwaitingInput }
  147. },
  148. }
  149. RunState::PlayerTurn
  150. }