Skip to content

Commit

Permalink
Adjusting new character collision
Browse files Browse the repository at this point in the history
  • Loading branch information
moreirayokoyama committed Jul 25, 2024
1 parent a93c1ee commit b716083
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
46 changes: 30 additions & 16 deletions src/character.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use bevy::{
app::{Plugin, Startup, Update},
asset::AssetServer,
math::{Rect, Vec2},
prelude::{default, Commands, Component, Query, Res, With},
sprite::{Sprite, SpriteBundle},
asset::{AssetServer, Assets, Handle},
math::{Rect, UVec2, Vec2},
prelude::{default, Commands, Component, Image, Query, Res, ResMut, Resource, With},
sprite::{Sprite, SpriteBundle, TextureAtlas, TextureAtlasLayout},
time::Time,
transform::components::Transform,
};
use bevy_rapier2d::prelude::*;

use crate::{
control::{CharacterControlOffset, MapControlOffset},
GameWorld, BLOCK_SIZE, CHARACTER_MOVEMENT_SPEED, PIXEL_PERFECT_LAYERS,
GameWorld, BLOCK_SIZE, CHARACTER_MOVEMENT_SPEED, CHARACTER_SIZE, PIXEL_PERFECT_LAYERS,
WORLD_BOTTOM_OFFSET_IN_PIXELS, WORLD_CENTER_COL,
};

Expand All @@ -29,13 +29,21 @@ impl Plugin for CharacterPlugin {
}
}

fn startup(mut commands: Commands, asset_server: Res<AssetServer>, game_world: Res<GameWorld>) {
fn startup(
mut commands: Commands,
asset_server: Res<AssetServer>,
game_world: Res<GameWorld>,
mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>,
) {
let atlas_layout = TextureAtlasLayout::from_grid(UVec2::new(16, 16), 8, 5, None, None);
let atlas_layout_handle = texture_atlases.add(atlas_layout);
let texture = asset_server.load("bgp_catdev/player_and_ui/Basic_Player.png");
commands.spawn((
Character {
movement_speed: CHARACTER_MOVEMENT_SPEED as f32,
},
SpriteBundle {
texture: asset_server.load("character/idle/i2.png"),
texture,
transform: Transform::from_xyz(
(BLOCK_SIZE as f32) * 0.5,
(((game_world.get_height_in_blocks(WORLD_CENTER_COL) as usize + 10) * BLOCK_SIZE)
Expand All @@ -45,28 +53,34 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>, game_world: R
),
sprite: Sprite {
//anchor: bevy::sprite::Anchor::BottomCenter,
custom_size: Option::Some(Vec2::new(14.0, 30.0)),
rect: Some(Rect {
max: Vec2::new(520.0, 540.0),
min: Vec2::new(320.0, 110.0),
}),
custom_size: Option::Some(Vec2::new(CHARACTER_SIZE as f32, CHARACTER_SIZE as f32)),
..default()
},

..default()
},
TextureAtlas {
layout: atlas_layout_handle,
index: 0,
..Default::default()
},
RigidBody::KinematicPositionBased,
Collider::capsule_y(11.5, 7.),
Collider::capsule_y((CHARACTER_SIZE / 16) as f32, (CHARACTER_SIZE / 2) as f32),
KinematicCharacterController {
custom_shape: Option::Some((Collider::cuboid(7., 15.), Vec2::new(0., 5.), 0.)),
custom_shape: Option::Some((
Collider::cuboid((CHARACTER_SIZE / 2) as f32, (CHARACTER_SIZE / 2) as f32),
Vec2::new(0., 5.),
0.,
)),
offset: CharacterLength::Absolute(0.1),
autostep: Option::Some(CharacterAutostep {
max_height: CharacterLength::Relative(0.5),
max_height: CharacterLength::Relative(0.4),
min_width: CharacterLength::Relative(0.1),
..default()
}),
slide: true,
snap_to_ground: Option::Some(CharacterLength::Absolute(0.1)),
normal_nudge_factor: 1.,
normal_nudge_factor: 0.1,
..default()
},
//LockedAxes::ROTATION_LOCKED,
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub const CHUNKS_TO_LOAD: usize = CHUNKS_IN_CANVAS + CHUNKS_LOAD_THRESHOLD;

pub const MAP_MOVEMENT_SPEED_IN_BLOCKS: usize = 4; //camera speed in blocks/second
pub const MAP_MOVEMENT_SPEED: usize = BLOCK_SIZE * MAP_MOVEMENT_SPEED_IN_BLOCKS; //camera speed in pixels/second

pub const CHARACTER_SIZE: usize = BLOCK_SIZE * 4;
pub const CHARACTER_MOVEMENT_SPEED: usize = MAP_MOVEMENT_SPEED_IN_BLOCKS * 2; //camera speed in blocks/second

pub const DAY_DURATION_IN_SECONDS: usize = 4 * 60;
Expand Down

0 comments on commit b716083

Please sign in to comment.