Overworld Movement #9

Merged
msrd0 merged 9 commits from move_overworld into main 2024-07-07 08:49:20 +00:00
3 changed files with 53 additions and 8 deletions
Showing only changes of commit d90b0ad992 - Show all commits

View file

@ -1,8 +1,13 @@
use comfy::{
draw_rect_outline, draw_sprite, main_camera_mut, EngineContext, IVec2, Vec2, RED,
WHITE
draw_circle, draw_rect_outline, draw_sprite, main_camera_mut, EngineContext, IVec2,
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;
pub mod worldgen;
@ -16,16 +21,54 @@ pub fn draw(state: &crate::State, _engine: &comfy::EngineContext<'_>) {
coords.as_vec2(),
WHITE,
ZLayer::MapMax - i,
Vec2::ONE
Vec2::ONE,
);
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<'_>) {
let mut camera = main_camera_mut();
camera.center = Vec2::ZERO;
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
/// tiles in the chunk.
const CHUNK_SIZE: u32 = 100;
const CHUNK_SIZE: u32 = 50;
/// Chunks
#[derive(Debug)]

View file

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