Added layers
This commit is contained in:
parent
2498152b21
commit
13e54db09c
5 changed files with 98 additions and 23 deletions
|
@ -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)()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,14 +12,19 @@ use room::Room;
|
||||||
pub struct HouseState {
|
pub struct HouseState {
|
||||||
room: Room,
|
room: 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<'_>) {
|
||||||
let house = {
|
let house = {
|
||||||
let room = Room::new(ctx);
|
let room = Room::new(ctx);
|
||||||
let player = Player::new(&room);
|
let player = Player::new(&room);
|
||||||
HouseState { room, player }
|
HouseState {
|
||||||
|
room,
|
||||||
|
player,
|
||||||
|
human_layer: false
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
state.house = Some(house);
|
state.house = Some(house);
|
||||||
|
@ -28,7 +33,9 @@ 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.room.draw();
|
house
|
||||||
|
.room
|
||||||
|
.draw(house.human_layer, house.player.can_see_metal(0.1));
|
||||||
|
|
||||||
//Draw Grid
|
//Draw Grid
|
||||||
//state.house.grid.draw();
|
//state.house.grid.draw();
|
||||||
|
|
|
@ -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]
|
||||||
}
|
}
|
||||||
|
@ -60,6 +66,10 @@ impl Player {
|
||||||
self.connection = 0;
|
self.connection = 0;
|
||||||
self.next_connections = vec![1];
|
self.next_connections = vec![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)) {
|
||||||
|
@ -70,6 +80,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();
|
||||||
}
|
}
|
||||||
|
@ -85,6 +111,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)
|
||||||
|
|
|
@ -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
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue