Merge branch 'main' into shader
This commit is contained in:
commit
686b31e41d
4 changed files with 139 additions and 35 deletions
|
@ -3,7 +3,7 @@ mod grid;
|
||||||
mod player;
|
mod player;
|
||||||
mod room;
|
mod room;
|
||||||
|
|
||||||
use comfy::{random_i32, EngineContext};
|
use comfy::{main_camera_mut, random_i32, vec2, EngineContext, RandomRange as _};
|
||||||
use grid::Grid;
|
use grid::Grid;
|
||||||
use log::error;
|
use log::error;
|
||||||
use player::Player;
|
use player::Player;
|
||||||
|
@ -18,7 +18,12 @@ pub struct HouseState {
|
||||||
rooms: Vec<Room>,
|
rooms: Vec<Room>,
|
||||||
//grid: Grid,
|
//grid: Grid,
|
||||||
player: Player,
|
player: Player,
|
||||||
human_layer: bool //Human, magnetic, electric
|
human_layer: bool, //Human, magnetic, electric
|
||||||
|
|
||||||
|
/// The energy level remaining in the house. Should decrease by itself, and much
|
||||||
|
/// faster when inhabited by the ghost.
|
||||||
|
pub charge: f32,
|
||||||
|
pub max_charge: f32
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HouseState {
|
impl HouseState {
|
||||||
|
@ -31,12 +36,16 @@ impl HouseState {
|
||||||
}
|
}
|
||||||
|
|
||||||
let player = Player::new(rooms.first().unwrap());
|
let player = Player::new(rooms.first().unwrap());
|
||||||
|
let max_charge = f32::gen_range(2_000.0, 5_000.0);
|
||||||
HouseState {
|
HouseState {
|
||||||
current_room_id: 0,
|
current_room_id: 0,
|
||||||
room_count,
|
room_count,
|
||||||
rooms,
|
rooms,
|
||||||
player,
|
player,
|
||||||
human_layer: false
|
human_layer: false,
|
||||||
|
// TODO this should be lower depending on the time elapsed
|
||||||
|
charge: max_charge,
|
||||||
|
max_charge
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,6 +72,9 @@ pub fn draw(state: &crate::State, _ctx: &mut comfy::EngineContext<'_>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) {
|
pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) {
|
||||||
|
let mut camera = main_camera_mut();
|
||||||
|
camera.center = vec2(0.0, 0.0);
|
||||||
|
|
||||||
let house = state.house_mut(ctx);
|
let house = state.house_mut(ctx);
|
||||||
let current_room = house.rooms.get(house.current_room_id).unwrap();
|
let current_room = house.rooms.get(house.current_room_id).unwrap();
|
||||||
house.player.update(¤t_room.grid);
|
house.player.update(¤t_room.grid);
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
use crate::{activities::Activity, game::ZLayer, State};
|
use crate::{
|
||||||
|
activities::Activity,
|
||||||
|
game::{ZLayer, GHOST_DISCHARGE_RATE, GHOST_DISCHARGE_RATE_MOVEMENT},
|
||||||
|
State
|
||||||
|
};
|
||||||
use comfy::{
|
use comfy::{
|
||||||
draw_circle, draw_rect_outline, draw_sprite, error, info, is_key_down,
|
delta, draw_circle, draw_rect_outline, draw_sprite, error, info, is_key_down,
|
||||||
main_camera_mut, EngineContext, IVec2, KeyCode, Vec2, RED, WHITE
|
main_camera_mut, EngineContext, IVec2, KeyCode, Vec2, RED, WHITE
|
||||||
};
|
};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
@ -27,7 +31,7 @@ pub fn draw(state: &crate::State, _engine: &comfy::EngineContext<'_>) {
|
||||||
draw_circle(state.ghost.overworld_pos, 0.5, RED, ZLayer::Ghost.into());
|
draw_circle(state.ghost.overworld_pos, 0.5, RED, ZLayer::Ghost.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_move_player(state: &mut State, ctx: &mut EngineContext<'_>) {
|
fn update_move_player(state: &mut State, _ctx: &mut EngineContext<'_>) {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
|
|
||||||
// Are there any pending position updates? If so, we ignore all user input and execute
|
// Are there any pending position updates? If so, we ignore all user input and execute
|
||||||
|
@ -174,6 +178,15 @@ pub fn update(state: &mut State, ctx: &mut EngineContext<'_>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// energie lost
|
||||||
|
{
|
||||||
|
let ghost = &mut state.ghost;
|
||||||
|
ghost.charge -= GHOST_DISCHARGE_RATE * ctx.delta;
|
||||||
|
if ghost.overworld_movement_pending != Vec2::ZERO {
|
||||||
|
ghost.charge -= GHOST_DISCHARGE_RATE_MOVEMENT * ctx.delta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// generate more chunks if needed
|
// generate more chunks if needed
|
||||||
{
|
{
|
||||||
let half_viewport = (camera.world_viewport() * 0.5 + 3.0).as_ivec2();
|
let half_viewport = (camera.world_viewport() * 0.5 + 3.0).as_ivec2();
|
||||||
|
|
68
src/game.rs
68
src/game.rs
|
@ -4,7 +4,10 @@ use crate::{
|
||||||
State
|
State
|
||||||
};
|
};
|
||||||
use comfy::{EngineContext, Vec2};
|
use comfy::{EngineContext, Vec2};
|
||||||
use std::{ops::Sub, time::Instant};
|
use std::{
|
||||||
|
ops::{Add, Sub},
|
||||||
|
time::Instant
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Ghost {
|
pub struct Ghost {
|
||||||
|
@ -71,7 +74,8 @@ pub enum ZLayer {
|
||||||
ElectricLayer = -2,
|
ElectricLayer = -2,
|
||||||
MapMax = -1,
|
MapMax = -1,
|
||||||
Human = 0,
|
Human = 0,
|
||||||
Ghost = 1
|
Ghost = 1,
|
||||||
|
UI = 100
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ZLayer> for i32 {
|
impl From<ZLayer> for i32 {
|
||||||
|
@ -89,18 +93,68 @@ impl Sub<i32> for ZLayer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn setup(state: &mut State, ctx: &mut EngineContext<'_>) {
|
impl Add<i32> for ZLayer {
|
||||||
|
type Output = i32;
|
||||||
|
|
||||||
|
fn add(self, other: i32) -> Self::Output {
|
||||||
|
i32::from(self) + other
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn setup(_state: &mut State, ctx: &mut EngineContext<'_>) {
|
||||||
Assets::load(ctx);
|
Assets::load(ctx);
|
||||||
|
|
||||||
//house::setup(state, ctx);
|
//house::setup(state, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(state: &mut State, engine: &mut EngineContext<'_>) {
|
/// The amount of energy a ghost consumes idle.
|
||||||
state.score += engine.delta * 10.0;
|
pub const GHOST_DISCHARGE_RATE: f32 = 70.0;
|
||||||
|
/// The amount of energy additionally consumed by a moving ghost.
|
||||||
|
pub const GHOST_DISCHARGE_RATE_MOVEMENT: f32 = 70.0;
|
||||||
|
/// The amount of energy a house consumes idle.
|
||||||
|
pub const HOUSE_DISCHARGE_RATE: f32 = 30.0;
|
||||||
|
/// The amount of energy a ghost can charge when inside a house.
|
||||||
|
pub const GHOST_CHARGE_RATE: f32 = 200.0;
|
||||||
|
|
||||||
|
pub fn update(state: &mut State, ctx: &mut EngineContext<'_>) {
|
||||||
|
// Update the score. It's based on time.
|
||||||
|
state.score += ctx.delta * 10.0;
|
||||||
|
|
||||||
|
// Update the currently active activity.
|
||||||
match state.activity {
|
match state.activity {
|
||||||
Activity::House(_) => house::update(state, engine),
|
Activity::House(_) => house::update(state, ctx),
|
||||||
Activity::Overworld => overworld::update(state, engine)
|
Activity::Overworld => overworld::update(state, ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We update the charge of houses here - the charge will always decrease, even if the
|
||||||
|
// house is not the current activity.
|
||||||
|
for (house_pos, house) in &mut state.houses {
|
||||||
|
house.charge -= ctx.delta * HOUSE_DISCHARGE_RATE;
|
||||||
|
|
||||||
|
match state.activity {
|
||||||
|
Activity::House(pos) if *house_pos == pos => {
|
||||||
|
// The ghost is currently inside the house. Increase its discarge rate.
|
||||||
|
house.charge -= ctx.delta * GHOST_DISCHARGE_RATE;
|
||||||
|
if house.charge < 0.0 {
|
||||||
|
state.ghost.charge += house.charge;
|
||||||
|
house.charge = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// And possibly also charge the ghost when inside a house.
|
||||||
|
if state.ghost.charge < state.ghost.max_charge {
|
||||||
|
let charge_transfer = (ctx.delta * GHOST_CHARGE_RATE)
|
||||||
|
.min(state.ghost.max_charge - state.ghost.charge)
|
||||||
|
.min(house.charge);
|
||||||
|
state.ghost.charge += charge_transfer;
|
||||||
|
house.charge -= charge_transfer;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure the ghost's charge never drops below 0.
|
||||||
|
state.ghost.charge = state.ghost.charge.max(0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(state: &State, engine: &mut EngineContext<'_>) {
|
pub fn draw(state: &State, engine: &mut EngineContext<'_>) {
|
||||||
|
|
69
src/ui.rs
69
src/ui.rs
|
@ -1,42 +1,66 @@
|
||||||
use crate::State;
|
use crate::{game::ZLayer, State};
|
||||||
use comfy::{
|
use comfy::{
|
||||||
draw_rect, draw_rect_outline, egui, screen_height, screen_to_world, screen_width,
|
draw_rect, draw_rect_outline, egui, screen_height, screen_to_world, screen_width,
|
||||||
EngineContext, Vec2, BLUE, RED, WHITE
|
vec2, Color, EngineContext, Vec2, BLACK, BLUE, PURPLE, RED, WHITE
|
||||||
};
|
};
|
||||||
use egui::widget_text::RichText;
|
use egui::widget_text::RichText;
|
||||||
|
|
||||||
pub fn draw_batterie(state: &State, _engine: &EngineContext<'_>) {
|
// seperate fill state into smaller section for better readability
|
||||||
// seperate fill state into smaller section for better readability
|
const BATTERY_SECTION_COUNT: u8 = 5;
|
||||||
let section_count = 5;
|
const BATTERY_SECTION_WIDTH: f32 = 1.0;
|
||||||
let mut start_positon = screen_to_world(Vec2::new(screen_width(), screen_height()));
|
const BATTERY_SECTION_HEIGHT: f32 = 0.5;
|
||||||
|
|
||||||
|
fn draw_battery(mut start_position: Vec2, charge: f32, max_charge: f32, color: Color) {
|
||||||
// section size in world codinates
|
// section size in world codinates
|
||||||
let section_size = Vec2::new(0.5, 0.25);
|
let section_size = vec2(BATTERY_SECTION_WIDTH, BATTERY_SECTION_HEIGHT);
|
||||||
start_positon.x -= 0.5 * section_size.x + 0.5 * section_size.y;
|
start_position.x -= 0.5 * section_size.x + 0.5 * section_size.y;
|
||||||
start_positon.y += 0.5 * section_size.y + 0.5 * section_size.y;
|
start_position.y += 0.5 * section_size.y + 0.5 * section_size.y;
|
||||||
// draw fill level
|
// draw fill level
|
||||||
{
|
{
|
||||||
let ghost = &state.ghost;
|
let percent = charge / max_charge;
|
||||||
let percent = ghost.charge / ghost.max_charge;
|
|
||||||
let mut size = section_size;
|
let mut size = section_size;
|
||||||
size.y = section_size.y * section_count as f32 * percent;
|
size.y = section_size.y * BATTERY_SECTION_COUNT as f32;
|
||||||
let mut position = start_positon;
|
let mut position = start_position;
|
||||||
position.y += 0.5 * -section_size.y + 0.5 * size.y;
|
position.y += 0.5 * -section_size.y + 0.5 * size.y;
|
||||||
draw_rect(position, size, BLUE, 100);
|
draw_rect(position, size, BLACK, ZLayer::UI.into());
|
||||||
|
size.y *= percent;
|
||||||
|
let mut position = start_position;
|
||||||
|
position.y += 0.5 * -section_size.y + 0.5 * size.y;
|
||||||
|
draw_rect(position, size, BLUE, ZLayer::UI + 1);
|
||||||
}
|
}
|
||||||
// draw sections
|
// draw sections
|
||||||
for i in 0 .. section_count {
|
for i in 0 .. BATTERY_SECTION_COUNT {
|
||||||
let mut position = start_positon;
|
let mut position = start_position;
|
||||||
position.y += i as f32 * section_size.y;
|
position.y += i as f32 * section_size.y;
|
||||||
draw_rect_outline(position, section_size, 0.1, RED, 100);
|
draw_rect_outline(position, section_size, 0.1, color, ZLayer::UI + 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_highscore(state: &State, _engine: &EngineContext<'_>) {
|
pub fn draw_ghost_battery(state: &State) {
|
||||||
|
let start_position = screen_to_world(Vec2::new(screen_width(), screen_height()));
|
||||||
|
draw_battery(
|
||||||
|
start_position,
|
||||||
|
state.ghost.charge,
|
||||||
|
state.ghost.max_charge,
|
||||||
|
RED
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw_house_battery(state: &State) {
|
||||||
|
let Some(house) = state.house() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let mut start_position = screen_to_world(Vec2::new(screen_width(), screen_height()));
|
||||||
|
start_position.x -= 2.0 * BATTERY_SECTION_WIDTH;
|
||||||
|
draw_battery(start_position, house.charge, house.max_charge, PURPLE)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw_highscore(state: &State) {
|
||||||
egui::Area::new("score")
|
egui::Area::new("score")
|
||||||
.anchor(egui::Align2::RIGHT_TOP, egui::vec2(0.0, 0.0))
|
.anchor(egui::Align2::RIGHT_TOP, egui::vec2(0.0, 0.0))
|
||||||
.show(egui(), |ui| {
|
.show(egui(), |ui| {
|
||||||
ui.label(
|
ui.label(
|
||||||
RichText::new(format!("{:.0}", state.score))
|
RichText::new(format!("{:.0} ", state.score))
|
||||||
.color(WHITE)
|
.color(WHITE)
|
||||||
.monospace()
|
.monospace()
|
||||||
.size(16.0)
|
.size(16.0)
|
||||||
|
@ -45,7 +69,8 @@ pub fn draw_highscore(state: &State, _engine: &EngineContext<'_>) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(state: &State, engine: &EngineContext<'_>) {
|
pub fn draw(state: &State, _ctx: &EngineContext<'_>) {
|
||||||
draw_batterie(state, engine);
|
draw_ghost_battery(state);
|
||||||
draw_highscore(state, engine);
|
draw_house_battery(state);
|
||||||
|
draw_highscore(state);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue