mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-08-19 05:06:14 +00:00
working player bound collision
This commit is contained in:
@@ -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
@@ -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
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user