player.rs 4.5 KB

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