if let Intersection(wall, pos) = lvl.intersect_walls(self.body.pos - self.body.vel, self.body.pos) {
self.body.standing_on = Some(wall);
self.body.pos = pos;
- self.body.vel = point!(0.0, 0.0);
self.state.exit();
- self.state = Box::new(StandState);
+ self.state = Box::new(StandState); // eller loopa igenom alla triggers eller states för att hitta rätt state?
self.state.enter(&mut self.body, &ctrl, objects);
}
}
struct StandState;
impl State for StandState {
+ fn enter(&mut self, body: &mut Body, _ctrl: &Controller, _objects: &mut Objects) {
+ if let Some(wall) = &body.standing_on {
+ body.vel = body.vel.project_onto(wall.angle());
+ }
+ }
+
fn update(&mut self, body: &mut Body, _ctrl: &Controller, _objects: &mut Objects, _lvl: &Level, _dt: Duration) -> Option<Box<dyn State>> {
- if let Some(_wall) = &body.standing_on {
- body.vel *= 0.9;
+ if let Some(wall) = &body.standing_on {
+ let (mut pos, mut vel) = wall.from_2d(&body.pos, &body.vel);
+ vel *= 0.9;
+ pos += vel;
+ let (p, v) = wall.to_2d(pos, vel);
+ body.pos = p;
+ body.vel = v;
+ }
+
+ None
+ }
+}
}
None