Skip to content

Commit

Permalink
Add errors; hooks use result as return value
Browse files Browse the repository at this point in the history
  • Loading branch information
jjyr committed Nov 6, 2024
1 parent 27288d4 commit b3c5bd3
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 150 deletions.
37 changes: 29 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ serde = { version = "1.0.207", features = ["derive"] }
serde_json = "1.0.124"
roast2d_derive = { version = "0", path = "roast2d_derive" }
hashbrown = "0.15.0"
thiserror = "2.0.0"


# Non-Wasm32 target
Expand Down
66 changes: 32 additions & 34 deletions examples/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,18 @@ impl Ball {
}

impl EntHooks for Ball {
fn draw(&self, eng: &mut Engine, w: &mut World, ent: Ent, viewport: Vec2) -> Option<()> {
fn draw(&self, eng: &mut Engine, w: &mut World, ent: Ent, viewport: Vec2) -> Result<()> {
let ent = w.get(ent)?;
if let Some(transform) = ent.get::<Transform>() {
eng.draw_rect(
self.size,
transform.pos + viewport,
self.color,
None,
Some(transform.scale),
None,
);
}
None
let transform = ent.get::<Transform>()?;
eng.draw_rect(
self.size,
transform.pos + viewport,
self.color,
None,
Some(transform.scale),
None,
);
Ok(())
}

fn collide(
Expand All @@ -92,7 +91,7 @@ impl EntHooks for Ball {
ent: Ent,
normal: Vec2,
_trace: Option<&Trace>,
) -> Option<()> {
) -> Result<()> {
let mut ent = w.get_mut(ent)?;
let t = ent.get_mut::<Physics>()?;

Expand All @@ -104,10 +103,10 @@ impl EntHooks for Ball {
t.vel.x = normal.x * BALL_MAX_VEL;
t.accel.x = normal.x * BALL_ACCEL;
}
None
Ok(())
}

fn post_update(&self, eng: &mut Engine, w: &mut World, ent: Ent) -> Option<()> {
fn post_update(&self, eng: &mut Engine, w: &mut World, ent: Ent) -> Result<()> {
let mut ent = w.get_mut(ent)?;
let view = eng.view_size();
let t = ent.get::<Transform>()?;
Expand All @@ -130,11 +129,10 @@ impl EntHooks for Ball {
ent.get_mut::<Physics>()?.vel.y = -BALL_MAX_VEL;
}

if let Some(p) = ent.get_mut::<Physics>() {
p.vel.y = p.vel.y.abs().clamp(BALL_MIN_VEL, BALL_MAX_VEL) * p.vel.y.signum();
}
let p = ent.get_mut::<Physics>()?;
p.vel.y = p.vel.y.abs().clamp(BALL_MIN_VEL, BALL_MAX_VEL) * p.vel.y.signum();

None
Ok(())
}
}

Expand Down Expand Up @@ -194,24 +192,24 @@ impl Brick {
pub struct BrickHooks;

impl EntHooks for BrickHooks {
fn draw(&self, eng: &mut Engine, w: &mut World, ent: Ent, viewport: Vec2) -> Option<()> {
fn draw(&self, eng: &mut Engine, w: &mut World, ent: Ent, viewport: Vec2) -> Result<()> {
let ent = w.get(ent)?;
let t = ent.get::<Transform>()?;
let color = ent.get::<Brick>()?.color;
eng.draw_rect(t.size, t.pos + viewport, color, None, Some(t.scale), None);
None
Ok(())
}

fn kill(&self, _eng: &mut Engine, w: &mut World, ent: Ent) -> Option<()> {
fn kill(&self, _eng: &mut Engine, w: &mut World, ent: Ent) -> Result<()> {
G.with_borrow_mut(|g| {
g.score += 1;
});

w.get_resource_mut::<CollisionSet>()?.remove(ent);
None
Ok(())
}

fn update(&self, eng: &mut Engine, w: &mut World, ent: Ent) -> Option<()> {
fn update(&self, eng: &mut Engine, w: &mut World, ent: Ent) -> Result<()> {
let mut ent = w.get_mut(ent)?;
if ent.get::<Brick>()?.hit {
let ent_id = ent.id();
Expand Down Expand Up @@ -239,18 +237,18 @@ impl EntHooks for BrickHooks {
let t = ent.get_mut::<Transform>()?;
t.scale = Vec2::splat(scale);
}
None
Ok(())
}

fn touch(&self, _eng: &mut Engine, w: &mut World, ent: Ent, _other: Ent) -> Option<()> {
fn touch(&self, _eng: &mut Engine, w: &mut World, ent: Ent, _other: Ent) -> Result<()> {
let mut ent = w.get_mut(ent)?;
let brick = ent.get_mut::<Brick>()?;
if !brick.hit {
brick.hit = true;
let pos = ent.get::<Transform>()?.pos;
ent.get_mut::<Brick>()?.dead_pos = pos;
}
None
Ok(())
}
}

Expand Down Expand Up @@ -284,15 +282,15 @@ impl Player {
pub struct PlayerHooks;

impl EntHooks for PlayerHooks {
fn draw(&self, eng: &mut Engine, w: &mut World, ent: Ent, viewport: Vec2) -> Option<()> {
fn draw(&self, eng: &mut Engine, w: &mut World, ent: Ent, viewport: Vec2) -> Result<()> {
let ent = w.get(ent)?;
let t = ent.get::<Transform>()?;
let p = ent.get::<Player>()?;
eng.draw_rect(t.size, t.pos + viewport, p.color, None, Some(t.scale), None);
None
Ok(())
}

fn update(&self, eng: &mut Engine, w: &mut World, ent: Ent) -> Option<()> {
fn update(&self, eng: &mut Engine, w: &mut World, ent: Ent) -> Result<()> {
let mut ent = w.get_mut(ent)?;
let phy = ent.get_mut::<Physics>()?;

Expand All @@ -305,18 +303,18 @@ impl EntHooks for PlayerHooks {
if input.pressed(Action::Left) {
phy.vel.x = -PLAYER_VEL;
}
None
Ok(())
}

fn touch(&self, _eng: &mut Engine, w: &mut World, ent: Ent, other: Ent) -> Option<()> {
fn touch(&self, _eng: &mut Engine, w: &mut World, ent: Ent, other: Ent) -> Result<()> {
let [mut ent, mut other] = w.many_mut([ent, other]);
if other.get::<Ball>().is_some() {
if other.get::<Ball>().is_ok() {
let p1 = ent.get_mut::<Physics>()?;
let p2 = other.get_mut::<Physics>()?;
p2.accel.x += p1.vel.x * 0.6;
p2.vel.x = p2.accel.x.signum() * p2.vel.x.abs();
}
None
Ok(())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Camera {
bounds: Option<Vec2>,
) {
if let Some(follow) = follow {
let Some(transform) = follow.get::<Transform>() else {
let Ok(transform) = follow.get::<Transform>() else {
log::warn!("Camera follow an non transform ent");
return;
};
Expand Down Expand Up @@ -103,7 +103,7 @@ impl Camera {
self.look_ahead_target.y = self.look_ahead.y;
}

if self.snap_to_platform && follow.get::<Physics>().is_some_and(|phy| phy.on_ground) {
if self.snap_to_platform && follow.get::<Physics>().is_ok_and(|phy| phy.on_ground) {
self.deadzone_pos.y = follow_rect.max.y - self.deadzone.y;
}

Expand Down
Loading

0 comments on commit b3c5bd3

Please sign in to comment.