more tests
This commit is contained in:
parent
7b0eaf6640
commit
e47036bd8d
57
src/game.rs
57
src/game.rs
|
@ -1,25 +1,31 @@
|
||||||
use crate::level::Level;
|
use crate::level::Level;
|
||||||
|
use crate::level_generator::LevelGenerator;
|
||||||
use crate::player::Player;
|
use crate::player::Player;
|
||||||
use crate::position::Position;
|
use crate::position::Position;
|
||||||
|
|
||||||
pub const LEVELS: usize = 2;
|
pub const LEVELS: usize = 2;
|
||||||
|
|
||||||
pub struct Game<'game> {
|
pub struct Game {
|
||||||
pub(crate) player: &'game mut Player,
|
pub player: Player,
|
||||||
pub(crate) levels: Vec<Level>,
|
pub levels: Vec<Level>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game<'_> {
|
impl Game {
|
||||||
pub fn new(p: &mut Player) -> Game {
|
pub fn new(p: Player) -> Game {
|
||||||
let mut v: Vec<Level> = Vec::with_capacity(LEVELS);
|
let mut v: Vec<Level> = Vec::with_capacity(LEVELS);
|
||||||
for d in 0..LEVELS {
|
for d in 0..LEVELS {
|
||||||
let l = Level::new(d);
|
v.push(LevelGenerator::generate(d).render());
|
||||||
v.push(l);
|
|
||||||
}
|
}
|
||||||
Game {
|
let mut g = Game {
|
||||||
player: p,
|
player: p,
|
||||||
levels: v,
|
levels: v,
|
||||||
|
};
|
||||||
|
let mut start = (0,0);
|
||||||
|
{
|
||||||
|
start = g.get_level(0).start;
|
||||||
}
|
}
|
||||||
|
g.get_mutable_player().get_position().set(0,start.0, start.1);
|
||||||
|
g
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_player(&self) -> &Player {
|
pub fn get_player(&self) -> &Player {
|
||||||
|
@ -28,4 +34,39 @@ impl Game<'_> {
|
||||||
pub fn get_mutable_player(&mut self) -> &mut Player {
|
pub fn get_mutable_player(&mut self) -> &mut Player {
|
||||||
&mut self.player
|
&mut self.player
|
||||||
}
|
}
|
||||||
|
pub fn get_level(&self, level: usize) -> &Level { &self.levels[level] }
|
||||||
|
pub fn get_mutable_level(&mut self, level: usize) -> &mut Level { &mut self.levels[level] }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn game_has_correct_number_of_levels() {
|
||||||
|
let p = Player::new("foo", 42);
|
||||||
|
let g = Game::new(p);
|
||||||
|
assert_eq!(g.levels.len(), LEVELS);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn game_has_player() {
|
||||||
|
let p = Player::new("foo", 42);
|
||||||
|
let g = Game::new(p);
|
||||||
|
assert_eq!(g.get_player().get_name(), "foo");
|
||||||
|
assert_eq!(g.get_player().get_immutable_position().get_level(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn game_has_mutable_player() {
|
||||||
|
let p = Player::new("foo", 42);
|
||||||
|
let mut g = Game::new(p);
|
||||||
|
assert_eq!(g.get_player().get_name(), "foo");
|
||||||
|
g.get_mutable_player().change_life(-1);
|
||||||
|
assert_eq!(g.get_player().get_life(), 41);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn game_get_level() {
|
||||||
|
let p = Player::new("foo", 42);
|
||||||
|
let mut g = Game::new(p);
|
||||||
|
g.get_level(0);
|
||||||
|
assert_ne!(g.get_level(0).start, (0, 0));
|
||||||
|
g.get_mutable_level(0).discover(&Position::new(0, 1, 1));
|
||||||
}
|
}
|
|
@ -11,7 +11,8 @@ use rand::rngs::ThreadRng;
|
||||||
|
|
||||||
use crate::level::{Level, StructureElement};
|
use crate::level::{Level, StructureElement};
|
||||||
use crate::monster::Monster;
|
use crate::monster::Monster;
|
||||||
use crate::artifacts::Artifact;
|
use crate::artifacts::{Artifact, Chest};
|
||||||
|
use crate::position::Position;
|
||||||
|
|
||||||
const ROOMS_VERTICAL: usize = 7;
|
const ROOMS_VERTICAL: usize = 7;
|
||||||
const ROOMS_HORIZONTAL: usize = 4;
|
const ROOMS_HORIZONTAL: usize = 4;
|
||||||
|
@ -79,6 +80,7 @@ impl Room {
|
||||||
|
|
||||||
pub struct LevelGenerator {
|
pub struct LevelGenerator {
|
||||||
rooms: [[Room; ROOMS_HORIZONTAL]; ROOMS_VERTICAL],
|
rooms: [[Room; ROOMS_HORIZONTAL]; ROOMS_VERTICAL],
|
||||||
|
level: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LevelGenerator {
|
impl LevelGenerator {
|
||||||
|
@ -105,6 +107,7 @@ impl LevelGenerator {
|
||||||
room_types.push(RoomType::StairDown);
|
room_types.push(RoomType::StairDown);
|
||||||
}
|
}
|
||||||
room_types.push(RoomType::MonsterRoom);
|
room_types.push(RoomType::MonsterRoom);
|
||||||
|
room_types.push(RoomType::TreasureRoom);
|
||||||
// generate a random set of rooms and shuffle them
|
// generate a random set of rooms and shuffle them
|
||||||
for _ in room_types.len()..ROOMS_HORIZONTAL * ROOMS_VERTICAL {
|
for _ in room_types.len()..ROOMS_HORIZONTAL * ROOMS_VERTICAL {
|
||||||
match rng.gen_range(1..=100) {
|
match rng.gen_range(1..=100) {
|
||||||
|
@ -205,7 +208,8 @@ impl LevelGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
LevelGenerator {
|
LevelGenerator {
|
||||||
rooms
|
rooms,
|
||||||
|
level,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -260,13 +264,13 @@ impl LevelGenerator {
|
||||||
if room.kind == RoomType::TreasureRoom {
|
if room.kind == RoomType::TreasureRoom {
|
||||||
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);
|
||||||
// artifacts[t_x][t_y] = Some(Artifact::Chest { gold: rng.gen_range(2..30) });
|
artifacts.push(Box::new(Chest::new(Position::new(self.level, t_x, t_y))));
|
||||||
}
|
}
|
||||||
if room.kind == RoomType::MonsterRoom {
|
if room.kind == RoomType::MonsterRoom {
|
||||||
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[t_x][t_y] = Some(Monster::new(2));
|
enemies.push(Monster::new_with_position(10, Position::new(self.level, t_x, t_y)));
|
||||||
}
|
}
|
||||||
if room.kind == RoomType::StairDown {
|
if room.kind == RoomType::StairDown {
|
||||||
end_x = left + room.offset_x + rng.gen_range(0..room.width);
|
end_x = left + room.offset_x + rng.gen_range(0..room.width);
|
||||||
|
@ -320,7 +324,7 @@ impl LevelGenerator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Level {
|
Level {
|
||||||
level: 0,
|
level: self.level,
|
||||||
structure,
|
structure,
|
||||||
discovered: [[false; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH],
|
discovered: [[false; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH],
|
||||||
monsters: enemies,
|
monsters: enemies,
|
||||||
|
@ -331,9 +335,13 @@ impl LevelGenerator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[test]
|
#[test]
|
||||||
// fn test_level_gen() {
|
fn test_level_gen() {
|
||||||
// let level = LevelGenerator::generate(0);
|
let level = LevelGenerator::generate(23).render();
|
||||||
// println!("{:#?}", level);
|
assert_eq!(level.level, 23);
|
||||||
// assert_eq!(1, 2);
|
assert_ne!(level.start, (0, 0));
|
||||||
// }
|
assert_ne!(level.end, (0, 0));
|
||||||
|
assert_ne!(level.start, level.end);
|
||||||
|
assert_ne!(level.monsters.len(), 0);
|
||||||
|
assert_ne!(level.artifacts.len(), 0);
|
||||||
|
}
|
|
@ -9,7 +9,7 @@ const FG_BROWN: Color = Color::Rgb(186, 74, 0);
|
||||||
|
|
||||||
//⌂ α Ω
|
//⌂ α Ω
|
||||||
pub struct LevelWidget<'a> {
|
pub struct LevelWidget<'a> {
|
||||||
pub game: &'a Game<'a>,
|
pub game: &'a Game,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LevelWidget<'_> {
|
impl LevelWidget<'_> {
|
||||||
|
|
|
@ -36,7 +36,7 @@ fn main() -> Result<()> {
|
||||||
levels.push(LevelGenerator::generate(l).render());
|
levels.push(LevelGenerator::generate(l).render());
|
||||||
}
|
}
|
||||||
let mut g = Game {
|
let mut g = Game {
|
||||||
player: &mut Player::new(realname().as_str(), 10),
|
player: Player::new(realname().as_str(), 10),
|
||||||
levels,
|
levels,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,12 @@ impl Monster {
|
||||||
position: Position::new(0, 0, 0),
|
position: Position::new(0, 0, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn new_with_position(life: usize, position: Position) -> Self {
|
||||||
|
Monster {
|
||||||
|
life,
|
||||||
|
position,
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn get_life(&self) -> usize { self.life }
|
pub fn get_life(&self) -> usize { self.life }
|
||||||
pub fn is_dead(&self) -> bool { self.life <= 0 }
|
pub fn is_dead(&self) -> bool { self.life <= 0 }
|
||||||
pub fn decrease_life(&mut self, by: usize) {
|
pub fn decrease_life(&mut self, by: usize) {
|
||||||
|
|
|
@ -76,13 +76,13 @@ fn test_change_life() {
|
||||||
max_life: 10,
|
max_life: 10,
|
||||||
gold: 0,
|
gold: 0,
|
||||||
};
|
};
|
||||||
assert_eq!(p.life, 5);
|
assert_eq!(p.get_life(), 5);
|
||||||
p.change_life(-2);
|
p.change_life(-2);
|
||||||
assert_eq!(p.life, 3);
|
assert_eq!(p.get_life(), 3);
|
||||||
p.change_life(10);
|
p.change_life(10);
|
||||||
assert_eq!(p.life, 10);
|
assert_eq!(p.get_life(), 10);
|
||||||
p.change_life(-12);
|
p.change_life(-12);
|
||||||
assert_eq!(p.life, 0);
|
assert_eq!(p.get_life(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -96,4 +96,17 @@ fn player_can_move() {
|
||||||
p.get_position().set(1, 2, 3);
|
p.get_position().set(1, 2, 3);
|
||||||
p.get_position().change(2, 1);
|
p.get_position().change(2, 1);
|
||||||
assert_eq!(p.get_position(), &Position::new(1, 4, 4));
|
assert_eq!(p.get_position(), &Position::new(1, 4, 4));
|
||||||
|
assert_eq!(p.get_immutable_position(), &Position::new(1, 4, 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_max_life() {
|
||||||
|
let p = Player {
|
||||||
|
name: "Teddy Tester".to_string(),
|
||||||
|
position: Position::new(0, 1, 1),
|
||||||
|
life: 5,
|
||||||
|
max_life: 10,
|
||||||
|
gold: 0,
|
||||||
|
};
|
||||||
|
assert_eq!(p.get_max_life(), 10);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::cmp::max;
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
level: usize,
|
level: usize,
|
||||||
|
@ -13,9 +15,10 @@ impl Position {
|
||||||
y,
|
y,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn change(&mut self, dx: i16, dy: i16) {
|
pub fn change(&mut self, dx: i16, dy: i16) -> (usize, usize) {
|
||||||
self.x = ((self.x as i16) + dx) as usize;
|
self.x = max((self.x as i16) + dx, 0) as usize;
|
||||||
self.y = ((self.y as i16) + dy) as usize;
|
self.y = max((self.y as i16) + dy, 0) as usize;
|
||||||
|
(self.x, self.y)
|
||||||
}
|
}
|
||||||
pub fn set(&mut self, level: usize, x: usize, y: usize) {
|
pub fn set(&mut self, level: usize, x: usize, y: usize) {
|
||||||
self.level = level;
|
self.level = level;
|
||||||
|
@ -32,4 +35,34 @@ impl Position {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_y(&self) -> usize { self.y }
|
pub fn get_y(&self) -> usize { self.y }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_position_change() {
|
||||||
|
let mut p = Position::new(0, 10, 10);
|
||||||
|
|
||||||
|
let res = p.change(-1, 1);
|
||||||
|
assert_eq!(res, (9, 11));
|
||||||
|
assert_eq!(p.get_x(), 9);
|
||||||
|
assert_eq!(p.get_y(), 11);
|
||||||
|
|
||||||
|
let res = p.change(-9, 1);
|
||||||
|
assert_eq!(res, (0, 12));
|
||||||
|
assert_eq!(p.get_x(), 0);
|
||||||
|
assert_eq!(p.get_y(), 12);
|
||||||
|
|
||||||
|
let res = p.change(-1, -13);
|
||||||
|
assert_eq!(res, (0, 0));
|
||||||
|
assert_eq!(p.get_x(), 0);
|
||||||
|
assert_eq!(p.get_y(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_position_set() {
|
||||||
|
let mut p = Position::new(0, 10, 10);
|
||||||
|
|
||||||
|
p.set(1, 2, 3);
|
||||||
|
assert_eq!(p.get_level(), 1);
|
||||||
|
assert_eq!(p.get_x(), 2);
|
||||||
|
assert_eq!(p.get_y(), 3);
|
||||||
}
|
}
|
Loading…
Reference in New Issue