make size of levels variable

This commit is contained in:
Joachim Lusiardi 2024-10-23 15:54:33 +02:00
parent 5111fec1fa
commit 6f4c37728f
3 changed files with 33 additions and 23 deletions

View File

@ -6,6 +6,10 @@ use rand::Rng;
use crate::artifacts::Artifact;
#[cfg(test)]
use crate::artifacts::{Chest, Potion};
use crate::level_generator::ROOMS_HORIZONTAL;
use crate::level_generator::ROOMS_VERTICAL;
use crate::level_generator::ROOM_HEIGHT;
use crate::level_generator::ROOM_WIDTH;
use crate::monster::Monster;
#[cfg(test)]
use crate::monster::{Orc, Rat};
@ -13,8 +17,8 @@ use crate::player::Player;
use crate::position::HasPosition;
use crate::position::Position;
pub const LEVEL_WIDTH: usize = 50;
pub const LEVEL_HEIGHT: usize = 25;
pub const LEVEL_WIDTH: usize = 1 + ROOMS_VERTICAL * ROOM_WIDTH;
pub const LEVEL_HEIGHT: usize = 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT;
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum StructureElement {

View File

@ -14,11 +14,11 @@ use crate::level::{Level, StructureElement};
use crate::monster::{Monster, Orc, Rat, Snake};
use crate::position::Position;
const ROOMS_VERTICAL: usize = 7;
const ROOMS_HORIZONTAL: usize = 4;
pub const ROOMS_VERTICAL: usize = 9;
pub const ROOMS_HORIZONTAL: usize = 7;
const ROOM_WIDTH: usize = 7;
const ROOM_HEIGHT: usize = 6;
pub const ROOM_WIDTH: usize = 7;
pub const ROOM_HEIGHT: usize = 6;
#[derive(PartialEq, Copy, Clone)]
enum RoomType {

View File

@ -33,6 +33,9 @@ mod position;
/// length of a game frame in ms
pub const FRAME_LENGTH: u64 = 100;
pub const MIN_WIDTH: u16 = 120;
pub const MIN_HEIGHT: u16 = 30;
//
fn main() -> Result<()> {
let mut game = Game::new(Player::new(realname().as_str(), 30));
@ -54,27 +57,30 @@ fn main() -> Result<()> {
// don't draw stuff except an info box if the terminal is too small (less than 80x25)
// to prevent the read drawing code from crashing the game.
if area.width < 80 || area.height < 25 {
if area.width < MIN_WIDTH || area.height < MIN_HEIGHT {
let block = Block::default()
.title("Info")
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::White))
.border_type(BorderType::Rounded)
.style(Style::default().bg(Color::Black));
let paragraph = Paragraph::new("Terminal needs to be at leas 80x25!")
let paragraph = Paragraph::new(format!(
"Terminal needs to be at leas {}x{}!",
MIN_WIDTH, MIN_HEIGHT
))
.block(block)
.wrap(Wrap { trim: true });
frame.render_widget(paragraph, area);
return;
}
if area.width > 80 {
area.x = (area.width - 80) / 2;
area.width = 80;
if area.width > MIN_WIDTH {
area.x = (area.width - MIN_WIDTH) / 2;
area.width = MIN_WIDTH;
}
if area.height > 25 {
area.y = (area.height - 25) / 2;
area.height = 25;
if area.height > MIN_HEIGHT {
area.y = (area.height - level::LEVEL_HEIGHT as u16) / 2;
area.height = level::LEVEL_HEIGHT as u16;
}
let map_area = Rect {
@ -86,10 +92,10 @@ fn main() -> Result<()> {
frame.render_stateful_widget(LevelWidget {}, map_area, &mut game);
let stats_area = Rect {
x: area.x + 50,
x: area.x + map_area.width,
y: area.y,
width: 30,
height: 15,
width: MIN_WIDTH - map_area.width,
height: map_area.height / 2 + 1,
};
let block = Block::default()
.title(
@ -117,10 +123,10 @@ fn main() -> Result<()> {
stats_area,
);
let messages_area = Rect {
x: area.x + 50,
y: area.y + 15,
width: 30,
height: 10,
x: area.x + map_area.width,
y: area.y + map_area.height / 2 + 1,
width: MIN_WIDTH - map_area.width,
height: map_area.height / 2,
};
// Display the latest messages from the game to the user
let block = Block::default()