player.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. use rltk::{VirtualKeyCode, Rltk, Point};
  2. use specs::prelude::*;
  3. use super::{Position, Player, WantsToMelee,
  4. Map, State, Viewshed,
  5. RunState, CombatStats, WantsToPickupItem,
  6. GameLog, Item};
  7. use std::cmp::{min, max};
  8. fn get_item(ecs: &mut World) {
  9. let player_pos = ecs.fetch::<Point>();
  10. let player_entity = ecs.fetch::<Entity>();
  11. let entities = ecs.entities();
  12. let items = ecs.read_storage::<Item>();
  13. let positions = ecs.read_storage::<Position>();
  14. let mut gamelog = ecs.fetch_mut::<GameLog>();
  15. let mut target_item : Option<Entity> = None;
  16. for (item_entity, _item, position) in (&entities, &items, &positions).join() {
  17. if position.x == player_pos.x && position.y == player_pos.y {
  18. target_item = Some(item_entity);
  19. }
  20. }
  21. match target_item {
  22. None => gamelog.entries.push("There is nothing here to pick up.".to_string()),
  23. Some(item) => {
  24. let mut pickup = ecs.write_storage::<WantsToPickupItem>();
  25. pickup.insert(*player_entity, WantsToPickupItem{ collected_by: *player_entity, item }).expect("Unable to insert want to pickup");
  26. }
  27. }
  28. }
  29. pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
  30. let mut positions = ecs.write_storage::<Position>();
  31. let players = ecs.write_storage::<Player>();
  32. let mut viewsheds = ecs.write_storage::<Viewshed>();
  33. let combat_stats = ecs.read_storage::<CombatStats>();
  34. let map = ecs.fetch::<Map>();
  35. let entities = ecs.entities();
  36. let mut wants_to_melee = ecs.write_storage::<WantsToMelee>();
  37. for (entity, _player, pos, viewshed) in (&entities, &players, &mut positions, &mut viewsheds).join() {
  38. if pos.x + delta_x < 1 || pos.x + delta_x > map.width-1 || pos.y + delta_y < 1 || pos.y + delta_y > map.height-1 { return; }
  39. let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
  40. for potential_target in map.tile_content[destination_idx].iter() {
  41. let target = combat_stats.get(*potential_target);
  42. if let Some(_target) = target {
  43. wants_to_melee.insert(entity, WantsToMelee{ target: *potential_target }).expect("Add target failed");
  44. return;
  45. }
  46. }
  47. if !map.blocked[destination_idx] {
  48. pos.x = min(79 , max(0, pos.x + delta_x));
  49. pos.y = min(49, max(0, pos.y + delta_y));
  50. let mut ppos = ecs.write_resource::<Point>();
  51. ppos.x = pos.x;
  52. ppos.y = pos.y;
  53. viewshed.dirty = true;
  54. }
  55. }
  56. }
  57. pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
  58. // Player movement
  59. match ctx.key {
  60. None => { return RunState::AwaitingInput } // Nothing happened
  61. Some(key) => match key {
  62. VirtualKeyCode::Left |
  63. VirtualKeyCode::Numpad4 |
  64. VirtualKeyCode::A |
  65. VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs),
  66. VirtualKeyCode::Right |
  67. VirtualKeyCode::Numpad6 |
  68. VirtualKeyCode::D |
  69. VirtualKeyCode::L => try_move_player(1, 0, &mut gs.ecs),
  70. VirtualKeyCode::Up |
  71. VirtualKeyCode::Numpad8 |
  72. VirtualKeyCode::W |
  73. VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs),
  74. VirtualKeyCode::Down |
  75. VirtualKeyCode::Numpad2 |
  76. VirtualKeyCode::S |
  77. VirtualKeyCode::J => try_move_player(0, 1, &mut gs.ecs),
  78. // Diagonals
  79. VirtualKeyCode::Numpad9 |
  80. VirtualKeyCode::E |
  81. VirtualKeyCode::Y => try_move_player(1, -1, &mut gs.ecs),
  82. VirtualKeyCode::Numpad7 |
  83. VirtualKeyCode::Q |
  84. VirtualKeyCode::U => try_move_player(-1, -1, &mut gs.ecs),
  85. VirtualKeyCode::Numpad3 |
  86. VirtualKeyCode::C |
  87. VirtualKeyCode::N => try_move_player(1, 1, &mut gs.ecs),
  88. VirtualKeyCode::Numpad1 |
  89. VirtualKeyCode::Z |
  90. VirtualKeyCode::B => try_move_player(-1, 1, &mut gs.ecs),
  91. VirtualKeyCode::G => get_item(&mut gs.ecs),
  92. VirtualKeyCode::I => return RunState::ShowInventory,
  93. VirtualKeyCode::X => return RunState::ShowDropItem,
  94. // Save and Quit
  95. VirtualKeyCode::Escape => return RunState::SaveGame,
  96. _ => { return RunState::AwaitingInput }
  97. },
  98. }
  99. RunState::PlayerTurn
  100. }