mirror of
https://github.com/Noratrieb/game-wip-dontplay.git
synced 2026-01-16 04:25:00 +01:00
Add tile graphic
This commit is contained in:
parent
d78726eb5b
commit
fbc7e35f9e
11 changed files with 812 additions and 20 deletions
13
src/app.rs
13
src/app.rs
|
|
@ -3,20 +3,24 @@ use sfml::{
|
|||
window::Event,
|
||||
};
|
||||
|
||||
use crate::graphics;
|
||||
use crate::{game::GameState, graphics, res::Res};
|
||||
|
||||
/// Application level state (includes game and ui state, etc.)
|
||||
pub struct App {
|
||||
rw: RenderWindow,
|
||||
should_quit: bool,
|
||||
game: GameState,
|
||||
res: Res,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
pub fn new() -> anyhow::Result<Self> {
|
||||
Ok(Self {
|
||||
rw: graphics::make_window(),
|
||||
should_quit: false,
|
||||
}
|
||||
game: GameState::default(),
|
||||
res: Res::load()?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn do_game_loop(&mut self) {
|
||||
|
|
@ -40,6 +44,7 @@ impl App {
|
|||
|
||||
fn do_rendering(&mut self) {
|
||||
self.rw.clear(Color::BLACK);
|
||||
self.game.draw_world(&mut self.rw, &self.res);
|
||||
self.rw.display();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
28
src/game.rs
28
src/game.rs
|
|
@ -1,16 +1,22 @@
|
|||
use crate::{math::WorldPos, world::World};
|
||||
use sfml::graphics::{RenderTarget, RenderWindow, Sprite};
|
||||
|
||||
use crate::{math::WorldPos, res::Res, world::World};
|
||||
|
||||
pub struct GameState {
|
||||
transient: TransientGameState,
|
||||
persistent: PersistentGameState,
|
||||
}
|
||||
|
||||
/// Transient game state, not saved to disk
|
||||
pub struct TransientGameState {
|
||||
camera_offset: WorldPos,
|
||||
}
|
||||
|
||||
/// Persistent game state, saved to disk
|
||||
pub struct PersistentGameState {
|
||||
world: World,
|
||||
}
|
||||
impl GameState {
|
||||
pub(crate) fn draw_world(&mut self, rw: &mut RenderWindow, res: &Res) {
|
||||
rw.draw(&Sprite::with_texture(&res.tile_atlas));
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for GameState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
camera_offset: WorldPos { x: 0, y: 0 },
|
||||
world: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
15
src/main.rs
15
src/main.rs
|
|
@ -2,11 +2,22 @@ mod app;
|
|||
mod game;
|
||||
mod graphics;
|
||||
mod math;
|
||||
mod res;
|
||||
mod world;
|
||||
|
||||
use app::App;
|
||||
|
||||
fn main() {
|
||||
let mut app = App::new();
|
||||
fn try_main() -> anyhow::Result<()> {
|
||||
let mut app = App::new()?;
|
||||
app.do_game_loop();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if let Err(e) = try_main() {
|
||||
rfd::MessageDialog::new()
|
||||
.set_title("Fatal error")
|
||||
.set_description(&e.to_string())
|
||||
.show();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
pub type WorldPosScalar = i32;
|
||||
|
||||
pub struct WorldPos {
|
||||
x: WorldPosScalar,
|
||||
y: WorldPosScalar,
|
||||
pub x: WorldPosScalar,
|
||||
pub y: WorldPosScalar,
|
||||
}
|
||||
|
|
|
|||
13
src/res.rs
Normal file
13
src/res.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
use sfml::{graphics::Texture, SfBox};
|
||||
|
||||
pub struct Res {
|
||||
pub tile_atlas: SfBox<Texture>,
|
||||
}
|
||||
|
||||
impl Res {
|
||||
pub fn load() -> anyhow::Result<Self> {
|
||||
Ok(Self {
|
||||
tile_atlas: Texture::from_file("res/tiles.png")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
21
src/world.rs
21
src/world.rs
|
|
@ -1,4 +1,5 @@
|
|||
use fnv::FnvHashMap;
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
type ChunkPosScalar = i16;
|
||||
|
||||
|
|
@ -12,6 +13,14 @@ pub struct World {
|
|||
chunks: FnvHashMap<ChunkPos, Chunk>,
|
||||
}
|
||||
|
||||
impl Default for World {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
chunks: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const CHUNK_EXTENT: u16 = 256;
|
||||
const CHUNK_N_BLOCKS: usize = CHUNK_EXTENT as usize * CHUNK_EXTENT as usize;
|
||||
|
||||
|
|
@ -21,8 +30,20 @@ pub struct Chunk {
|
|||
blocks: ChunkBlocks,
|
||||
}
|
||||
|
||||
impl Chunk {
|
||||
pub fn new_rand() -> Self {
|
||||
let mut rng = thread_rng();
|
||||
let mut blocks = [Block { id: 0 }; CHUNK_N_BLOCKS];
|
||||
for b in &mut blocks {
|
||||
b.id = rng.gen();
|
||||
}
|
||||
Self { blocks }
|
||||
}
|
||||
}
|
||||
|
||||
type BlockId = u16;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Block {
|
||||
id: BlockId,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue