player.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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};
  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. fn get_item(ecs: &mut World) {
  36. let player_pos = ecs.fetch::<Point>();
  37. let player_entity = ecs.fetch::<Entity>();
  38. let entities = ecs.entities();
  39. let items = ecs.read_storage::<Item>();
  40. let positions = ecs.read_storage::<Position>();
  41. let mut gamelog = ecs.fetch_mut::<GameLog>();
  42. let mut target_item : Option<Entity> = None;
  43. for (item_entity, _item, position) in (&entities, &items, &positions).join() {
  44. if position.x == player_pos.x && position.y == player_pos.y {
  45. target_item = Some(item_entity);
  46. }
  47. }
  48. match target_item {
  49. None => gamelog.entries.insert(0, "There is nothing here to pick up.".to_string()),
  50. Some(item) => {
  51. let mut pickup = ecs.write_storage::<WantsToPickupItem>();
  52. pickup.insert(*player_entity, WantsToPickupItem{ collected_by: *player_entity, item }).expect("Unable to insert want to pickup");
  53. }
  54. }
  55. }
  56. pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
  57. // Player movement
  58. match ctx.key {
  59. None => { return RunState::AwaitingInput } // Nothing happened
  60. Some(key) => match key {
  61. VirtualKeyCode::Left |
  62. VirtualKeyCode::Numpad4 |
  63. VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs),
  64. VirtualKeyCode::Right |
  65. VirtualKeyCode::Numpad6 |
  66. VirtualKeyCode::L => try_move_player(1, 0, &mut gs.ecs),
  67. VirtualKeyCode::Up |
  68. VirtualKeyCode::Numpad8 |
  69. VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs),
  70. VirtualKeyCode::Down |
  71. VirtualKeyCode::Numpad2 |
  72. VirtualKeyCode::J => try_move_player(0, 1, &mut gs.ecs),
  73. // Diagonals
  74. VirtualKeyCode::Numpad9 |
  75. VirtualKeyCode::U => try_move_player(1, -1, &mut gs.ecs),
  76. VirtualKeyCode::Numpad7 |
  77. VirtualKeyCode::Y => try_move_player(-1, -1, &mut gs.ecs),
  78. VirtualKeyCode::Numpad3 |
  79. VirtualKeyCode::N => try_move_player(1, 1, &mut gs.ecs),
  80. VirtualKeyCode::Numpad1 |
  81. VirtualKeyCode::B => try_move_player(-1, 1, &mut gs.ecs),
  82. // Picking up items
  83. VirtualKeyCode::G => get_item(&mut gs.ecs),
  84. VirtualKeyCode::I => return RunState::ShowInventory,
  85. VirtualKeyCode::D => return RunState::ShowDropItem,
  86. _ => { return RunState::AwaitingInput }
  87. },
  88. }
  89. RunState::PlayerTurn
  90. }