Can no longer run off the map
This commit is contained in:
parent
64a59daf8b
commit
e6157067ed
@ -6,7 +6,7 @@
|
|||||||
"top_texture": {
|
"top_texture": {
|
||||||
"file_path": "assets/env/env_testObject/env_testObjectTop.png"
|
"file_path": "assets/env/env_testObject/env_testObjectTop.png"
|
||||||
},
|
},
|
||||||
"footprint_radius": 128.0,
|
"footprint_radius": 256.0,
|
||||||
"physics_colliders": [
|
"physics_colliders": [
|
||||||
{
|
{
|
||||||
"position": [
|
"position": [
|
||||||
@ -16,7 +16,8 @@
|
|||||||
"size": [
|
"size": [
|
||||||
230,
|
230,
|
||||||
127
|
127
|
||||||
]
|
],
|
||||||
|
"radius": 128.0
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"temperature": 5.0
|
"temperature": 5.0
|
||||||
|
9
game/dist/map_gameMap.objects.json
vendored
9
game/dist/map_gameMap.objects.json
vendored
@ -1,8 +1,11 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"type":"env",
|
"type": "env",
|
||||||
"name":"env_testObject",
|
"name": "env_testObject",
|
||||||
"position": [0,0],
|
"position": [
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
],
|
||||||
"rotation_radians": 0.5
|
"rotation_radians": 0.5
|
||||||
}
|
}
|
||||||
]
|
]
|
@ -460,4 +460,69 @@ impl MapRenderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Used to modify the player's velocity based on the effects of the world
|
||||||
|
pub fn effect_velocity_with_collisions(
|
||||||
|
&self,
|
||||||
|
player_position: na::Vector2<f32>,
|
||||||
|
player_velocity: na::Vector2<f32>,
|
||||||
|
) -> na::Vector2<f32> {
|
||||||
|
// If the player is not moving, we don't need to do anything
|
||||||
|
if player_velocity.norm() == 0.0 {
|
||||||
|
return player_velocity;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the velocity unit vector
|
||||||
|
let player_velocity_unit_vector = player_velocity.normalize();
|
||||||
|
|
||||||
|
// Find the position 1 pixel infront of the player
|
||||||
|
let player_position_1_pixel_infront = player_position + player_velocity_unit_vector;
|
||||||
|
|
||||||
|
// Check if this is in the collision zone of any objects
|
||||||
|
for obj_ref in &self.world_objects.object_references {
|
||||||
|
// Filter out anything more than 1000 pixels away
|
||||||
|
if (obj_ref.position - player_position).norm() > 1000.0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the object definition
|
||||||
|
let object_key = format!("{}:{}", obj_ref.kind, obj_ref.name);
|
||||||
|
let obj_def = self
|
||||||
|
.world_objects
|
||||||
|
.object_definitions
|
||||||
|
.get(&object_key)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Check if the player is about to be in a collision zone
|
||||||
|
for collider in &obj_def.physics_colliders {
|
||||||
|
// Handle a radius collider vs a size collider
|
||||||
|
if let Some(radius) = collider.radius {
|
||||||
|
// if (player_position_1_pixel_infront - obj_ref.position).norm() <= radius {
|
||||||
|
// // We are in a collision zone. Only allow the player to move away from the object
|
||||||
|
// let player_to_object_vector = obj_ref.position - player_position;
|
||||||
|
// }
|
||||||
|
} else if let Some(size) = collider.size {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the player is about to leave the map
|
||||||
|
let next_player_position = player_position + player_velocity;
|
||||||
|
let mut player_velocity = player_velocity;
|
||||||
|
if next_player_position.x < 0.0 {
|
||||||
|
player_velocity.x = 0.0;
|
||||||
|
}
|
||||||
|
else if next_player_position.x > self.map.width as f32 * 128.0 {
|
||||||
|
player_velocity.x = 0.0;
|
||||||
|
}
|
||||||
|
if next_player_position.y > 0.0 {
|
||||||
|
player_velocity.y = 0.0;
|
||||||
|
}
|
||||||
|
else if next_player_position.y < self.map.height as f32 * -128.0 {
|
||||||
|
player_velocity.y = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we got here, the player is not in a collision zone
|
||||||
|
player_velocity
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,17 +44,14 @@ impl PlayableScene {
|
|||||||
|
|
||||||
Self {
|
Self {
|
||||||
has_updated_discord_rpc: false,
|
has_updated_discord_rpc: false,
|
||||||
player: Player::new(na::Vector2::new(10.0 * constants.tile_size as f32, -10.0 * constants.tile_size as f32)),
|
player: Player::new(na::Vector2::new(
|
||||||
|
10.0 * constants.tile_size as f32,
|
||||||
|
-10.0 * constants.tile_size as f32,
|
||||||
|
)),
|
||||||
world_map: map_renderer,
|
world_map: map_renderer,
|
||||||
camera: raylib::camera::Camera2D {
|
camera: raylib::camera::Camera2D {
|
||||||
target: raylib::math::Vector2 {
|
target: raylib::math::Vector2 { x: 0.0, y: 0.0 },
|
||||||
x: 0.0,
|
offset: raylib::math::Vector2 { x: 0.0, y: 0.0 },
|
||||||
y: 0.0,
|
|
||||||
},
|
|
||||||
offset: raylib::math::Vector2 {
|
|
||||||
x: 0.0,
|
|
||||||
y: 0.0
|
|
||||||
},
|
|
||||||
rotation: 0.0,
|
rotation: 0.0,
|
||||||
zoom: 1.0,
|
zoom: 1.0,
|
||||||
},
|
},
|
||||||
@ -191,38 +188,47 @@ impl PlayableScene {
|
|||||||
.set_magnitude((constants.player.max_velocity * constants.tile_size) as f32);
|
.set_magnitude((constants.player.max_velocity * constants.tile_size) as f32);
|
||||||
}
|
}
|
||||||
|
|
||||||
player.position += &player.velocity * delta_time;
|
let velocity_modifier = &player.velocity * delta_time;
|
||||||
|
|
||||||
|
// Handle the player colliding with the world
|
||||||
|
let velocity_modifier = self
|
||||||
|
.world_map
|
||||||
|
.effect_velocity_with_collisions(player.position, velocity_modifier);
|
||||||
|
|
||||||
|
player.position += velocity_modifier;
|
||||||
|
|
||||||
self.update_camera(raylib);
|
self.update_camera(raylib);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the camera
|
// Update the camera
|
||||||
pub fn update_camera(
|
pub fn update_camera(&mut self, raylib: &raylib::RaylibHandle) {
|
||||||
&mut self,
|
|
||||||
raylib: & raylib::RaylibHandle,
|
|
||||||
) {
|
|
||||||
|
|
||||||
// Bounding box
|
// Bounding box
|
||||||
let bbox = na::Vector2::new(0.2, 0.2);
|
let bbox = na::Vector2::new(0.2, 0.2);
|
||||||
|
|
||||||
// Get bounding box dimensions on the screen
|
// Get bounding box dimensions on the screen
|
||||||
let bbox_screen_min: raylib::math::Vector2 = (((na::Vector2::new(1.0, 1.0) - bbox) * 0.5).component_mul(
|
let bbox_screen_min: raylib::math::Vector2 = (((na::Vector2::new(1.0, 1.0) - bbox) * 0.5)
|
||||||
&na::Vector2::new(raylib.get_screen_width() as f32, raylib.get_screen_height() as f32)
|
.component_mul(&na::Vector2::new(
|
||||||
)).into();
|
raylib.get_screen_width() as f32,
|
||||||
let bbox_screen_max: raylib::math::Vector2 = (((na::Vector2::new(1.0, 1.0) + bbox) * 0.5).component_mul(
|
raylib.get_screen_height() as f32,
|
||||||
&na::Vector2::new(raylib.get_screen_width() as f32, raylib.get_screen_height() as f32)
|
)))
|
||||||
)).into();
|
.into();
|
||||||
|
let bbox_screen_max: raylib::math::Vector2 = (((na::Vector2::new(1.0, 1.0) + bbox) * 0.5)
|
||||||
|
.component_mul(&na::Vector2::new(
|
||||||
|
raylib.get_screen_width() as f32,
|
||||||
|
raylib.get_screen_height() as f32,
|
||||||
|
)))
|
||||||
|
.into();
|
||||||
|
|
||||||
// Get bounding box in world space
|
// Get bounding box in world space
|
||||||
let mut bbox_world_min = raylib.get_screen_to_world2D(bbox_screen_min, self.camera);
|
let mut bbox_world_min = raylib.get_screen_to_world2D(bbox_screen_min, self.camera);
|
||||||
let mut bbox_world_max = raylib.get_screen_to_world2D(bbox_screen_max, self.camera);
|
let mut bbox_world_max = raylib.get_screen_to_world2D(bbox_screen_max, self.camera);
|
||||||
|
|
||||||
// Invert y
|
// Invert y
|
||||||
bbox_world_min.y *= -1.0;
|
bbox_world_min.y *= -1.0;
|
||||||
bbox_world_max.y *= -1.0;
|
bbox_world_max.y *= -1.0;
|
||||||
|
|
||||||
self.camera.offset = bbox_screen_min;
|
self.camera.offset = bbox_screen_min;
|
||||||
|
|
||||||
if self.player.position.x < bbox_world_min.x {
|
if self.player.position.x < bbox_world_min.x {
|
||||||
self.camera.target.x = self.player.position.x;
|
self.camera.target.x = self.player.position.x;
|
||||||
}
|
}
|
||||||
@ -234,7 +240,7 @@ impl PlayableScene {
|
|||||||
if self.player.position.x > bbox_world_max.x {
|
if self.player.position.x > bbox_world_max.x {
|
||||||
self.camera.target.x = bbox_world_min.x + (self.player.position.x - bbox_world_max.x);
|
self.camera.target.x = bbox_world_min.x + (self.player.position.x - bbox_world_max.x);
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.player.position.y < bbox_world_max.y {
|
if self.player.position.y < bbox_world_max.y {
|
||||||
self.camera.target.y = bbox_world_max.y - (self.player.position.y + bbox_world_min.y);
|
self.camera.target.y = bbox_world_max.y - (self.player.position.y + bbox_world_min.y);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user