Merge branch 'main' into battery-ui
This commit is contained in:
commit
b3b0bfb725
15 changed files with 1296 additions and 38 deletions
|
@ -1,7 +0,0 @@
|
|||
use comfy::*;
|
||||
|
||||
pub fn draw(_state: &crate::State, _engine: &comfy::EngineContext) {
|
||||
draw_circle(vec2(0.0, 0.0), 0.5, RED, 0);
|
||||
}
|
||||
|
||||
pub fn update(_state: &mut crate::State, _engine: &mut comfy::EngineContext) {}
|
69
src/activities/house/grid.rs
Normal file
69
src/activities/house/grid.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
use comfy::{draw_circle, draw_line, error, vec2, Vec2, BLUE};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Grid {
|
||||
pub nodes: Vec<Vec2>,
|
||||
pub connections: Vec<(usize, usize)>
|
||||
}
|
||||
|
||||
impl Default for Grid {
|
||||
fn default() -> Self {
|
||||
Grid::load() //Just for testing purposes
|
||||
}
|
||||
}
|
||||
|
||||
impl Grid {
|
||||
fn load() -> Self {
|
||||
let mut grid = Self {
|
||||
nodes: vec![
|
||||
vec2(10.0, 0.0),
|
||||
vec2(0.0, 0.0),
|
||||
vec2(0.0, 10.0),
|
||||
vec2(-10.0, 0.0),
|
||||
vec2(10.0, 10.0),
|
||||
],
|
||||
connections: vec![(0, 1), (1, 2), (1, 3), (0, 4), (0, 5), (5, 1), (6, 7)]
|
||||
};
|
||||
|
||||
grid.sanitize();
|
||||
|
||||
grid
|
||||
}
|
||||
|
||||
fn sanitize(&mut self) {
|
||||
let mut len = self.nodes.len();
|
||||
let connections = self
|
||||
.connections
|
||||
.iter()
|
||||
.filter(|(conn_i1, conn_i2)| {
|
||||
if conn_i1 >= &mut len || conn_i2 >= &mut len {
|
||||
error!("Connection in grid not possible {:?}", (conn_i1, conn_i2));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
})
|
||||
.map(|(conn_i1, conn_i2)| (*conn_i1, *conn_i2))
|
||||
.collect();
|
||||
|
||||
self.connections = connections;
|
||||
}
|
||||
|
||||
pub fn draw(&self) {
|
||||
//Draw Grid
|
||||
for node in &self.nodes {
|
||||
draw_circle(*node, 0.25, BLUE, 0);
|
||||
}
|
||||
for (conn_i1, conn_i2) in &self.connections {
|
||||
let node_1 = self.nodes.get(*conn_i1);
|
||||
let node_2 = self.nodes.get(*conn_i2);
|
||||
|
||||
if node_1.is_none() || node_2.is_none() {
|
||||
error!("Connection in grid not available {:?}", (conn_i1, conn_i2));
|
||||
continue;
|
||||
}
|
||||
|
||||
draw_line(*node_1.unwrap(), *node_2.unwrap(), 0.1, BLUE, 0);
|
||||
}
|
||||
}
|
||||
}
|
23
src/activities/house/mod.rs
Normal file
23
src/activities/house/mod.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
mod grid;
|
||||
mod player;
|
||||
|
||||
use grid::Grid;
|
||||
use player::Player;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct HouseState {
|
||||
grid: Grid,
|
||||
player: Player
|
||||
}
|
||||
|
||||
pub fn draw(state: &crate::State, _engine: &comfy::EngineContext<'_>) {
|
||||
//Draw Grid
|
||||
state.house.grid.draw();
|
||||
|
||||
//Draw Player
|
||||
state.house.player.draw();
|
||||
}
|
||||
|
||||
pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext<'_>) {
|
||||
state.house.player.update(&state.house.grid);
|
||||
}
|
268
src/activities/house/player.rs
Normal file
268
src/activities/house/player.rs
Normal file
|
@ -0,0 +1,268 @@
|
|||
use super::Grid;
|
||||
use comfy::{delta, draw_circle, is_key_down, vec2, KeyCode, Vec2, RED};
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
position: Vec2,
|
||||
speed: f32,
|
||||
connection: usize,
|
||||
next_connections: Vec<usize>
|
||||
}
|
||||
|
||||
impl Default for Player {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
position: Default::default(),
|
||||
speed: 10.0,
|
||||
connection: 0,
|
||||
next_connections: vec![1, 2, 3]
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Player {
|
||||
pub fn draw(&self) {
|
||||
draw_circle(self.position, 0.5, RED, 0);
|
||||
}
|
||||
pub fn update(&mut self, grid: &Grid) {
|
||||
let allowed_movement = get_allowed_movement(self, grid);
|
||||
|
||||
move_player(self, allowed_movement);
|
||||
|
||||
if !on_current_connection(self, grid) && !update_connections(self, grid) {
|
||||
snap_to_closest_node(self, grid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn move_player(player: &mut Player, allowed_movement: (bool, bool, bool, bool)) {
|
||||
let (
|
||||
allow_up_movement,
|
||||
allow_down_movement,
|
||||
allow_left_movement,
|
||||
allow_right_movement
|
||||
) = allowed_movement;
|
||||
|
||||
if allow_up_movement && is_key_down(KeyCode::Up) {
|
||||
player.position += vec2(0.0, player.speed) * delta();
|
||||
}
|
||||
|
||||
if allow_down_movement && is_key_down(KeyCode::Down) {
|
||||
player.position += vec2(0.0, -player.speed) * delta();
|
||||
}
|
||||
|
||||
if allow_right_movement && is_key_down(KeyCode::Right) {
|
||||
player.position += vec2(player.speed, 0.0) * delta();
|
||||
}
|
||||
|
||||
if allow_left_movement && is_key_down(KeyCode::Left) {
|
||||
player.position += vec2(-player.speed, 0.0) * delta();
|
||||
}
|
||||
}
|
||||
|
||||
//(UP, DOWN, LEFT, RIGHT)
|
||||
fn get_allowed_movement(player: &Player, grid: &Grid) -> (bool, bool, bool, bool) {
|
||||
let (conn_i1, conn_i2) = grid.connections.get(player.connection).unwrap();
|
||||
let node_1 = grid.nodes.get(*conn_i1).unwrap();
|
||||
let node_2 = grid.nodes.get(*conn_i2).unwrap();
|
||||
|
||||
let range = 0.25;
|
||||
|
||||
if in_node_range(&player.position, node_1, range)
|
||||
|| in_node_range(&player.position, node_2, range)
|
||||
{
|
||||
(true, true, true, true)
|
||||
} else {
|
||||
let (
|
||||
mut allow_up_movement,
|
||||
mut allow_down_movement,
|
||||
mut allow_left_movement,
|
||||
mut allow_right_movement
|
||||
) = get_allowed_connection_movement(&player.position, node_1, node_2);
|
||||
|
||||
for conn in &player.next_connections {
|
||||
let (next_conn_i1, next_conn_i2) = grid.connections.get(*conn).unwrap();
|
||||
let next_node_1 = grid.nodes.get(*next_conn_i1).unwrap();
|
||||
let next_node_2 = grid.nodes.get(*next_conn_i2).unwrap();
|
||||
|
||||
let (
|
||||
next_allow_up_movement,
|
||||
next_allow_down_movement,
|
||||
next_allow_left_movement,
|
||||
next_allow_right_movement
|
||||
) = get_allowed_connection_movement(&player.position, next_node_1, next_node_2);
|
||||
|
||||
(
|
||||
allow_up_movement,
|
||||
allow_down_movement,
|
||||
allow_left_movement,
|
||||
allow_right_movement
|
||||
) = (
|
||||
allow_up_movement || next_allow_up_movement,
|
||||
allow_down_movement || next_allow_down_movement,
|
||||
allow_left_movement || next_allow_left_movement,
|
||||
allow_right_movement || next_allow_right_movement
|
||||
);
|
||||
}
|
||||
(
|
||||
allow_up_movement,
|
||||
allow_down_movement,
|
||||
allow_left_movement,
|
||||
allow_right_movement
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
//(UP, DOWN, LEFT, RIGHT)
|
||||
fn get_allowed_connection_movement(
|
||||
player_position: &Vec2,
|
||||
node_1: &Vec2,
|
||||
node_2: &Vec2
|
||||
) -> (bool, bool, bool, bool) {
|
||||
let allow_left_movement = {
|
||||
if node_1.x <= node_2.x {
|
||||
node_1.x < player_position.x
|
||||
&& player_position.x <= node_2.x
|
||||
&& player_position.y == node_1.y
|
||||
} else {
|
||||
node_2.x < player_position.x
|
||||
&& player_position.x <= node_1.x
|
||||
&& player_position.y == node_1.y
|
||||
}
|
||||
};
|
||||
|
||||
let allow_right_movement = {
|
||||
if node_1.x <= node_2.x {
|
||||
node_1.x <= player_position.x
|
||||
&& player_position.x < node_2.x
|
||||
&& player_position.y == node_1.y
|
||||
} else {
|
||||
node_2.x <= player_position.x
|
||||
&& player_position.x < node_1.x
|
||||
&& player_position.y == node_1.y
|
||||
}
|
||||
};
|
||||
|
||||
let allow_up_movement = {
|
||||
if node_1.y <= node_2.y {
|
||||
node_1.y <= player_position.y
|
||||
&& player_position.y < node_2.y
|
||||
&& player_position.x == node_1.x
|
||||
} else {
|
||||
node_2.y <= player_position.y
|
||||
&& player_position.y < node_1.y
|
||||
&& player_position.x == node_1.x
|
||||
}
|
||||
};
|
||||
|
||||
let allow_down_movement = {
|
||||
if node_1.y <= node_2.y {
|
||||
node_1.y < player_position.y
|
||||
&& player_position.y <= node_2.y
|
||||
&& player_position.x == node_1.x
|
||||
} else {
|
||||
node_2.y < player_position.y
|
||||
&& player_position.y <= node_1.y
|
||||
&& player_position.x == node_1.x
|
||||
}
|
||||
};
|
||||
|
||||
(
|
||||
allow_up_movement,
|
||||
allow_down_movement,
|
||||
allow_left_movement,
|
||||
allow_right_movement
|
||||
)
|
||||
}
|
||||
|
||||
fn on_current_connection(player: &Player, grid: &Grid) -> bool {
|
||||
let (conn_i1, conn_i2) = grid.connections.get(player.connection).unwrap();
|
||||
let node_1 = grid.nodes.get(*conn_i1).unwrap();
|
||||
let node_2 = grid.nodes.get(*conn_i2).unwrap();
|
||||
|
||||
on_connection(&player.position, node_1, node_2)
|
||||
}
|
||||
|
||||
fn on_connection(player_position: &Vec2, node_1: &Vec2, node_2: &Vec2) -> bool {
|
||||
let on_x = if node_1.x <= node_2.x {
|
||||
node_1.x <= player_position.x && player_position.x <= node_2.x
|
||||
} else {
|
||||
node_2.x <= player_position.x && player_position.x <= node_1.x
|
||||
};
|
||||
|
||||
let on_y = if node_1.y <= node_2.y {
|
||||
node_1.y <= player_position.y && player_position.y <= node_2.y
|
||||
} else {
|
||||
node_2.y <= player_position.y && player_position.y <= node_1.y
|
||||
};
|
||||
|
||||
on_x && on_y
|
||||
}
|
||||
|
||||
fn update_connections(player: &mut Player, grid: &Grid) -> bool {
|
||||
for conn in &player.next_connections {
|
||||
let (next_conn_i1, next_conn_i2) = grid.connections.get(*conn).unwrap();
|
||||
let next_node_1 = grid.nodes.get(*next_conn_i1).unwrap();
|
||||
let next_node_2 = grid.nodes.get(*next_conn_i2).unwrap();
|
||||
|
||||
if on_connection(&player.position, next_node_1, next_node_2) {
|
||||
player.connection = *conn;
|
||||
//Update next connections
|
||||
let mut next_connections = Vec::new();
|
||||
for (i, (poss_conn_i1, poss_conn_i2)) in grid.connections.iter().enumerate() {
|
||||
if (next_conn_i1, next_conn_i2) != (poss_conn_i1, poss_conn_i2)
|
||||
&& next_conn_i1 == poss_conn_i1
|
||||
|| next_conn_i1 == poss_conn_i2
|
||||
|| next_conn_i2 == poss_conn_i1
|
||||
|| next_conn_i2 == poss_conn_i2
|
||||
{
|
||||
next_connections.push(i);
|
||||
}
|
||||
}
|
||||
player.next_connections = next_connections;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn in_node_range(player_position: &Vec2, node: &Vec2, range: f32) -> bool {
|
||||
node.x - range <= player_position.x
|
||||
&& player_position.x <= node.x + range
|
||||
&& node.y - range <= player_position.y
|
||||
&& player_position.y <= node.y + range
|
||||
}
|
||||
|
||||
fn get_squared_node_range(player_position: &Vec2, node: &Vec2) -> f32 {
|
||||
(player_position.x - node.x).abs() + (player_position.y - node.y).abs()
|
||||
}
|
||||
|
||||
fn snap_to_closest_node(player: &mut Player, grid: &Grid) {
|
||||
let (current_connection_index_1, current_connection_index_2) =
|
||||
grid.connections.get(player.connection).unwrap();
|
||||
|
||||
let mut nodes = HashSet::new();
|
||||
nodes.insert(current_connection_index_1);
|
||||
nodes.insert(current_connection_index_2);
|
||||
for conn in &player.next_connections {
|
||||
let (next_connection_index_1, next_connection_index_2) =
|
||||
grid.connections.get(*conn).unwrap();
|
||||
|
||||
nodes.insert(next_connection_index_1);
|
||||
nodes.insert(next_connection_index_2);
|
||||
}
|
||||
|
||||
let mut closest_sqared_range = f32::MAX;
|
||||
let mut closest_node = &vec2(0.0, 0.0);
|
||||
for node_index in nodes {
|
||||
let current_node = grid.nodes.get(*node_index).unwrap();
|
||||
let current_squard_range = get_squared_node_range(&player.position, current_node);
|
||||
if closest_sqared_range > current_squard_range {
|
||||
closest_sqared_range = current_squard_range;
|
||||
closest_node = current_node;
|
||||
}
|
||||
}
|
||||
|
||||
player.position = *closest_node;
|
||||
}
|
|
@ -1,8 +1,10 @@
|
|||
pub mod house;
|
||||
pub mod overworld;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Default)]
|
||||
pub enum Activity {
|
||||
#[allow(dead_code)]
|
||||
House,
|
||||
#[default]
|
||||
Overworld
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
use comfy::*;
|
||||
|
||||
pub fn draw(_state: &crate::State, _engine: &comfy::EngineContext) {
|
||||
draw_circle(vec2(0.0, 0.0), 0.5, GREEN, 0);
|
||||
}
|
||||
|
||||
pub fn update(_state: &mut crate::State, _engine: &mut comfy::EngineContext) {}
|
31
src/activities/overworld/mod.rs
Normal file
31
src/activities/overworld/mod.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
use comfy::{
|
||||
draw_rect_outline, draw_sprite, main_camera_mut, EngineContext, IVec2, Vec2, RED,
|
||||
WHITE
|
||||
};
|
||||
|
||||
use crate::game::ZLayer;
|
||||
|
||||
pub mod worldgen;
|
||||
|
||||
pub fn draw(state: &crate::State, _engine: &comfy::EngineContext<'_>) {
|
||||
for (coords, tile) in state.overworld.iter_tiles() {
|
||||
for (i, texture) in tile.textures().iter().rev().enumerate() {
|
||||
let i = i as i32;
|
||||
draw_sprite(
|
||||
*texture,
|
||||
coords.as_vec2(),
|
||||
WHITE,
|
||||
ZLayer::MapMax - i,
|
||||
Vec2::ONE
|
||||
);
|
||||
draw_rect_outline(coords.as_vec2(), Vec2::ONE, 0.1, RED, 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
316
src/activities/overworld/worldgen.rs
Normal file
316
src/activities/overworld/worldgen.rs
Normal file
|
@ -0,0 +1,316 @@
|
|||
// TODO remove this
|
||||
#![allow(dead_code)]
|
||||
|
||||
use crate::assets::ASSETS;
|
||||
use comfy::{IVec2, TextureHandle, UVec2};
|
||||
use log::info;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub enum MovementCost {
|
||||
/// No movement possible - cost infinitely high.
|
||||
Infinite,
|
||||
/// There is a path for this movement - movement is cheap.
|
||||
Path,
|
||||
/// There is no path and no obstacle.
|
||||
Default,
|
||||
/// There is an obstacle (i.e. fence) - movement is expensive.
|
||||
Obstacle
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum Tile {
|
||||
Grass,
|
||||
|
||||
Path {
|
||||
left: bool,
|
||||
right: bool,
|
||||
top: bool,
|
||||
bottom: bool
|
||||
},
|
||||
|
||||
House {
|
||||
texture: TextureHandle,
|
||||
door: bool
|
||||
}
|
||||
}
|
||||
|
||||
impl Tile {
|
||||
pub fn textures(&self) -> Vec<TextureHandle> {
|
||||
match self {
|
||||
Self::Grass => vec![ASSETS.overworld.grass],
|
||||
|
||||
Self::Path {
|
||||
left,
|
||||
right,
|
||||
top,
|
||||
bottom
|
||||
} => {
|
||||
let path_texture = match (left, right, top, bottom) {
|
||||
(true, true, false, false) => ASSETS.overworld.path_horiz,
|
||||
(false, false, true, true) => ASSETS.overworld.path_vert,
|
||||
|
||||
(true, false, true, false) => ASSETS.overworld.path_top_left,
|
||||
(true, false, false, true) => ASSETS.overworld.path_bottom_left,
|
||||
(false, true, true, false) => ASSETS.overworld.path_top_right,
|
||||
(false, true, false, true) => ASSETS.overworld.path_bottom_right,
|
||||
|
||||
(true, true, true, false) => ASSETS.overworld.path_top_left_right,
|
||||
(true, true, false, true) => ASSETS.overworld.path_bottom_left_right,
|
||||
(true, false, true, true) => ASSETS.overworld.path_top_left_bottom,
|
||||
(false, true, true, true) => ASSETS.overworld.path_top_right_bottom,
|
||||
|
||||
(true, true, true, true) => ASSETS.overworld.path_crossing,
|
||||
|
||||
(true, false, false, false)
|
||||
| (false, true, false, false)
|
||||
| (false, false, true, false)
|
||||
| (false, false, false, true) => panic!("We don't have no dead ends"),
|
||||
(false, false, false, false) => panic!("I think you meant grass?!?")
|
||||
};
|
||||
|
||||
vec![ASSETS.overworld.grass, path_texture]
|
||||
},
|
||||
|
||||
Self::House { texture, .. } => {
|
||||
vec![ASSETS.overworld.grass, *texture]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn can_stand_inside(&self) -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
pub fn movement_cost_left(&self) -> MovementCost {
|
||||
unimplemented!()
|
||||
}
|
||||
pub fn movement_cost_right(&self) -> MovementCost {
|
||||
unimplemented!()
|
||||
}
|
||||
pub fn movement_cost_up(&self) -> MovementCost {
|
||||
unimplemented!()
|
||||
}
|
||||
pub fn movement_cost_down(&self) -> MovementCost {
|
||||
unimplemented!()
|
||||
}
|
||||
pub fn can_enter_house(&self) -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
/// 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;
|
||||
|
||||
/// Chunks
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
pub struct Chunk {
|
||||
/// All tiles within this chunk.
|
||||
tiles: [Tile; (CHUNK_SIZE * CHUNK_SIZE) as usize],
|
||||
/// All paths that leave this chunk on the left hand side.
|
||||
paths_left: Vec<u32>,
|
||||
/// All paths that leave this chunk on the right hand side.
|
||||
paths_right: Vec<u32>,
|
||||
/// All paths that leave this chunk on the bottom side.
|
||||
paths_bottom: Vec<u32>,
|
||||
/// All paths that leave this chunk on the top side.
|
||||
paths_top: Vec<u32>
|
||||
}
|
||||
|
||||
impl Chunk {
|
||||
pub fn generate_chunk() -> Self {
|
||||
info!("gen chunk");
|
||||
// TODO real worldgen
|
||||
// for the time being we just copy this pre-made house block into the chunk
|
||||
|
||||
fn path(left: bool, right: bool, top: bool, bottom: bool) -> Tile {
|
||||
Tile::Path {
|
||||
left,
|
||||
right,
|
||||
top,
|
||||
bottom
|
||||
}
|
||||
}
|
||||
|
||||
fn house(texture: TextureHandle) -> Tile {
|
||||
Tile::House {
|
||||
texture,
|
||||
door: false
|
||||
}
|
||||
}
|
||||
|
||||
let path_horiz = path(true, true, false, false);
|
||||
let path_vert = path(false, false, true, true);
|
||||
let path_crossing = path(true, true, true, true);
|
||||
let grass = Tile::Grass;
|
||||
|
||||
let block = [
|
||||
[
|
||||
path_crossing,
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path_crossing
|
||||
],
|
||||
[
|
||||
path_vert,
|
||||
grass,
|
||||
grass,
|
||||
grass,
|
||||
house(ASSETS.overworld.house_roof_top),
|
||||
grass,
|
||||
grass,
|
||||
grass,
|
||||
grass,
|
||||
grass,
|
||||
path_vert
|
||||
],
|
||||
[
|
||||
path_vert,
|
||||
grass,
|
||||
grass,
|
||||
house(ASSETS.overworld.house_roof_mid_left),
|
||||
house(ASSETS.overworld.house_mid_window),
|
||||
house(ASSETS.overworld.house_roof_mid_right),
|
||||
grass,
|
||||
grass,
|
||||
grass,
|
||||
grass,
|
||||
path_vert
|
||||
],
|
||||
[
|
||||
path_vert,
|
||||
grass,
|
||||
house(ASSETS.overworld.house_roof_left),
|
||||
house(ASSETS.overworld.house_mid_window),
|
||||
house(ASSETS.overworld.house_mid),
|
||||
house(ASSETS.overworld.house_mid_window),
|
||||
house(ASSETS.overworld.house_roof_right),
|
||||
grass,
|
||||
grass,
|
||||
grass,
|
||||
path_vert
|
||||
],
|
||||
[
|
||||
path_vert,
|
||||
grass,
|
||||
house(ASSETS.overworld.house_bottom_left),
|
||||
house(ASSETS.overworld.house_bottom_door),
|
||||
house(ASSETS.overworld.house_bottom_window),
|
||||
house(ASSETS.overworld.house_bottom_window),
|
||||
house(ASSETS.overworld.house_bottom_right),
|
||||
grass,
|
||||
grass,
|
||||
grass,
|
||||
path_vert
|
||||
],
|
||||
[
|
||||
path_vert,
|
||||
grass,
|
||||
grass,
|
||||
path(false, true, true, false),
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path_horiz,
|
||||
path(true, false, true, true)
|
||||
],
|
||||
[
|
||||
path_vert, grass, grass, grass, grass, grass, grass, grass, grass, grass,
|
||||
path_vert
|
||||
],
|
||||
[
|
||||
path_vert, grass, grass, grass, grass, grass, grass, grass, grass, grass,
|
||||
path_vert
|
||||
]
|
||||
];
|
||||
|
||||
// and then we copy this pre-made block into our chunk
|
||||
let mut tiles = [Tile::Grass; (CHUNK_SIZE * CHUNK_SIZE) as usize];
|
||||
for y in 0 .. CHUNK_SIZE {
|
||||
for x in 0 .. CHUNK_SIZE {
|
||||
let row = block[y as usize % block.len()];
|
||||
tiles[(y * CHUNK_SIZE + x) as usize] = row[x as usize % row.len()];
|
||||
}
|
||||
}
|
||||
Self {
|
||||
tiles,
|
||||
paths_left: vec![],
|
||||
paths_right: vec![],
|
||||
paths_bottom: vec![],
|
||||
paths_top: vec![]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_tile(&self, local_chunk_coords: UVec2) -> Option<&Tile> {
|
||||
self.tiles
|
||||
.get((local_chunk_coords.y * CHUNK_SIZE + local_chunk_coords.x) as usize)
|
||||
}
|
||||
|
||||
/// iterate over all tiles and its local chunk coords
|
||||
pub fn iter_tiles(&self) -> impl Iterator<Item = (UVec2, &Tile)> {
|
||||
self.tiles.iter().enumerate().map(|(i, tile)| {
|
||||
let i = i as u32;
|
||||
(UVec2::new(i % CHUNK_SIZE, i / CHUNK_SIZE), tile)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Overworld {
|
||||
chunks: HashMap<IVec2, Chunk>
|
||||
}
|
||||
|
||||
fn world_to_chunk_and_local_coords(world_coords: IVec2) -> (IVec2, UVec2) {
|
||||
let chunk_coords = IVec2 {
|
||||
x: world_coords.x.div_euclid(CHUNK_SIZE as _),
|
||||
y: world_coords.y.div_euclid(CHUNK_SIZE as _)
|
||||
};
|
||||
let local_chunk_coords = UVec2 {
|
||||
x: world_coords.x.rem_euclid(CHUNK_SIZE as _) as _,
|
||||
y: world_coords.y.rem_euclid(CHUNK_SIZE as _) as _
|
||||
};
|
||||
(chunk_coords, local_chunk_coords)
|
||||
}
|
||||
|
||||
impl Overworld {
|
||||
/// Return a [`Tile`] at the given world coordinates, or `None` if that tile has not
|
||||
/// been generated yet.
|
||||
pub fn get_tile(&self, world_coords: IVec2) -> Option<&Tile> {
|
||||
let (chunk_coords, local_chunk_coords) =
|
||||
world_to_chunk_and_local_coords(world_coords);
|
||||
|
||||
let chunk = self.chunks.get(&chunk_coords)?;
|
||||
chunk.get_tile(local_chunk_coords)
|
||||
}
|
||||
|
||||
pub fn get_or_generate_tile(&mut self, world_coords: IVec2) -> &Tile {
|
||||
let (chunk_coords, local_chunk_coords) =
|
||||
world_to_chunk_and_local_coords(world_coords);
|
||||
|
||||
let chunk = self
|
||||
.chunks
|
||||
.entry(chunk_coords)
|
||||
.or_insert_with(Chunk::generate_chunk);
|
||||
chunk.get_tile(local_chunk_coords).unwrap()
|
||||
}
|
||||
|
||||
/// iterate over all tiles and its global coords
|
||||
pub fn iter_tiles(&self) -> impl Iterator<Item = (IVec2, &Tile)> {
|
||||
self.chunks.iter().flat_map(|(chunk_coords, chunk)| {
|
||||
chunk.iter_tiles().map(|(local_coords, tile)| {
|
||||
// never fail because chunksize fits alswas into i32
|
||||
let local_coords: IVec2 = local_coords.try_into().unwrap();
|
||||
(local_coords + (*chunk_coords * CHUNK_SIZE as i32), tile)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
29
src/game.rs
29
src/game.rs
|
@ -1,9 +1,9 @@
|
|||
use comfy::*;
|
||||
|
||||
use crate::{
|
||||
activities::{house, overworld, Activity},
|
||||
State
|
||||
};
|
||||
use comfy::EngineContext;
|
||||
use std::ops::Sub;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Gost {
|
||||
|
@ -21,15 +21,36 @@ impl Default for Gost {
|
|||
}
|
||||
}
|
||||
}
|
||||
#[repr(i32)]
|
||||
pub enum ZLayer {
|
||||
MapMax = -1,
|
||||
Human = 0,
|
||||
Ghost = 1
|
||||
}
|
||||
|
||||
pub fn update(state: &mut State, engine: &mut EngineContext) {
|
||||
impl From<ZLayer> for i32 {
|
||||
fn from(value: ZLayer) -> Self {
|
||||
// safe because #[repr(i32)]
|
||||
value as i32
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<i32> for ZLayer {
|
||||
type Output = i32;
|
||||
|
||||
fn sub(self, other: i32) -> Self::Output {
|
||||
i32::from(self) - other
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(state: &mut State, engine: &mut EngineContext<'_>) {
|
||||
match state.activity {
|
||||
Activity::House => house::update(state, engine),
|
||||
Activity::Overworld => overworld::update(state, engine)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(state: &State, engine: &EngineContext) {
|
||||
pub fn draw(state: &State, engine: &EngineContext<'_>) {
|
||||
match state.activity {
|
||||
Activity::House => house::draw(state, engine),
|
||||
Activity::Overworld => overworld::draw(state, engine)
|
||||
|
|
44
src/main.rs
44
src/main.rs
|
@ -1,26 +1,35 @@
|
|||
#![warn(rust_2018_idioms)]
|
||||
#![deny(clippy::wildcard_imports)]
|
||||
#![forbid(elided_lifetimes_in_paths, unsafe_code)]
|
||||
|
||||
mod assets {
|
||||
include!(env!("ASSETS_RS"));
|
||||
}
|
||||
|
||||
mod activities;
|
||||
mod game;
|
||||
mod ui;
|
||||
|
||||
use activities::Activity;
|
||||
use comfy::*;
|
||||
use self::{
|
||||
activities::{house::HouseState, overworld::worldgen::Overworld, Activity},
|
||||
assets::Assets
|
||||
use game::Gost;
|
||||
};
|
||||
use comfy::{
|
||||
init_game_config, pollster, run_comfy_main_async, EngineContext, EngineState,
|
||||
GameConfig, GameLoop
|
||||
};
|
||||
|
||||
const GAME_NAME: &str = "Powercreep";
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Default)]
|
||||
struct State {
|
||||
setup_called: bool,
|
||||
activity: Activity,
|
||||
activity: Activity,
|
||||
gohst: Gost
|
||||
}
|
||||
|
||||
impl Default for State {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
activity: Activity::House,
|
||||
gohst: Default::default()
|
||||
}
|
||||
}
|
||||
overworld: Overworld,
|
||||
house: HouseState
|
||||
}
|
||||
|
||||
impl GameLoop for State {
|
||||
|
@ -28,7 +37,12 @@ impl GameLoop for State {
|
|||
Self::default()
|
||||
}
|
||||
|
||||
fn update(&mut self, engine: &mut EngineContext) {
|
||||
fn update(&mut self, engine: &mut EngineContext<'_>) {
|
||||
if !self.setup_called {
|
||||
setup(engine);
|
||||
self.setup_called = true;
|
||||
}
|
||||
|
||||
game::update(self, engine);
|
||||
game::draw(self, engine);
|
||||
}
|
||||
|
@ -38,6 +52,10 @@ fn config(config: GameConfig) -> GameConfig {
|
|||
config
|
||||
}
|
||||
|
||||
fn setup(ctx: &mut EngineContext<'_>) {
|
||||
Assets::load(ctx);
|
||||
}
|
||||
|
||||
async fn run() {
|
||||
init_game_config(GAME_NAME.to_string(), env!("CARGO_PKG_VERSION"), config);
|
||||
let mut engine = EngineState::new();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue