Skip to content

Commit 50df1eb

Browse files
Fix physics system
1 parent 796208e commit 50df1eb

File tree

9 files changed

+139
-35
lines changed

9 files changed

+139
-35
lines changed

resources/images/Some(ferris).png

403 Bytes
Loading

resources/images/ground_left.png

-188 Bytes
Loading

resources/images/ground_right.png

-109 Bytes
Loading

src/components/cloud.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use ggez::{
55
};
66
use graphics::DrawParam;
77

8-
use crate::{WIDTH, utils::{AssetManager, Position}};
8+
use crate::{
9+
utils::{AssetManager, Position},
10+
WIDTH,
11+
};
912

1013
pub struct Cloud {
1114
position: Position,
@@ -15,10 +18,16 @@ pub struct Cloud {
1518
}
1619

1720
impl Cloud {
18-
pub fn new(pos_x: f32, pos_y: f32, scale: f32, speed: f32, asset_manager: &AssetManager) -> Self {
21+
pub fn new(
22+
pos_x: f32,
23+
pos_y: f32,
24+
scale: f32,
25+
speed: f32,
26+
asset_manager: &AssetManager,
27+
) -> Self {
1928
let cloud = asset_manager.get_image("Some(cloud).png");
2029
let position = Position::new(pos_x, pos_y, cloud.width(), cloud.height());
21-
30+
2231
Self {
2332
position,
2433
scale,
@@ -53,7 +62,12 @@ impl Cloud {
5362
self.position.move_by("x+", delta_time * self.speed);
5463

5564
if self.position.pos_start.x > WIDTH + 100. {
56-
self.position = Position::new(-100., self.position.pos_start.y, cloud.width(), cloud.height());
65+
self.position = Position::new(
66+
-100.,
67+
self.position.pos_start.y,
68+
cloud.width(),
69+
cloud.height(),
70+
);
5771
}
5872
}
5973
}

src/components/player.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use ggez_goodies::{
44
nalgebra_glm::Vec2,
55
};
66

7-
use crate::{HEIGHT, utils::{AssetManager, lerp}};
7+
use crate::{
8+
utils::{lerp, AssetManager},
9+
HEIGHT,
10+
};
811

912
const HEIGHT2: f32 = HEIGHT / 2.;
1013

@@ -104,7 +107,7 @@ impl Player {
104107
if self.going_boom {
105108
self.pos_y -= self.velocity;
106109

107-
if self.pos_y < 0. {
110+
if self.pos_y < 0. && !gonna_boom {
108111
self.going_boom = false;
109112

110113
self.pos_y = 0.;
@@ -117,8 +120,8 @@ impl Player {
117120
self.pos_y -= self.velocity;
118121
}
119122

120-
if self.pos_y < 0. && !gonna_boom && !self.going_boom {
121-
self.pos_y = 0.;
123+
if self.pos_y == 0.0 && self.velocity != 0.0 {
124+
self.velocity = 0.0;
122125
}
123126
}
124127

@@ -127,7 +130,7 @@ impl Player {
127130
return Some(Turbofish::new(
128131
self.pos_x + 220.,
129132
(-HEIGHT2 + 106.) + self.pos_y,
130-
asset_manager
133+
asset_manager,
131134
));
132135
} else {
133136
None

src/components/tile.rs

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use ggez_goodies::{
44
nalgebra_glm::Vec2,
55
};
66

7-
use crate::{utils::AssetManager, HEIGHT};
7+
use crate::{
8+
utils::{AssetManager, Position},
9+
HEIGHT,
10+
};
811

912
const HEIGHT2: f32 = HEIGHT / 2.;
1013

@@ -15,13 +18,61 @@ pub enum TileType {
1518
}
1619

1720
pub struct Tile {
18-
pub pos_x: f32,
21+
position: Position,
22+
width: f32,
23+
1924
tile_type: TileType,
2025
}
2126

2227
impl Tile {
23-
pub fn new(pos_x: f32, tile_type: TileType) -> Self {
24-
Self { pos_x, tile_type }
28+
pub fn new(pos_x: f32, asset_manager: &AssetManager, tile_type: TileType) -> Self {
29+
let position;
30+
let width;
31+
32+
match tile_type {
33+
TileType::LEFT => {
34+
let ground_left = asset_manager.get_image("ground_left.png");
35+
36+
position = Position::new(
37+
pos_x,
38+
-HEIGHT2 + ground_left.height() as f32,
39+
ground_left.width(),
40+
ground_left.height(),
41+
);
42+
43+
width = ground_left.width();
44+
}
45+
TileType::CENTER => {
46+
let ground_centre = asset_manager.get_image("ground_centre.png");
47+
48+
position = Position::new(
49+
pos_x,
50+
-HEIGHT2 + ground_centre.height() as f32,
51+
ground_centre.width(),
52+
ground_centre.height(),
53+
);
54+
55+
width = ground_centre.width();
56+
}
57+
TileType::RIGHT => {
58+
let ground_right = asset_manager.get_image("ground_right.png");
59+
60+
position = Position::new(
61+
pos_x,
62+
-HEIGHT2 + ground_right.height() as f32,
63+
ground_right.width(),
64+
ground_right.height(),
65+
);
66+
67+
width = ground_right.width();
68+
}
69+
}
70+
71+
Self {
72+
position,
73+
tile_type,
74+
width: width as f32,
75+
}
2576
}
2677

2778
pub fn draw(
@@ -39,7 +90,7 @@ impl Tile {
3990
ground_left.draw_camera(
4091
&camera,
4192
ctx,
42-
Vec2::new(self.pos_x, -HEIGHT2 + ground_left.height() as f32),
93+
Vec2::new(self.position.pos_start.x, self.position.pos_start.y),
4394
0.0,
4495
)?;
4596
}
@@ -48,20 +99,28 @@ impl Tile {
4899
ground_centre.draw_camera(
49100
&camera,
50101
ctx,
51-
Vec2::new(self.pos_x, -HEIGHT2 + ground_centre.height() as f32),
102+
Vec2::new(self.position.pos_start.x, self.position.pos_start.y),
52103
0.0,
53104
)?;
54105
}
55106
TileType::RIGHT => {
56107
ground_right.draw_camera(
57108
&camera,
58109
ctx,
59-
Vec2::new(self.pos_x, -HEIGHT2 + ground_right.height() as f32),
110+
Vec2::new(self.position.pos_start.x, self.position.pos_start.y),
60111
0.0,
61112
)?;
62113
}
63114
}
64115

65116
Ok(())
66117
}
118+
119+
pub fn position(&self) -> Position {
120+
self.position
121+
}
122+
123+
pub fn width(&self) -> f32 {
124+
self.width
125+
}
67126
}

src/game.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl Game {
121121
rng.gen_range(10., 40.),
122122
rng.gen_range(0.1, 0.3),
123123
rng.gen_range(10., 35.),
124-
&asset_manager
124+
&asset_manager,
125125
));
126126
}
127127

@@ -480,15 +480,16 @@ impl Game {
480480
let ferris_pos_x = self.player.pos_x;
481481
let ferris_pos_y = self.player.pos_y;
482482

483+
let ferris = self.asset_manager.get_image("Some(ferris).png");
484+
483485
let mut ferris_is_falling_down = true;
484486

485487
for tile in &mut self.ground {
486-
let tile_start = tile.pos_x;
487-
let tile_end = tile.pos_x + 64.;
488-
489-
if ferris_pos_x >= tile_start
490-
&& ferris_pos_x <= tile_end
491-
&& ferris_pos_y + (-HEIGHT / 2.0) - 64. >= (-HEIGHT / 2.0) - 64.
488+
// AABB
489+
if ferris_pos_x + ferris.width() as f32 >= tile.position().pos_start.x
490+
&& tile.position().pos_end.x >= ferris_pos_x
491+
&& ferris_pos_y + ferris.height() as f32 >= tile.position().pos_start.y
492+
&& tile.position().pos_end.y <= ferris_pos_y
492493
{
493494
ferris_is_falling_down = false;
494495

src/map.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,44 +47,66 @@ impl Map {
4747
for id in line.chars() {
4848
match id {
4949
'[' => {
50-
self.ground.push(Tile::new(self.draw_pos, TileType::LEFT));
50+
let tile = Tile::new(self.draw_pos, asset_manager, TileType::LEFT);
51+
52+
self.draw_inc = tile.width();
53+
self.ground.push(tile);
5154

5255
self.draw_pos += self.draw_inc;
5356
}
5457

5558
'-' => {
56-
self.ground.push(Tile::new(self.draw_pos, TileType::CENTER));
59+
let tile = Tile::new(self.draw_pos, asset_manager, TileType::CENTER);
60+
61+
self.draw_inc = tile.width();
62+
self.ground.push(tile);
5763

5864
self.draw_pos += self.draw_inc;
5965
}
6066

6167
']' => {
62-
self.ground.push(Tile::new(self.draw_pos, TileType::RIGHT));
68+
let tile = Tile::new(self.draw_pos, asset_manager, TileType::RIGHT);
69+
70+
self.draw_inc = tile.width();
71+
self.ground.push(tile);
6372

6473
self.draw_pos += self.draw_inc;
6574
}
6675

6776
'_' => {
77+
self.draw_inc = 100.0;
6878
self.draw_pos += self.draw_inc;
6979
}
7080

7181
'8' => {
72-
self.ground.push(Tile::new(self.draw_pos, TileType::CENTER));
82+
let tile = Tile::new(self.draw_pos, asset_manager, TileType::CENTER);
83+
84+
self.draw_inc = tile.width();
85+
86+
self.ground.push(tile);
7387
self.enemies.push(Enemy::new(self.draw_pos, asset_manager));
7488

7589
self.draw_pos += self.draw_inc;
7690
self.total_enemies += 1;
7791
}
7892

7993
'4' => {
80-
self.ground.push(Tile::new(self.draw_pos, TileType::CENTER));
94+
let tile = Tile::new(self.draw_pos, asset_manager, TileType::CENTER);
95+
96+
self.draw_inc = tile.width();
97+
98+
self.ground.push(tile);
8199
self.player = Some(Player::new(self.draw_pos));
82100

83101
self.draw_pos += self.draw_inc;
84102
}
85103

86104
'*' => {
87-
self.ground.push(Tile::new(self.draw_pos, TileType::CENTER));
105+
let tile = Tile::new(self.draw_pos, asset_manager, TileType::CENTER);
106+
107+
self.draw_inc = tile.width();
108+
109+
self.ground.push(tile);
88110
self.barrels.push(Barrel::new(self.draw_pos, asset_manager));
89111

90112
self.draw_pos += self.draw_inc;

src/utils.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,20 @@ pub struct Position {
2222
pub pos_end: Point2<f32>,
2323

2424
width: u16,
25-
height: u16
25+
height: u16,
2626
}
2727

2828
impl Position {
2929
pub fn new(x: f32, y: f32, width: u16, height: u16) -> Self {
3030
let pos_start = Point2::new(x, y);
3131
let pos_end = Point2::new(x + width as f32, y - height as f32);
3232

33-
Self { pos_start, pos_end, width, height }
33+
Self {
34+
pos_start,
35+
pos_end,
36+
width,
37+
height,
38+
}
3439
}
3540

3641
pub fn is_touching(&self, x: f32, y: f32) -> bool {
@@ -55,7 +60,7 @@ impl Position {
5560
"y+" => {
5661
self.pos_start.y += by;
5762
self.pos_end.y += by;
58-
},
63+
}
5964

6065
"x-" => {
6166
self.pos_start.x -= by;
@@ -65,7 +70,7 @@ impl Position {
6570
"y-" => {
6671
self.pos_start.y -= by;
6772
self.pos_end.y -= by;
68-
},
73+
}
6974

7075
_ => panic!(),
7176
}
@@ -77,14 +82,14 @@ impl Position {
7782
"x" => {
7883
self.pos_start.x = to;
7984
self.pos_end.x = to + self.width as f32;
80-
},
85+
}
8186

8287
"y" => {
8388
self.pos_start.y = to;
8489
self.pos_end.y = to - self.height as f32;
8590
}
8691

87-
_ => panic!()
92+
_ => panic!(),
8893
}
8994
}
9095
}

0 commit comments

Comments
 (0)