player.rs 5.8 KB

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