mirror of
https://github.com/Mercantec-GHC/h5-projekt-mst.git
synced 2026-08-26 21:27:38 +02:00
draw skateboard
This commit is contained in:
parent
eb32577ae0
commit
ad332fb309
@ -72,6 +72,14 @@ impl Mul<f64> for V3 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<Self> for V3 {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, rhs: Self) -> Self::Output {
|
||||
Self(self.0 * rhs.0, self.1 * rhs.1, self.2 * rhs.2)
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<&Self> for V3 {
|
||||
type Output = Self;
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::{
|
||||
f64::consts::PI,
|
||||
sync::{Arc, Mutex},
|
||||
time::Duration,
|
||||
};
|
||||
@ -58,7 +59,7 @@ impl<R: Renderer> engine::Game<R> for Game {
|
||||
|
||||
fn render(&mut self, r: &mut R) {
|
||||
let mut scene = Scene::new();
|
||||
for object in &mut self.objects {
|
||||
for object in &self.objects {
|
||||
object.render(&mut scene);
|
||||
}
|
||||
scene.render(r, V3(0.0, 0.0, -1.0));
|
||||
@ -73,19 +74,63 @@ struct Object {
|
||||
}
|
||||
|
||||
enum ObjectKind {
|
||||
Player { pos: V3, vel: V3 },
|
||||
SkateBoard { pos: V3, vel: V3, rot: V3 },
|
||||
Obstacle { pos: V3, vel: V3 },
|
||||
Ground { pos: V3, vel_z: f64 },
|
||||
}
|
||||
|
||||
struct MultiShapeShape {
|
||||
shape: Shape,
|
||||
offset: V3,
|
||||
}
|
||||
|
||||
struct MultiShape {
|
||||
shapes: Vec<MultiShapeShape>,
|
||||
}
|
||||
|
||||
impl MultiShape {
|
||||
pub fn new(shapes: Vec<MultiShapeShape>) -> Self {
|
||||
Self { shapes }
|
||||
}
|
||||
pub fn rotate(mut self, rot: V3) -> Self {
|
||||
for shape in &mut self.shapes {
|
||||
shape.shape = shape
|
||||
.shape
|
||||
.translate(shape.offset)
|
||||
.rotate(rot)
|
||||
.translate(shape.offset * -1.0);
|
||||
}
|
||||
self
|
||||
}
|
||||
pub fn translate(mut self, offset: V3) -> Self {
|
||||
for shape in &mut self.shapes {
|
||||
shape.shape = shape.shape.translate(offset);
|
||||
}
|
||||
self
|
||||
}
|
||||
pub fn draw(self, pos: V3, scene: &mut Scene, outline_color: Color, fill_color: Color) {
|
||||
for shape in self.shapes {
|
||||
scene.draw_shape(
|
||||
pos,
|
||||
&shape.shape.translate(shape.offset),
|
||||
outline_color,
|
||||
fill_color,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Object {
|
||||
fn update(&mut self, delta_time: Duration) {
|
||||
match &mut self.kind {
|
||||
ObjectKind::Player { pos, vel } => {
|
||||
ObjectKind::SkateBoard { pos, vel, rot } => {
|
||||
*pos += *vel * delta_time.as_secs_f64();
|
||||
if pos.0 >= 2.1 {
|
||||
pos.0 = -2.0;
|
||||
}
|
||||
// rot.0 += delta_time.as_secs_f64() * PI * 1.0;
|
||||
rot.1 += delta_time.as_secs_f64() * PI * 1.0;
|
||||
// rot.2 += delta_time.as_secs_f64() * PI * 2.0;
|
||||
}
|
||||
ObjectKind::Obstacle { pos, vel } => {
|
||||
*pos += *vel * delta_time.as_secs_f64();
|
||||
@ -96,19 +141,30 @@ impl Object {
|
||||
}
|
||||
}
|
||||
|
||||
fn render(&mut self, scene: &mut Scene) {
|
||||
match &mut self.kind {
|
||||
ObjectKind::Player { pos, .. } => {
|
||||
scene.draw_shape(
|
||||
*pos,
|
||||
&Shape::new_cube(V3(0.2, 0.2, 0.2)),
|
||||
Color::Green,
|
||||
Color::Black,
|
||||
);
|
||||
fn render(&self, scene: &mut Scene) {
|
||||
match self.kind {
|
||||
ObjectKind::SkateBoard { pos, rot, .. } => {
|
||||
let board = MultiShape::new(vec![
|
||||
MultiShapeShape {
|
||||
shape: Shape::new_cube(V3(0.2, 0.01, 0.05)),
|
||||
offset: V3(0.0, 0.0, 0.0),
|
||||
},
|
||||
MultiShapeShape {
|
||||
shape: Shape::new_cube(V3(0.02, 0.02, 0.05)),
|
||||
offset: V3(0.04, -0.02, 0.0),
|
||||
},
|
||||
MultiShapeShape {
|
||||
shape: Shape::new_cube(V3(0.02, 0.02, 0.05)),
|
||||
offset: V3(0.14, -0.02, 0.0),
|
||||
},
|
||||
])
|
||||
.translate(V3(-0.1, -0.005, -0.0025))
|
||||
.rotate(rot);
|
||||
board.draw(pos, scene, Color::Green, Color::Black);
|
||||
}
|
||||
ObjectKind::Obstacle { pos, .. } => {
|
||||
scene.draw_shape(
|
||||
*pos,
|
||||
pos,
|
||||
&Shape::new_cube(V3(0.2, 0.1, 0.2)),
|
||||
Color::Green,
|
||||
Color::Black,
|
||||
@ -135,13 +191,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let event_queue = Arc::new(Mutex::new(EventQueue::new()));
|
||||
let mut game = Game::new(event_queue.clone());
|
||||
std::thread::spawn(move || {
|
||||
let server = Server::bind("10.133.51.127:42069");
|
||||
let server = Server::bind("10.133.51.127:5000");
|
||||
server.start(event_queue);
|
||||
});
|
||||
let mut objects: Vec<ObjectKind> = Vec::new();
|
||||
objects.push(ObjectKind::Player {
|
||||
pos: V3(-0.3, -0.25, 0.0),
|
||||
objects.push(ObjectKind::SkateBoard {
|
||||
pos: V3(0.0, -0.1, 0.0),
|
||||
vel: V3(0.0, 0.0, 0.0),
|
||||
rot: V3(0.0, 0.0, 0.0),
|
||||
});
|
||||
objects.push(ObjectKind::Ground {
|
||||
pos: V3(0.0, -0.3, -0.5),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user