room-creation #15

Merged
Glaeder merged 18 commits from room-creation into main 2024-07-07 14:13:52 +00:00
5 changed files with 97 additions and 23 deletions
Showing only changes of commit 641fda7043 - Show all commits

View file

@ -23,7 +23,7 @@ impl FurnitureAsset {
} }
fn asset_path_magnet(&self) -> String { 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 { fn asset_path_elec(&self) -> String {
@ -110,4 +110,8 @@ impl Furniture {
pub fn get_magnet_texture_handle(&self) -> Option<TextureHandle> { pub fn get_magnet_texture_handle(&self) -> Option<TextureHandle> {
self.handles.magnet self.handles.magnet
} }
pub fn is_on(&self) -> bool {
(self.on)()
}
} }

View file

@ -17,7 +17,8 @@ pub struct HouseState {
room_count: usize, room_count: usize,
rooms: Vec<Room>, rooms: Vec<Room>,
//grid: Grid, //grid: Grid,
player: Player player: Player,
human_layer: bool //Human, magnetic, electric
} }
pub fn setup(state: &mut crate::State, ctx: &mut EngineContext<'_>) { pub fn setup(state: &mut crate::State, ctx: &mut EngineContext<'_>) {
@ -30,7 +31,11 @@ pub fn setup(state: &mut crate::State, ctx: &mut EngineContext<'_>) {
} }
let player = Player::new(rooms.first().unwrap()); let player = Player::new(rooms.first().unwrap());
HouseState { current_room_id: 0, room_count, rooms, player } HouseState { current_room_id: 0, room_count,
rooms,
player,
human_layer: false
}
}; };
state.house = Some(house); state.house = Some(house);
@ -39,7 +44,8 @@ pub fn setup(state: &mut crate::State, ctx: &mut EngineContext<'_>) {
pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) { pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) {
if let Some(house) = &state.house { if let Some(house) = &state.house {
//Draw House //Draw House
house.rooms.get(house.current_room_id).unwrap().draw(); house
.rooms.get(house.current_room_id).unwrap().draw(house.human_layer, house.player.can_see_metal(0.1));
//Draw Grid //Draw Grid
//state.house.grid.draw(); //state.house.grid.draw();

View file

@ -9,6 +9,9 @@ use std::collections::HashSet;
pub struct Player { pub struct Player {
position: Vec2, position: Vec2,
speed: f32, speed: f32,
last_speed: f32,
current_acceleration: f32,
movement_time: f32,
connection: usize, connection: usize,
next_connections: Vec<usize> next_connections: Vec<usize>
} }
@ -20,7 +23,10 @@ impl Player {
((0.25) - room.size.0 as f32 / 2.0) * SCALE, ((0.25) - room.size.0 as f32 / 2.0) * SCALE,
room.grid.nodes.first().unwrap().y 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, connection: 0,
next_connections: vec![1] next_connections: vec![1]
} }
@ -75,6 +81,10 @@ impl Player {
self.next_connections = vec![connction_index - 1]; self.next_connections = vec![connction_index - 1];
} }
} }
pub fn can_see_metal(&self, threshold: f32) -> bool {
self.current_acceleration > threshold
}
} }
fn move_player(player: &mut Player, allowed_movement: (bool, bool, bool, bool)) { fn move_player(player: &mut Player, allowed_movement: (bool, bool, bool, bool)) {
@ -85,6 +95,22 @@ fn move_player(player: &mut Player, allowed_movement: (bool, bool, bool, bool))
allow_right_movement allow_right_movement
) = allowed_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) { if allow_up_movement && is_key_down(KeyCode::Up) {
player.position += vec2(0.0, player.speed) * delta(); player.position += vec2(0.0, player.speed) * delta();
} }
@ -100,6 +126,14 @@ fn move_player(player: &mut Player, allowed_movement: (bool, bool, bool, bool))
if allow_left_movement && is_key_down(KeyCode::Left) { if allow_left_movement && is_key_down(KeyCode::Left) {
player.position += vec2(-player.speed, 0.0) * delta(); 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) //(UP, DOWN, LEFT, RIGHT)

View file

@ -241,14 +241,15 @@ impl Room {
Grid::new(nodes, connections) Grid::new(nodes, connections)
} }
pub fn draw(&self) { //Human, magnetic, electric
pub fn draw(&self, human_layer: bool, magnet_layer: bool) {
let (width, height) = self.size; let (width, height) = self.size;
draw_rect( draw_rect(
vec2(0.0, 0.0), vec2(0.0, 0.0),
vec2(width as f32 * SCALE, height as f32 * SCALE), vec2(width as f32 * SCALE, height as f32 * SCALE),
PURPLE, PURPLE,
game::ZLayer::MapMax as i32 - 2 game::ZLayer::HumanLayer as i32 - 4
); );
draw_rect_outline( draw_rect_outline(
vec2(0.0, 0.0), vec2(0.0, 0.0),
@ -262,22 +263,48 @@ impl Room {
let mut pos = tile.pos - vec2(width as f32 / 2.0, height as f32 / 2.0); let mut pos = tile.pos - vec2(width as f32 / 2.0, height as f32 / 2.0);
pos += tile.size * 0.5; pos += tile.size * 0.5;
if let Some(texture) = tile.f.get_human_texture_handle() { if human_layer {
draw_sprite( if let Some(texture) = tile.f.get_human_texture_handle() {
texture, draw_sprite(
pos * SCALE, texture,
WHITE, pos * SCALE,
game::ZLayer::MapMax as i32 + tile.z, WHITE,
tile.size * SCALE game::ZLayer::HumanLayer as i32 + tile.z,
); tile.size * SCALE
} else { );
draw_rect_outline( } else {
pos * SCALE, draw_rect_outline(
tile.size * SCALE, pos * SCALE,
0.3, tile.size * SCALE,
GREEN, 0.3,
game::ZLayer::MapMax as i32 + tile.z 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

@ -24,6 +24,9 @@ impl Default for Ghost {
} }
#[repr(i32)] #[repr(i32)]
pub enum ZLayer { pub enum ZLayer {
HumanLayer = -8,
MagneticLayer = -5,
ElectricLayer = -2,
MapMax = -1, MapMax = -1,
Human = 0, Human = 0,
Ghost = 1 Ghost = 1