starting on enemies

This commit is contained in:
Joachim Lusiardi 2023-11-24 16:03:32 +01:00
parent dc82e021da
commit a61dfda3c9
4 changed files with 71 additions and 28 deletions

View File

@ -15,8 +15,8 @@ impl Game {
let new_x: i16 = player_pos.get_x() as i16 + dx;
let new_y: i16 = player_pos.get_y() as i16 + dy;
let can_go: bool = match level.get_element(new_x, new_y) {
(None, _) => { false }
(Some(t), _) => {
(None, _, _) => { false }
(Some(t), _, _) => {
match t {
StructureElement::Wall => { false }
_ => { true }
@ -52,6 +52,15 @@ impl Game {
}
_ => {}
}
match tmp.2 {
Some() => {
self.levels[player_pos.get_level()].remove_artifact(new_x, new_y);
self.player.add_gold(gold);
}
_ => {}
}
}
}
}

View File

@ -20,12 +20,18 @@ pub enum Artifact {
Chest { gold: usize },
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Enemy {
Rat
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Level {
pub(crate) structure: [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) discovered: [[bool; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) artifacts: [[Option<Artifact>; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) enemies: [[Option<Enemy>; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) start_x: usize,
pub(crate) start_y: usize,
pub(crate) end_x: usize,
@ -39,6 +45,7 @@ impl Level {
structure: s,
discovered: [[false; LEVEL_HEIGHT]; LEVEL_WIDTH],
artifacts: [[None; LEVEL_HEIGHT]; LEVEL_WIDTH],
enemies: [[None; LEVEL_HEIGHT]; LEVEL_WIDTH],
start_x: 1,
start_y: 1,
end_x: 1,
@ -56,47 +63,57 @@ impl Level {
}
self.artifacts[x][y] = None;
}
pub fn get_element(&self, x: i16, y: i16) -> (Option<StructureElement>, Option<Artifact>) {
pub fn get_element(&self, x: i16, y: i16) -> (Option<StructureElement>, Option<Artifact>, Option<Enemy>) {
if x < 0 || y < 0 {
return (None, None);
return (None, None, None);
}
let x = x as usize;
let y = y as usize;
if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT {
return (None, None);
return (None, None, None);
}
if !self.discovered[x][y] {
return (Some(StructureElement::Unknown), None);
return (Some(StructureElement::Unknown), None, None);
}
return (Some(self.structure[x][y]), self.artifacts[x][y]);
return (Some(self.structure[x][y]), self.artifacts[x][y], self.enemies[x][y]);
}
/// discover the area with in the level around the given position
pub fn discover(&mut self, pos: Position) {
let x = pos.get_x();
let y = pos.get_y();
// uncover fields directly next to the player
for x_r in max(x - 1, 0)..=min(x + 1, LEVEL_WIDTH - 1) {
for y_r in max(y - 1, 0)..=min(y + 1, LEVEL_HEIGHT - 1) {
self.discovered[x_r][y_r] = true;
}
}
// uncover fields up to 2 fields right of the player
for x_r in x..=min(x + 2, LEVEL_WIDTH - 1) {
self.discovered[x_r][y] = true;
if self.structure[x_r][y] == StructureElement::Wall {
break;
}
}
// uncover fields up to 2 fields below of the player
for y_r in y..=min(y + 2, LEVEL_HEIGHT - 1) {
self.discovered[x][y_r] = true;
if self.structure[x][y_r] == StructureElement::Wall {
break;
}
}
// uncover fields up to 2 fields left of the player
for x_r in (max(x, 2)..x + 2).rev() {
self.discovered[x_r - 2][y] = true;
if self.structure[x_r - 2][y] == StructureElement::Wall {
break;
}
}
// uncover fields up to 2 fields above of the player
for y_r in (max(y, 2)..y + 2).rev() {
self.discovered[x][y_r - 2] = true;
if self.structure[x][y_r - 2] == StructureElement::Wall {

View File

@ -9,7 +9,7 @@ use rand::prelude::SliceRandom;
use rand::Rng;
use rand::rngs::ThreadRng;
use crate::level::{Artifact, Level, StructureElement};
use crate::level::{Artifact, Enemy, Level, StructureElement};
const ROOMS_VERTICAL: usize = 7;
const ROOMS_HORIZONTAL: usize = 4;
@ -24,6 +24,7 @@ pub enum RoomType {
StairDown,
BasicRoom,
TreasureRoom,
MonsterRoom,
EmptyRoom,
}
@ -102,12 +103,13 @@ impl LevelGenerator {
} else {
room_types.push(RoomType::StairDown);
}
room_types.push(RoomType::TreasureRoom);
room_types.push(RoomType::MonsterRoom);
// generate a random set of rooms and shuffle them
for _ in room_types.len()..ROOMS_HORIZONTAL * ROOMS_VERTICAL {
match rng.gen_range(1..=6) {
match rng.gen_range(1..=100) {
// TODO tune room type distribution
1..=3 => { room_types.push(RoomType::EmptyRoom) }
1..=33 => { room_types.push(RoomType::EmptyRoom) }
34..=66 => { room_types.push(RoomType::TreasureRoom) }
_ => { room_types.push(RoomType::BasicRoom) }
}
}
@ -236,8 +238,9 @@ impl LevelGenerator {
}
pub fn render(&self) -> Level {
let mut rng = rand::thread_rng();
let mut s = [[StructureElement::Wall; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH];
let mut d = [[None; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH];
let mut structure = [[StructureElement::Wall; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH];
let mut artifacts = [[None; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH];
let mut enemies = [[None; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH];
let mut start_x: usize = 0;
let mut start_y: usize = 0;
let mut end_x: usize = 0;
@ -249,33 +252,39 @@ impl LevelGenerator {
let room = self.rooms[c][r];
for x in 0..room.width {
for y in 0..room.height {
s[left + room.offset_x + x][top + room.offset_y + y] = StructureElement::Floor;
structure[left + room.offset_x + x][top + room.offset_y + y] = StructureElement::Floor;
}
}
if room.kind == RoomType::TreasureRoom {
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);
d[t_x][t_y] = Some(Artifact::Chest { gold: rng.gen_range(2..100) });
artifacts[t_x][t_y] = Some(Artifact::Chest { gold: rng.gen_range(2..30) });
}
if room.kind == RoomType::MonsterRoom {
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);
// TODO randomize enemies here
enemies[t_x][t_y] = Some(Enemy::Rat);
}
if room.kind == RoomType::StairDown {
end_x = left + room.offset_x + rng.gen_range(0..room.width);
end_y = top + room.offset_y + rng.gen_range(0..room.height);
}
if room.kind == RoomType::StairDown {
s[end_x][end_y] = StructureElement::StairDown;
structure[end_x][end_y] = StructureElement::StairDown;
}
if room.kind == RoomType::End {
s[end_x][end_y] = StructureElement::End;
structure[end_x][end_y] = StructureElement::End;
}
if room.kind == RoomType::Start || room.kind == RoomType::StairUp {
start_x = left + room.offset_x + rng.gen_range(0..room.width);
start_y = top + room.offset_y + rng.gen_range(0..room.height);
}
if room.kind == RoomType::StairUp {
s[start_x][start_y] = StructureElement::StairUp;
structure[start_x][start_y] = StructureElement::StairUp;
}
if room.kind == RoomType::Start {
s[start_x][start_y] = StructureElement::Start;
structure[start_x][start_y] = StructureElement::Start;
}
}
}
@ -289,8 +298,8 @@ impl LevelGenerator {
let left = 1 + c * ROOM_WIDTH + x_conn.offset;
let bottom = 1 + (r + x_conn.distance) * ROOM_HEIGHT + tgt_room.offset_y + tgt_room.height;
for i in top..bottom {
if s[left][i] == StructureElement::Wall {
s[left][i] = StructureElement::Floor;
if structure[left][i] == StructureElement::Wall {
structure[left][i] = StructureElement::Floor;
}
}
}
@ -301,17 +310,18 @@ impl LevelGenerator {
let left = 1 + c * ROOM_WIDTH + src_room.offset_x;
let right = 1 + (c + y_conn.distance) * ROOM_WIDTH + tgt_room.offset_x + tgt_room.width;
for i in left..right {
if s[i][top] == StructureElement::Wall {
s[i][top] = StructureElement::Floor;
if structure[i][top] == StructureElement::Wall {
structure[i][top] = StructureElement::Floor;
}
}
}
}
}
Level {
structure: s,
structure,
discovered: [[false; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH],
artifacts: d,
artifacts,
enemies,
start_x,
start_y,
end_x,

View File

@ -82,15 +82,22 @@ impl<'a> Widget for LevelWidget<'a> {
let level_x = (x - al) as i16;
let level_y = (y - at) as i16;
match level.get_element(level_x, level_y) {
(None, _) => {}
(_, Some(e)) => {
(None, _, _) => {}
(_, Some(e), _) => {
match e {
Artifact::Chest { gold: _ } => {
self.set_cell(buf, x, y, "", Color::Black, Color::Gray);
}
}
}
(Some(e), _) => {
(_, _, Some(e)) => {
match e {
Ene => {
self.set_cell(buf, x, y, "R", Color::Black, Color::Gray);
}
}
}
(Some(e), _, _) => {
match e {
StructureElement::Wall => {
let top = level.get_element(level_x, level_y - 1).0;