working player bound collision

This commit is contained in:
Thilo Behnke
2022-05-02 13:28:55 +02:00
parent 9fd69abc26
commit 41f6ae77e7
4 changed files with 12 additions and 11 deletions
+4 -4
View File
@@ -3,7 +3,7 @@ use crate::game_object::components::{DefaultGeomComp, DefaultPhysicsComp};
use crate::game_object::game_object::{DefaultGameObject, GameObject};
use crate::geom::geom::Vector;
use crate::geom::shape::{Shape, ShapeType};
use crate::pong::pong_collisions::{handle_ball_bounds_collision, handle_player_ball_collision};
use crate::pong::pong_collisions::{handle_ball_bounds_collision, handle_player_ball_collision, handle_player_bound_collision};
use crate::utils::utils::{DefaultLoggerFactory, Logger, LoggerFactory, NoopLogger};
use std::borrow::{Borrow, BorrowMut};
use std::cell::{Cell, Ref, RefCell, RefMut};
@@ -69,7 +69,7 @@ impl Field {
field.collision_handler.register(
(String::from("player"), String::from("bound")),
handle_ball_bounds_collision,
handle_player_bound_collision,
);
field.collision_detector.set_groups(
@@ -135,11 +135,11 @@ impl Field {
let input = input_opt.unwrap();
match input.input {
InputType::UP => {
let updated_vel_y = (obj_mut.vel().y - 1.).max(-1.);
let updated_vel_y = (obj_mut.vel().y - 1.).max(-5.);
obj_mut.vel_mut().y = updated_vel_y;
}
InputType::DOWN => {
let updated_vel_y = (obj_mut.vel().y + 1.).min(1.);
let updated_vel_y = (obj_mut.vel().y + 1.).min(5.);
obj_mut.vel_mut().y = updated_vel_y;
}
};
+3 -3
View File
@@ -45,11 +45,11 @@ pub mod pong_collisions {
let shape = player.shape().clone();
let player_orientation = player.orientation().clone();
let height = match shape {
ShapeType::Rect(_, _, height) => height.clone(),
ShapeType::Circle(_, radius) => radius * 2.,
ShapeType::Rect(_, _, height) => height.clone() / 2.,
ShapeType::Circle(_, radius) => radius,
};
let mut perpendicular = player_orientation.get_opposing_orthogonal(bound.orientation());
perpendicular.y *= height;
perpendicular.y *= (height + 1.);
let mut new_pos = bound.pos().clone();
new_pos.add(&perpendicular);
let player_pos = player.pos_mut();
+4 -4
View File
@@ -33,8 +33,8 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
pub struct GameObjectDTO {
pub id: u16,
pub x: u16,
pub y: u16,
pub x: f64,
pub y: f64,
pub orientation_x: f64,
pub orientation_y: f64,
pub vel_x: f64,
@@ -53,8 +53,8 @@ impl GameObjectDTO {
let shape = obj.shape();
return GameObjectDTO {
id: obj.id(),
x: pos.x as u16,
y: pos.y as u16,
x: pos.x,
y: pos.y,
orientation_x: orientation.x,
orientation_y: orientation.y,
vel_x: vel.x,
+1
View File
@@ -94,6 +94,7 @@ const drawObjects = () => {
drawLine(ctx, obj.x, obj.y, obj.x + obj.vel_x * 20, obj.y + obj.vel_y * 20, 'red')
// orientation
drawLine(ctx, obj.x, obj.y, obj.x + obj.orientation_x * 20, obj.y + obj.orientation_y * 20, 'blue')
ctx.fillText(`[x: ${obj.x}, y: ${obj.y}]`, obj.x + 10, obj.y)
}
})
}