diff --git a/.idea/runConfigurations/debug.xml b/.idea/runConfigurations/debug.xml
index f8c23cb..780dcc7 100644
--- a/.idea/runConfigurations/debug.xml
+++ b/.idea/runConfigurations/debug.xml
@@ -9,7 +9,7 @@
-
+
diff --git a/src/gameobject.rs b/src/gameobject.rs
index 4b7ad5b..382e098 100644
--- a/src/gameobject.rs
+++ b/src/gameobject.rs
@@ -1,51 +1,57 @@
-use cgmath::{Matrix4, SquareMatrix, Vector3, vec3};
+use cgmath::{Matrix4, Vector3, Quaternion, Euler, Deg};
use crate::vulkan::{MeshHandle, VulkanRenderer};
use crate::input::InputState;
+#[derive(Debug, Clone)]
pub struct GameObject {
pub mesh_index: usize,
pub texture_index: usize,
- pub model_matrix: Matrix4,
+ pub position: Vector3,
+ pub rotation: Quaternion,
+ pub scale: Vector3,
}
impl GameObject {
pub fn new(mesh: MeshHandle, texture_index: usize) -> GameObject {
- GameObject { mesh_index: mesh, texture_index, model_matrix: Matrix4::identity() }
+ GameObject { mesh_index: mesh, texture_index, position: Vector3::new(0.0, 0.0, 0.0),
+ rotation: Quaternion::new(1.0, 0.0, 0.0, 0.0), scale: Vector3::new(1.0, 1.0, 1.0) }
}
- pub fn set_position(&mut self, pos: (f32, f32, f32)) {
- self.model_matrix.w.x = pos.0;
- self.model_matrix.w.y = pos.1;
- self.model_matrix.w.z = pos.2;
+ pub fn set_position(&mut self, x: f32, y: f32, z: f32) {
+ self.position.x = x;
+ self.position.y = y;
+ self.position.z = z;
}
- pub fn _get_position(&self) -> Vector3 {
- vec3(self.model_matrix.w.x, self.model_matrix.w.y, self.model_matrix.w.z)
+ pub fn set_scale(&mut self, x: f32, y: f32, z: f32) {
+ self.scale.x = x;
+ self.scale.y = y;
+ self.scale.z = z;
}
- pub fn set_scale(&mut self, scale: (f32, f32, f32)) {
- self.model_matrix.x.x = scale.0;
- self.model_matrix.y.y = scale.1;
- self.model_matrix.z.z = scale.2;
- }
-
- pub fn _get_scale(&mut self) -> Vector3 {
- vec3(self.model_matrix.x.x, self.model_matrix.y.y, self.model_matrix.z.z)
+ pub fn set_rotation(&mut self, euler_x: f32, euler_y: f32, euler_z: f32) {
+ self.rotation = Quaternion::from(Euler::new(Deg(euler_x), Deg(euler_y), Deg(euler_z)));
}
pub fn translate(&mut self, x: f32, y: f32, z: f32) {
- self.model_matrix.w.x += x;
- self.model_matrix.w.y += y;
- self.model_matrix.w.z += z;
+ self.position.x += x;
+ self.position.y += y;
+ self.position.z += z;
}
- pub fn update(&mut self, delta_time: f32, input: &InputState) {
- if input.button_down("test") {
- self.translate(delta_time as f32, 0.0, 0.0);
- }
+ pub fn rotate(&mut self, x: f32, y: f32, z: f32) {
+ self.rotation = self.rotation * Quaternion::from(Euler::new(Deg(x), Deg(y), Deg(z)));
+ }
+
+ pub fn get_model_matrix(&self) -> Matrix4 {
+ let translation = Matrix4::from_translation(self.position);
+ let rotation: Matrix4 = self.rotation.into();
+ let scale = Matrix4::from_nonuniform_scale(self.scale.x, self.scale.y, self.scale.z);
+ translation * rotation * scale
}
}
+#[derive(Debug, Clone)]
pub struct GameObjectHandle {
pub object_index: usize
}
@@ -54,4 +60,28 @@ impl GameObjectHandle {
pub fn get_game_object<'a>(&mut self, renderer: &'a mut VulkanRenderer) -> Option<&'a mut GameObject> {
renderer.game_data.game_objects.get_mut(self.object_index)
}
+}
+
+pub trait Component {
+ fn update(&mut self, delta_time: f32, input: &InputState, renderer: &mut VulkanRenderer);
+ fn add_game_object(&mut self, handle: GameObjectHandle);
+}
+
+#[derive(Debug)]
+pub struct TestComponent {
+ pub game_objects: Vec
+}
+
+impl Component for TestComponent {
+ fn update(&mut self, delta_time: f32, input: &InputState, renderer: &mut VulkanRenderer) {
+ self.game_objects.iter_mut().for_each(|handle| {
+ if input.button_down("test") {
+ handle.get_game_object(renderer).unwrap().rotate(90.0 * delta_time, 90.0 * delta_time, 90.0 * delta_time);
+ }
+ });
+ }
+
+ fn add_game_object(&mut self, handle: GameObjectHandle) {
+ self.game_objects.push(handle);
+ }
}
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index 8cb5bee..c04a400 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,7 +5,7 @@ use crate::config::LogConfig;
use crate::input::InputState;
use crate::vulkan::{Game, LinePoint, MeshHandle, VulkanRenderer};
use crate::vulkan::vs::ty::ObjectUniformData;
-use crate::gameobject::{GameObject, GameObjectHandle};
+use crate::gameobject::{GameObject, GameObjectHandle, Component, TestComponent};
mod vulkan;
mod input;
@@ -22,6 +22,7 @@ struct TestGame {
log_config: LogConfig,
texture_index_counter: usize,
last_time: f32,
+ components: Vec>,
}
impl Game for TestGame {
@@ -35,9 +36,9 @@ impl Game for TestGame {
let frame_time = time - self.last_time;
let input = &self.input;
- let objects = &mut self.game_objects;
- objects.iter_mut().for_each(|game_object| {
- game_object.get_game_object(renderer).unwrap().update(frame_time, &input);
+ let components = &mut self.components;
+ components.iter_mut().for_each(|component| {
+ component.update(frame_time, &input, renderer);
});
// User interaction
@@ -45,6 +46,10 @@ impl Game for TestGame {
renderer.game_data.shutdown = true;
}
+ if self.input.button_just_released("test") {
+ println!("{:?}", self.game_objects[0].get_game_object(renderer).unwrap().position);
+ }
+
if self.input.button_just_pressed("reload_shaders") {
renderer.game_data.recreate_pipeline = true;
}
@@ -54,8 +59,7 @@ impl Game for TestGame {
}
let light_pos = vec3(f32::sin(time) * 2.0 + 2.0, f32::cos(time) * 2.0 + 2.0, 2.0);
- self.game_objects[2].get_game_object(renderer).unwrap().set_position(light_pos.into());
-
+ self.game_objects[2].get_game_object(renderer).unwrap().position = light_pos;
self.cam_rotation = self.cam_rotation * Quaternion::from_angle_y(Deg(self.input.get_axis("look_horizontal") * 0.05));
self.cam_rotation = Quaternion::from_angle_x(Deg(self.input.get_axis("look_vertical") * 0.05)) * self.cam_rotation;
@@ -100,15 +104,22 @@ fn _matrix_vector_mul(matrix: &Matrix4, vector: &Vector3) -> Vector3