From 3f762fde47cbec9460ea5fb3d6f89dbf0bdaa33c Mon Sep 17 00:00:00 2001 From: hodasemi Date: Mon, 8 May 2023 20:56:50 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 + .vscode/settings.json | 7 ++ Cargo.toml | 12 +++ src/board.rs | 189 ++++++++++++++++++++++++++++++++++++++++++ src/game.rs | 53 ++++++++++++ src/main.rs | 17 ++++ 6 files changed, 280 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 Cargo.toml create mode 100644 src/board.rs create mode 100644 src/game.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..869df07 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5805f31 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "workbench.colorCustomizations": { + "activityBar.background": "#412902", + "titleBar.activeBackground": "#5B3903", + "titleBar.activeForeground": "#FFF9F1" + } +} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0851b5d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "mill_game" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +engine = { path = "../Gavania/engine", features = ["graphical"] } + +anyhow = { version = "1.0.70", features = ["backtrace"] } + diff --git a/src/board.rs b/src/board.rs new file mode 100644 index 0000000..3aa4698 --- /dev/null +++ b/src/board.rs @@ -0,0 +1,189 @@ +use std::sync::Arc; + +use anyhow::Result; +use engine::prelude::{ + cgmath::{vec3, Vector3}, + *, +}; + +pub struct Board { + board: Entity, +} + +impl Board { + pub fn new(engine: Arc, scene: &mut SceneHandle) -> Result> { + let mut board_entity = engine.assets().empty_entity(); + + let meshes = vec![Self::create_board_base(&engine)?]; + + let draw = Draw::new(meshes); + board_entity.insert_component(draw); + + let location = Location::from_entity(&mut board_entity); + board_entity.insert_component(location); + + let mut board = None; + scene.on_scene(|scene| { + board = Some(scene.add_entity(board_entity)?); + + Ok(()) + })?; + + Ok(Arc::new(Self { + board: board.unwrap(), + })) + } + + fn create_board_base(engine: &Arc) -> Result { + let mut board_base = AssetMesh::new( + engine.device(), + engine.settings().graphics_info()?.render_type, + )?; + + let vertex_buffer = Buffer::builder() + .set_data(&Self::create_cuboid( + vec3(-17.5, -20.0, -2.0), + vec3(35.0, 40.0, 2.0), + )) + .set_usage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT) + .set_memory_usage(MemoryUsage::CpuOnly) + .build(engine.device().clone())?; + + let color = Color::try_from("#b77b2b")?; + let a: [f32; 3] = color.into(); + + board_base.add_primitive( + vertex_buffer, + None, + None, + PrimitiveMaterial { + color: [a[0], a[1], a[2], 1.0], + metallic_factor: 0.2, + emissive_factor: [0.2, 0.2, 0.2], + roughness_factor: 0.8, + alpha_mode: AlphaMode::Opaque, + alpha_cut_off: 0.5, + }, + true, + )?; + + Ok(board_base) + } + + fn create_cuboid(offset: Vector3, extent: Vector3) -> Vec { + vec![ + // near + PositionNormal::new( + vec3(offset.x, offset.y + extent.y, offset.z + extent.z), + vec3(0.0, -1.0, 0.0), + ), + PositionNormal::new( + vec3( + offset.x + extent.x, + offset.y + extent.y, + offset.z + extent.z, + ), + vec3(0.0, -1.0, 0.0), + ), + PositionNormal::new( + vec3(offset.x, offset.y + extent.y, offset.z), + vec3(0.0, -1.0, 0.0), + ), + PositionNormal::new( + vec3(offset.x, offset.y + extent.y, offset.z), + vec3(0.0, -1.0, 0.0), + ), + PositionNormal::new( + vec3(offset.x, offset.y + extent.y, offset.z), + vec3(0.0, -1.0, 0.0), + ), + PositionNormal::new( + vec3(offset.x, offset.y + extent.y, offset.z + extent.z), + vec3(0.0, -1.0, 0.0), + ), + // right + PositionNormal::new( + vec3( + offset.x + extent.x, + offset.y + extent.y, + offset.z + extent.z, + ), + vec3(1.0, 0.0, 0.0), + ), + PositionNormal::new( + vec3(offset.x + extent.x, offset.y, offset.z + extent.z), + vec3(1.0, 0.0, 0.0), + ), + PositionNormal::new( + vec3(offset.x + extent.x, offset.y, offset.z), + vec3(1.0, 0.0, 0.0), + ), + PositionNormal::new( + vec3(offset.x + extent.x, offset.y, offset.z), + vec3(1.0, 0.0, 0.0), + ), + PositionNormal::new( + vec3(offset.x + extent.x, offset.y + extent.y, offset.z), + vec3(1.0, 0.0, 0.0), + ), + PositionNormal::new( + vec3( + offset.x + extent.x, + offset.y + extent.y, + offset.z + extent.z, + ), + vec3(1.0, 0.0, 0.0), + ), + // far + PositionNormal::new( + vec3(offset.x + extent.x, offset.y, offset.z + extent.z), + vec3(0.0, 1.0, 0.0), + ), + PositionNormal::new( + vec3(offset.x, offset.y, offset.z + extent.z), + vec3(0.0, 1.0, 0.0), + ), + PositionNormal::new(vec3(offset.x, offset.y, offset.z), vec3(0.0, 1.0, 0.0)), + PositionNormal::new(vec3(offset.x, offset.y, offset.z), vec3(0.0, 1.0, 0.0)), + PositionNormal::new( + vec3(offset.x + extent.x, offset.y, offset.z), + vec3(0.0, 1.0, 0.0), + ), + PositionNormal::new( + vec3(offset.x + extent.x, offset.y, offset.z + extent.z), + vec3(0.0, 1.0, 0.0), + ), + ] + } +} + +impl Map for Board { + fn name(&self) -> &str { + "MillBoard" + } + + fn check_walkability( + &self, + _position: cgmath::Vector2, + _direction: cgmath::Vector2, + _radius: f32, + ) -> Result { + Ok(true) + } + + fn get_height(&self, _x: f32, _y: f32) -> Result { + Ok(0.0) + } + + fn spawn_positions(&self) -> Result>> { + Ok(vec![]) + } + + fn leave_markers(&self) -> Result> { + Ok(vec![]) + } + + fn disable(&self, scene: &mut Scene) -> Result<()> { + todo!() + } +} diff --git a/src/game.rs b/src/game.rs new file mode 100644 index 0000000..8a95076 --- /dev/null +++ b/src/game.rs @@ -0,0 +1,53 @@ +use std::sync::Arc; + +use anyhow::Result; +use engine::prelude::*; + +use crate::board::Board; + +pub struct MillGame { + board: Arc, + scene: SceneHandle, +} + +impl MillGame { + pub fn new(engine: Arc) -> Result> { + let mut scene = SceneHandle::new(&engine)?; + + scene.activate()?; + + Ok(Arc::new(Self { + board: Board::new(engine, &mut scene)?, + scene, + })) + } +} + +impl EngineObject for MillGame { + fn name(&self) -> &str { + "MillGame" + } + + fn update(&self) -> Result<()> { + Ok(()) + } + + fn event(&self, event: EngineEvent) -> Result<()> { + match event { + EngineEvent::MouseMotion(_, _) => todo!(), + EngineEvent::MouseButtonDown(_) => todo!(), + EngineEvent::MouseButtonUp(_) => todo!(), + EngineEvent::MouseWheel(_, _, _) => todo!(), + EngineEvent::KeyDown(_) => todo!(), + EngineEvent::KeyUp(_) => todo!(), + EngineEvent::ButtonDown(_) => todo!(), + EngineEvent::ButtonUp(_) => todo!(), + EngineEvent::ControllerAxis(_) => todo!(), + EngineEvent::ControllerAdded(_) => todo!(), + EngineEvent::ControllerRemoved(_) => todo!(), + EngineEvent::FileDrop(_) => todo!(), + } + + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..70951ff --- /dev/null +++ b/src/main.rs @@ -0,0 +1,17 @@ +mod board; +mod game; + +use anyhow::Result; +use engine::prelude::*; +use game::MillGame; + +fn main() -> Result<()> { + let mut create_info = EngineCreateInfo::default(); + create_info.resource_base_path = "".to_string(); + + let engine = Engine::new(create_info)?; + + engine.set_game_object(Some(MillGame::new(engine.clone())?))?; + + engine.run() +}