start move ment

This commit is contained in:
luckyturtledev 2024-07-07 00:52:19 +02:00
parent 9c8a399b38
commit d90b0ad992
3 changed files with 53 additions and 8 deletions

View file

@ -1,8 +1,13 @@
use comfy::{ use comfy::{
draw_rect_outline, draw_sprite, main_camera_mut, EngineContext, IVec2, Vec2, RED, draw_circle, draw_rect_outline, draw_sprite, main_camera_mut, EngineContext, IVec2,
WHITE Vec2, RED, WHITE,
}; };
use log::info;
use comfy::is_key_pressed;
use comfy::is_key_down;
use comfy::KeyCode;
use crate::game::Ghost;
use crate::game::ZLayer; use crate::game::ZLayer;
pub mod worldgen; pub mod worldgen;
@ -16,16 +21,54 @@ pub fn draw(state: &crate::State, _engine: &comfy::EngineContext<'_>) {
coords.as_vec2(), coords.as_vec2(),
WHITE, WHITE,
ZLayer::MapMax - i, ZLayer::MapMax - i,
Vec2::ONE Vec2::ONE,
); );
draw_rect_outline(coords.as_vec2(), Vec2::ONE, 0.1, RED, 10); draw_rect_outline(coords.as_vec2(), Vec2::ONE, 0.1, RED, 10);
} }
} }
draw_circle(
state.ghost.overworld_pos.as_vec2(),
0.5,
RED,
ZLayer::Ghost.into(),
);
} }
pub fn update(state: &mut crate::State, _engine: &mut EngineContext<'_>) { pub fn update(state: &mut crate::State, _engine: &mut EngineContext<'_>) {
let mut camera = main_camera_mut(); let mut camera = main_camera_mut();
camera.center = Vec2::ZERO; camera.center = Vec2::ZERO;
camera.zoom = 30.0; camera.zoom = 30.0;
state.overworld.get_or_generate_tile(IVec2::ZERO);
let mut ghost_pos = &mut state.ghost.overworld_pos;
// move player
if is_key_down(KeyCode::Up) {
ghost_pos.y +=1;
}
if is_key_down(KeyCode::Down) {
ghost_pos.y -=1;
}
if is_key_down(KeyCode::Left) {
ghost_pos.x -=1;
}
if is_key_down(KeyCode::Right) {
ghost_pos.x +=1;
}
// generate more chunks if needed
{
let half_viewport = (camera.world_viewport() * 0.5 + 3.0).as_ivec2();
state
.overworld
.get_or_generate_tile(*ghost_pos + IVec2::new(half_viewport.x, half_viewport.y));
state
.overworld
.get_or_generate_tile(*ghost_pos + IVec2::new(half_viewport.x, -half_viewport.y));
state
.overworld
.get_or_generate_tile(*ghost_pos + IVec2::new(-half_viewport.x, half_viewport.y));
state
.overworld
.get_or_generate_tile(*ghost_pos + IVec2::new(-half_viewport.x, -half_viewport.y));
}
} }

View file

@ -138,7 +138,7 @@ impl Tile {
/// The size of a chunk (both width and height). This value squared gives the amount of /// The size of a chunk (both width and height). This value squared gives the amount of
/// tiles in the chunk. /// tiles in the chunk.
const CHUNK_SIZE: u32 = 100; const CHUNK_SIZE: u32 = 50;
/// Chunks /// Chunks
#[derive(Debug)] #[derive(Debug)]

View file

@ -2,7 +2,7 @@ use crate::{
activities::{house, overworld, Activity}, activities::{house, overworld, Activity},
State State
}; };
use comfy::EngineContext; use comfy::{EngineContext, IVec2};
use std::ops::Sub; use std::ops::Sub;
#[derive(Debug)] #[derive(Debug)]
@ -10,14 +10,16 @@ pub struct Ghost {
/// current electric charge of the Ghost /// current electric charge of the Ghost
pub charge: f32, pub charge: f32,
/// max electric charge of the Ghost /// max electric charge of the Ghost
pub max_charge: f32 pub max_charge: f32,
pub overworld_pos: IVec2,
} }
impl Default for Ghost { impl Default for Ghost {
fn default() -> Self { fn default() -> Self {
Self { Self {
charge: 1000.0, charge: 1000.0,
max_charge: 1000.0 max_charge: 1000.0,
overworld_pos: IVec2::ZERO,
} }
} }
} }