gameobjects
This commit is contained in:
47
src/gameobject.rs
Normal file
47
src/gameobject.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use cgmath::{Matrix4, SquareMatrix, Vector3, vec3};
|
||||
use crate::vulkan::{MeshHandle, VulkanRenderer};
|
||||
use crate::input::InputState;
|
||||
|
||||
pub struct GameObject {
|
||||
pub mesh_index: usize,
|
||||
pub texture_index: usize,
|
||||
pub model_matrix: Matrix4<f32>,
|
||||
}
|
||||
|
||||
impl GameObject {
|
||||
pub fn new(mesh: MeshHandle, texture_index: usize) -> GameObject {
|
||||
GameObject { mesh_index: mesh, texture_index, model_matrix: Matrix4::identity() }
|
||||
}
|
||||
|
||||
pub fn set_position(&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;
|
||||
}
|
||||
|
||||
pub fn get_position(&self) -> Vector3<f32> {
|
||||
vec3(self.model_matrix.w.x, self.model_matrix.w.y, self.model_matrix.w.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;
|
||||
}
|
||||
|
||||
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 struct GameObjectHandle {
|
||||
pub object_index: usize
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user