Merge branch 'room-layers' into room-creation
This commit is contained in:
commit
641fda7043
5 changed files with 97 additions and 23 deletions
|
@ -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)()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,8 @@ pub struct HouseState {
|
|||
room_count: usize,
|
||||
rooms: Vec<Room>,
|
||||
//grid: Grid,
|
||||
player: Player
|
||||
player: Player,
|
||||
human_layer: bool //Human, magnetic, electric
|
||||
}
|
||||
|
||||
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());
|
||||
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);
|
||||
|
@ -39,7 +44,8 @@ pub fn setup(state: &mut crate::State, ctx: &mut EngineContext<'_>) {
|
|||
pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) {
|
||||
if let Some(house) = &state.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
|
||||
//state.house.grid.draw();
|
||||
|
|
|
@ -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]
|
||||
}
|
||||
|
@ -75,6 +81,10 @@ impl Player {
|
|||
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)) {
|
||||
|
@ -85,6 +95,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();
|
||||
}
|
||||
|
@ -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) {
|
||||
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)
|
||||
|
|
|
@ -241,14 +241,15 @@ impl Room {
|
|||
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;
|
||||
|
||||
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),
|
||||
|
@ -262,22 +263,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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -24,6 +24,9 @@ impl Default for Ghost {
|
|||
}
|
||||
#[repr(i32)]
|
||||
pub enum ZLayer {
|
||||
HumanLayer = -8,
|
||||
MagneticLayer = -5,
|
||||
ElectricLayer = -2,
|
||||
MapMax = -1,
|
||||
Human = 0,
|
||||
Ghost = 1
|
||||
|
|
Loading…
Reference in a new issue