Skip to content

Commit b716083

Browse files
Adjusting new character collision
1 parent a93c1ee commit b716083

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

src/character.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use bevy::{
22
app::{Plugin, Startup, Update},
3-
asset::AssetServer,
4-
math::{Rect, Vec2},
5-
prelude::{default, Commands, Component, Query, Res, With},
6-
sprite::{Sprite, SpriteBundle},
3+
asset::{AssetServer, Assets, Handle},
4+
math::{Rect, UVec2, Vec2},
5+
prelude::{default, Commands, Component, Image, Query, Res, ResMut, Resource, With},
6+
sprite::{Sprite, SpriteBundle, TextureAtlas, TextureAtlasLayout},
77
time::Time,
88
transform::components::Transform,
99
};
1010
use bevy_rapier2d::prelude::*;
1111

1212
use crate::{
1313
control::{CharacterControlOffset, MapControlOffset},
14-
GameWorld, BLOCK_SIZE, CHARACTER_MOVEMENT_SPEED, PIXEL_PERFECT_LAYERS,
14+
GameWorld, BLOCK_SIZE, CHARACTER_MOVEMENT_SPEED, CHARACTER_SIZE, PIXEL_PERFECT_LAYERS,
1515
WORLD_BOTTOM_OFFSET_IN_PIXELS, WORLD_CENTER_COL,
1616
};
1717

@@ -29,13 +29,21 @@ impl Plugin for CharacterPlugin {
2929
}
3030
}
3131

32-
fn startup(mut commands: Commands, asset_server: Res<AssetServer>, game_world: Res<GameWorld>) {
32+
fn startup(
33+
mut commands: Commands,
34+
asset_server: Res<AssetServer>,
35+
game_world: Res<GameWorld>,
36+
mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>,
37+
) {
38+
let atlas_layout = TextureAtlasLayout::from_grid(UVec2::new(16, 16), 8, 5, None, None);
39+
let atlas_layout_handle = texture_atlases.add(atlas_layout);
40+
let texture = asset_server.load("bgp_catdev/player_and_ui/Basic_Player.png");
3341
commands.spawn((
3442
Character {
3543
movement_speed: CHARACTER_MOVEMENT_SPEED as f32,
3644
},
3745
SpriteBundle {
38-
texture: asset_server.load("character/idle/i2.png"),
46+
texture,
3947
transform: Transform::from_xyz(
4048
(BLOCK_SIZE as f32) * 0.5,
4149
(((game_world.get_height_in_blocks(WORLD_CENTER_COL) as usize + 10) * BLOCK_SIZE)
@@ -45,28 +53,34 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>, game_world: R
4553
),
4654
sprite: Sprite {
4755
//anchor: bevy::sprite::Anchor::BottomCenter,
48-
custom_size: Option::Some(Vec2::new(14.0, 30.0)),
49-
rect: Some(Rect {
50-
max: Vec2::new(520.0, 540.0),
51-
min: Vec2::new(320.0, 110.0),
52-
}),
56+
custom_size: Option::Some(Vec2::new(CHARACTER_SIZE as f32, CHARACTER_SIZE as f32)),
5357
..default()
5458
},
59+
5560
..default()
5661
},
62+
TextureAtlas {
63+
layout: atlas_layout_handle,
64+
index: 0,
65+
..Default::default()
66+
},
5767
RigidBody::KinematicPositionBased,
58-
Collider::capsule_y(11.5, 7.),
68+
Collider::capsule_y((CHARACTER_SIZE / 16) as f32, (CHARACTER_SIZE / 2) as f32),
5969
KinematicCharacterController {
60-
custom_shape: Option::Some((Collider::cuboid(7., 15.), Vec2::new(0., 5.), 0.)),
70+
custom_shape: Option::Some((
71+
Collider::cuboid((CHARACTER_SIZE / 2) as f32, (CHARACTER_SIZE / 2) as f32),
72+
Vec2::new(0., 5.),
73+
0.,
74+
)),
6175
offset: CharacterLength::Absolute(0.1),
6276
autostep: Option::Some(CharacterAutostep {
63-
max_height: CharacterLength::Relative(0.5),
77+
max_height: CharacterLength::Relative(0.4),
6478
min_width: CharacterLength::Relative(0.1),
6579
..default()
6680
}),
6781
slide: true,
6882
snap_to_ground: Option::Some(CharacterLength::Absolute(0.1)),
69-
normal_nudge_factor: 1.,
83+
normal_nudge_factor: 0.1,
7084
..default()
7185
},
7286
//LockedAxes::ROTATION_LOCKED,

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ pub const CHUNKS_TO_LOAD: usize = CHUNKS_IN_CANVAS + CHUNKS_LOAD_THRESHOLD;
6161

6262
pub const MAP_MOVEMENT_SPEED_IN_BLOCKS: usize = 4; //camera speed in blocks/second
6363
pub const MAP_MOVEMENT_SPEED: usize = BLOCK_SIZE * MAP_MOVEMENT_SPEED_IN_BLOCKS; //camera speed in pixels/second
64+
65+
pub const CHARACTER_SIZE: usize = BLOCK_SIZE * 4;
6466
pub const CHARACTER_MOVEMENT_SPEED: usize = MAP_MOVEMENT_SPEED_IN_BLOCKS * 2; //camera speed in blocks/second
6567

6668
pub const DAY_DURATION_IN_SECONDS: usize = 4 * 60;

0 commit comments

Comments
 (0)