room-creation #15

Merged
Glaeder merged 18 commits from room-creation into main 2024-07-07 14:13:52 +00:00
8 changed files with 195 additions and 59 deletions

37
Cargo.lock generated
View file

@ -242,7 +242,7 @@ dependencies = [
"regex",
"rustc-hash",
"shlex",
"syn 2.0.68",
"syn 2.0.69",
]
[[package]]
@ -326,7 +326,7 @@ checksum = "1ee891b04274a59bd38b412188e24b849617b2e45a0fd8d057deb63e7403761b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.68",
"syn 2.0.69",
]
[[package]]
@ -369,9 +369,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.0.104"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74b6a57f98764a267ff415d50a25e6e166f3831a5071af4995296ea97d210490"
checksum = "5208975e568d83b6b05cc0a063c8e7e9acc2b43bee6da15616a5b73e109d7437"
dependencies = [
"jobserver",
"libc",
@ -1111,7 +1111,7 @@ checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.68",
"syn 2.0.69",
]
[[package]]
@ -1897,7 +1897,7 @@ checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.68",
"syn 2.0.69",
]
[[package]]
@ -1927,7 +1927,7 @@ dependencies = [
"proc-macro-crate",
"proc-macro2",
"quote",
"syn 2.0.68",
"syn 2.0.69",
]
[[package]]
@ -2130,6 +2130,7 @@ dependencies = [
"comfy",
"heck",
"indexmap",
"log",
"resvg",
]
@ -2427,22 +2428,22 @@ checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73"
[[package]]
name = "serde"
version = "1.0.203"
version = "1.0.204"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094"
checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.203"
version = "1.0.204"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.68",
"syn 2.0.69",
]
[[package]]
@ -2702,9 +2703,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.68"
version = "2.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9"
checksum = "201fcda3845c23e8212cd466bfebf0bd20694490fc0356ae8e428e0824a915a6"
dependencies = [
"proc-macro2",
"quote",
@ -2737,7 +2738,7 @@ checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.68",
"syn 2.0.69",
]
[[package]]
@ -2977,7 +2978,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.68",
"syn 2.0.69",
"wasm-bindgen-shared",
]
@ -3011,7 +3012,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.68",
"syn 2.0.69",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -3695,7 +3696,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.68",
"syn 2.0.69",
]
[[package]]

View file

@ -17,6 +17,7 @@ opt-level = 3
[dependencies]
comfy = { version = "0.4.0", features = ["wayland"] }
indexmap = "2"
log = "0.4.22"
[build-dependencies]
heck = "0.5"

View file

@ -23,7 +23,7 @@ impl FurnitureAsset {
}
fn asset_path_magnet(&self) -> String {
format!("{}/magnet/{}.png", self.folder, self.name)
format!("{}/metal/{}.png", self.folder, self.name)
}
fn asset_path_elec(&self) -> String {
@ -110,4 +110,8 @@ impl Furniture {
pub fn get_magnet_texture_handle(&self) -> Option<TextureHandle> {
self.handles.magnet
}
pub fn is_on(&self) -> bool {
(self.on)()
}
}

View file

@ -3,33 +3,56 @@ mod grid;
mod player;
mod room;
use self::{grid::Grid, player::Player, room::Room};
use crate::State;
use comfy::{error, EngineContext};
use comfy::{random_i32, EngineContext};
use grid::Grid;
use log::error;
use player::Player;
use room::Room;
const MAX_ROOMS: i32 = 6;
#[derive(Debug)]
pub struct HouseState {
room: Room,
current_room_id: usize,
room_count: usize,
rooms: Vec<Room>,
//grid: Grid,
player: Player
player: Player,
human_layer: bool //Human, magnetic, electric
}
impl HouseState {
pub fn generate_new_house(ctx: &mut EngineContext<'_>) -> Self {
let room = Room::new(ctx);
let player = Player::new(&room);
HouseState { room, player }
let room_count = random_i32(2, MAX_ROOMS) as usize;
let mut rooms = Vec::new();
for _ in 0 .. room_count {
rooms.push(Room::new(ctx));
}
let player = Player::new(rooms.first().unwrap());
HouseState {
current_room_id: 0,
room_count,
rooms,
player,
human_layer: false
}
}
}
pub fn draw(state: &State, _ctx: &comfy::EngineContext<'_>) {
pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) {
let Some(house) = state.house() else {
error!("How can I render a house when I'm not in one?!?");
return;
};
//Draw House
house.room.draw();
house
.rooms
.get(house.current_room_id)
.unwrap()
.draw(house.human_layer, house.player.can_see_metal(0.1));
//Draw Grid
//state.house.grid.draw();
@ -38,15 +61,28 @@ pub fn draw(state: &State, _ctx: &comfy::EngineContext<'_>) {
house.player.draw();
}
pub fn update(state: &mut State, ctx: &mut comfy::EngineContext<'_>) {
pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) {
let house = state.house_mut(ctx);
house.player.update(&house.room.grid);
let current_room = house.rooms.get(house.current_room_id).unwrap();
house.player.update(&current_room.grid);
if house.player.is_moving_to_right_room(&house.room) {
house.room = Room::new(ctx);
house.player.reset_on_room(&house.room, true);
} else if house.player.is_moving_to_left_room(&house.room) {
house.room = Room::new(ctx);
house.player.reset_on_room(&house.room, false);
if house.player.is_moving_to_right_room(current_room) {
if house.current_room_id < (house.room_count - 1) {
house.current_room_id += 1;
let current_room = house.rooms.get(house.current_room_id).unwrap();
house.player.reset_on_room(current_room, true);
} else {
house.player.reset_on_room(current_room, false);
}
} else if house.player.is_moving_to_left_room(current_room) {
if house.current_room_id > 0 {
house.current_room_id -= 1;
let current_room = house.rooms.get(house.current_room_id).unwrap();
house.player.reset_on_room(current_room, false);
} else {
house.player.reset_on_room(current_room, true);
}
}
}

View file

@ -9,6 +9,9 @@ use std::collections::HashSet;
pub struct Player {
position: Vec2,
speed: f32,
last_speed: f32,
current_acceleration: f32,
movement_time: f32,
connection: usize,
next_connections: Vec<usize>
}
@ -20,7 +23,10 @@ impl Player {
((0.25) - room.size.0 as f32 / 2.0) * SCALE,
room.grid.nodes.first().unwrap().y
),
speed: 10.0,
speed: 0.0,
last_speed: 0.0,
current_acceleration: 0.0,
movement_time: 0.0,
connection: 0,
next_connections: vec![1]
}
@ -57,8 +63,38 @@ impl Player {
};
self.position = vec2(x, room.grid.nodes.first().unwrap().y);
self.connection = 0;
self.next_connections = vec![1];
if place_left {
self.connection = 0;
self.next_connections = vec![1];
} else {
let mut current_index = 0;
for (i, node_pos) in room.grid.nodes.iter().enumerate().rev() {
if in_node_range(
&vec2((room.size.0 as f32 / 2.0 + 0.5) * SCALE, self.position.y),
node_pos,
1.0
) {
current_index = i;
}
}
let connction_index = room
.grid
.connections
.iter()
.enumerate()
.find(|(_i, (_node_1, node_2))| *node_2 == current_index)
.map(|(i, _)| i)
.unwrap();
self.connection = connction_index;
self.next_connections = vec![connction_index - 1];
}
}
pub fn can_see_metal(&self, threshold: f32) -> bool {
self.current_acceleration > threshold
}
}
@ -70,6 +106,22 @@ fn move_player(player: &mut Player, allowed_movement: (bool, bool, bool, bool))
allow_right_movement
) = allowed_movement;
if is_key_down(KeyCode::Up)
|| is_key_down(KeyCode::Down)
|| is_key_down(KeyCode::Left)
|| is_key_down(KeyCode::Right)
{
player.movement_time += delta();
player.speed = calc_player_speed(player.movement_time);
player.speed = player.speed.clamp(0.0, 10.0);
} else {
player.speed = 0.0;
player.last_speed = 0.0;
player.movement_time = 0.0;
}
player.current_acceleration = (player.speed - player.last_speed).abs();
if allow_up_movement && is_key_down(KeyCode::Up) {
player.position += vec2(0.0, player.speed) * delta();
}
@ -85,6 +137,14 @@ fn move_player(player: &mut Player, allowed_movement: (bool, bool, bool, bool))
if allow_left_movement && is_key_down(KeyCode::Left) {
player.position += vec2(-player.speed, 0.0) * delta();
}
player.last_speed = player.speed;
}
fn calc_player_speed(time: f32) -> f32 {
//3x^2-2x^3; x=t
let t = time * 20.0;
3.0 * t * t - 2.0 * t * t
}
//(UP, DOWN, LEFT, RIGHT)

View file

@ -310,14 +310,14 @@ impl Room {
Grid::new(nodes, connections)
}
pub fn draw(&self) {
pub fn draw(&self, human_layer: bool, magnet_layer: bool) {
let (width, height) = self.size;
draw_rect(
vec2(0.0, 0.0),
vec2(width as f32 * SCALE, height as f32 * SCALE),
PURPLE,
game::ZLayer::MapMax as i32 - 2
game::ZLayer::HumanLayer as i32 - 4
);
draw_rect_outline(
vec2(0.0, 0.0),
@ -331,22 +331,48 @@ impl Room {
let mut pos = tile.pos - vec2(width as f32 / 2.0, height as f32 / 2.0);
pos += tile.size * 0.5;
if let Some(texture) = tile.f.get_human_texture_handle() {
draw_sprite(
texture,
pos * SCALE,
WHITE,
game::ZLayer::MapMax as i32 + tile.z,
tile.size * SCALE
);
} else {
draw_rect_outline(
pos * SCALE,
tile.size * SCALE,
0.3,
GREEN,
game::ZLayer::MapMax as i32 + tile.z
);
if human_layer {
if let Some(texture) = tile.f.get_human_texture_handle() {
draw_sprite(
texture,
pos * SCALE,
WHITE,
game::ZLayer::HumanLayer as i32 + tile.z,
tile.size * SCALE
);
} else {
draw_rect_outline(
pos * SCALE,
tile.size * SCALE,
0.3,
GREEN,
game::ZLayer::HumanLayer as i32 + tile.z
);
}
}
if magnet_layer {
if let Some(texture) = tile.f.get_magnet_texture_handle() {
draw_sprite(
texture,
pos * SCALE,
WHITE,
game::ZLayer::MagneticLayer as i32 + tile.z,
tile.size * SCALE
);
}
}
if tile.f.is_on() {
if let Some(texture) = tile.f.get_elec_texture_handle() {
draw_sprite(
texture,
pos * SCALE,
WHITE,
game::ZLayer::ElectricLayer as i32 + tile.z,
tile.size * SCALE
);
}
}
}

View file

@ -66,6 +66,9 @@ impl Default for Ghost {
}
#[repr(i32)]
pub enum ZLayer {
HumanLayer = -8,
MagneticLayer = -5,
ElectricLayer = -2,
MapMax = -1,
Human = 0,
Ghost = 1
@ -86,8 +89,10 @@ impl Sub<i32> for ZLayer {
}
}
pub fn setup(_state: &mut State, ctx: &mut EngineContext<'_>) {
pub fn setup(state: &mut State, ctx: &mut EngineContext<'_>) {
Assets::load(ctx);
//house::setup(state, ctx);
}
pub fn update(state: &mut State, engine: &mut EngineContext<'_>) {

View file

@ -76,12 +76,15 @@ impl GameLoop for State {
fn update(&mut self, ctx: &mut EngineContext<'_>) {
if !self.setup_called {
game::setup(self, ctx);
game::setup(self, ctx);
self.setup_called = true;
}
game::update(self, ctx);
game::draw(self, ctx);
game::update(self, ctx);
game::draw(self, ctx);
}
}