fight monsters, collect artifacts
This commit is contained in:
parent
643f3b76f9
commit
8b21e179c2
|
@ -9,6 +9,7 @@ pub trait Artifact {
|
||||||
fn get_immutable_position(&self) -> &Position;
|
fn get_immutable_position(&self) -> &Position;
|
||||||
/// call to apply the effects of the artifact to the player
|
/// call to apply the effects of the artifact to the player
|
||||||
fn collect(&mut self, player: &mut Player);
|
fn collect(&mut self, player: &mut Player);
|
||||||
|
fn was_collected(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Chest {
|
pub struct Chest {
|
||||||
|
@ -36,6 +37,10 @@ impl Artifact for Chest {
|
||||||
player.retrieve_gold(self.gold);
|
player.retrieve_gold(self.gold);
|
||||||
self.gold = 0;
|
self.gold = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn was_collected(&self) -> bool {
|
||||||
|
self.gold == 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Potion {
|
pub struct Potion {
|
||||||
|
@ -62,4 +67,8 @@ impl Artifact for Potion {
|
||||||
player.change_life(self.health.try_into().unwrap());
|
player.change_life(self.health.try_into().unwrap());
|
||||||
self.health = 0;
|
self.health = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn was_collected(&self) -> bool {
|
||||||
|
self.health == 0
|
||||||
|
}
|
||||||
}
|
}
|
65
src/game.rs
65
src/game.rs
|
@ -1,3 +1,4 @@
|
||||||
|
use std::ops::Deref;
|
||||||
use crate::artifacts::Artifact;
|
use crate::artifacts::Artifact;
|
||||||
use crate::level::{Level, StructureElement};
|
use crate::level::{Level, StructureElement};
|
||||||
use crate::level_generator::LevelGenerator;
|
use crate::level_generator::LevelGenerator;
|
||||||
|
@ -72,22 +73,20 @@ impl Game {
|
||||||
let level = &mut self.levels[player_level];
|
let level = &mut self.levels[player_level];
|
||||||
level.get_element(new_x, new_y)
|
level.get_element(new_x, new_y)
|
||||||
}
|
}
|
||||||
pub fn move_player(&mut self, mut dx: i16, mut dy: i16) {
|
|
||||||
|
pub fn move_player(&mut self, mut dx: i16, mut dy: i16) -> (i16, i16) {
|
||||||
// verify the player can do the move
|
// verify the player can do the move
|
||||||
if !self.can_move(dx, dy) {
|
if !self.can_move(dx, dy) {
|
||||||
return;
|
return (-dx, -dy);
|
||||||
}
|
}
|
||||||
|
|
||||||
let player_pos = &self.player.get_position();
|
let player_pos = &self.player.get_position();
|
||||||
// // let new_x: i16 = player_pos.get_x() as i16 + dx;
|
|
||||||
// // let new_y: i16 = player_pos.get_y() as i16 + dy;
|
|
||||||
let mut player_level = player_pos.get_level();
|
let mut player_level = player_pos.get_level();
|
||||||
// // let level = &mut self.levels[player_level];
|
let (structure, _, _) = self.next_element(dx, dy);
|
||||||
let element = self.next_element(dx, dy);
|
|
||||||
//
|
// use stairs if walked onto one
|
||||||
// // use stairs if walked onto one
|
match structure {
|
||||||
match element {
|
Some(e) => {
|
||||||
(Some(e), None, None) => {
|
|
||||||
match e {
|
match e {
|
||||||
StructureElement::StairDown => {
|
StructureElement::StairDown => {
|
||||||
(dx, dy) = (0, 0);
|
(dx, dy) = (0, 0);
|
||||||
|
@ -104,21 +103,47 @@ impl Game {
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(_, _, _) => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
//
|
|
||||||
// // interact with monsters / artifacts
|
|
||||||
// match element {
|
|
||||||
// (_, _, Some(a)) => {
|
|
||||||
// // a.collect(&mut self.player);
|
|
||||||
// // level.remove_artifact(&Position::new(player_level, new_x as usize, new_y as usize)).expect("Could not collect artifact");
|
|
||||||
// }
|
|
||||||
// (_, _, _) => {}
|
|
||||||
// }
|
|
||||||
|
|
||||||
// update position and discover area on new position
|
// update position and discover area on new position
|
||||||
let (new_x, new_y) = self.get_mutable_player().get_position().change(dx, dy);
|
let (new_x, new_y) = self.get_mutable_player().get_position().change(dx, dy);
|
||||||
self.get_mutable_level(player_level).discover(&Position::new(player_level, new_x, new_y));
|
self.get_mutable_level(player_level).discover(&Position::new(player_level, new_x, new_y));
|
||||||
|
(-dx, -dy)
|
||||||
|
}
|
||||||
|
pub fn player_fights_monster(&mut self) -> bool {
|
||||||
|
let player_pos = &self.player.get_immutable_position();
|
||||||
|
let player_level = player_pos.get_level();
|
||||||
|
let level = &mut self.levels[player_level];
|
||||||
|
let (_, m, _) = level.get_element(player_pos.get_x() as i16, player_pos.get_y() as i16);
|
||||||
|
match m {
|
||||||
|
None => {}
|
||||||
|
Some(m) => {
|
||||||
|
// TODO fight the monster
|
||||||
|
self.player.change_life(-1);
|
||||||
|
m.decrease_life(1);
|
||||||
|
return m.is_dead();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
true
|
||||||
|
}
|
||||||
|
pub fn player_collects_artifact(&mut self) {
|
||||||
|
let player_pos = &self.player.get_immutable_position();
|
||||||
|
let player_level = player_pos.get_level();
|
||||||
|
let level = &mut self.levels[player_level];
|
||||||
|
let (_, _, a) = level.get_element(player_pos.get_x() as i16, player_pos.get_y() as i16);
|
||||||
|
match a {
|
||||||
|
None => {}
|
||||||
|
Some(a) => {
|
||||||
|
a.collect(&mut self.player);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn update_level(&mut self) {
|
||||||
|
let player_pos = &self.player.get_immutable_position();
|
||||||
|
let player_level = player_pos.get_level();
|
||||||
|
let level = &mut self.levels[player_level];
|
||||||
|
level.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
15
src/level.rs
15
src/level.rs
|
@ -172,6 +172,21 @@ impl Level {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self) {
|
||||||
|
for (index, a) in &mut self.artifacts.iter().enumerate() {
|
||||||
|
if a.was_collected() {
|
||||||
|
self.artifacts.remove(index);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (index, m) in &mut self.monsters.iter().enumerate() {
|
||||||
|
if m.is_dead() {
|
||||||
|
self.monsters.remove(index);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -274,7 +274,7 @@ impl LevelGenerator {
|
||||||
let t_x = left + room.offset_x + rng.gen_range(0..room.width);
|
let t_x = left + room.offset_x + rng.gen_range(0..room.width);
|
||||||
let t_y = top + room.offset_y + rng.gen_range(0..room.height);
|
let t_y = top + room.offset_y + rng.gen_range(0..room.height);
|
||||||
// TODO randomize enemies here
|
// TODO randomize enemies here
|
||||||
enemies.push(Monster::new_with_position(10, Position::new(self.level, t_x, t_y)));
|
enemies.push(Monster::new_with_position(2, Position::new(self.level, t_x, t_y)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if room.kind == RoomType::End || room.kind == RoomType::StairDown {
|
if room.kind == RoomType::End || room.kind == RoomType::StairDown {
|
||||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -27,6 +27,7 @@ mod level_widget;
|
||||||
mod level_generator;
|
mod level_generator;
|
||||||
mod artifacts;
|
mod artifacts;
|
||||||
mod monster;
|
mod monster;
|
||||||
|
|
||||||
//
|
//
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut game = Game::new(Player::new(realname().as_str(), 10));
|
let mut game = Game::new(Player::new(realname().as_str(), 10));
|
||||||
|
@ -81,17 +82,20 @@ fn main() -> Result<()> {
|
||||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Left {
|
if key.kind == KeyEventKind::Press {
|
||||||
game.move_player(-1, 0);
|
let new_pos = match key.code {
|
||||||
|
KeyCode::Left => { game.move_player(-1, 0) }
|
||||||
|
KeyCode::Right => { game.move_player(1, 0) }
|
||||||
|
KeyCode::Up => { game.move_player(0, -1) }
|
||||||
|
KeyCode::Down => { game.move_player(0, 1) }
|
||||||
|
_ => { (0, 0) }
|
||||||
|
};
|
||||||
|
if !game.player_fights_monster() {
|
||||||
|
// player attacked monster but did not kill it
|
||||||
|
game.move_player(new_pos.0, new_pos.1);
|
||||||
}
|
}
|
||||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Right {
|
game.player_collects_artifact();
|
||||||
game.move_player(1, 0);
|
game.update_level();
|
||||||
}
|
|
||||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Up {
|
|
||||||
game.move_player(0, -1);
|
|
||||||
}
|
|
||||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Down {
|
|
||||||
game.move_player(0, 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue