Back In Business

1 minute read

43 Days Until I Can Walk

Aside from the look up/down feature, I think it’s fair to say that the functionality of the engine is pretty much back to where it was before I started refactoring code, except things are now much neater and make significantly better use of Rust features. This has taken a lot longer than expected, but I’ve learned a ridiculous amount over the course of tweaking and reworking the codebase. I’m very happy with where things have finally landed. I might just finish out the week moving a few little inconsequential bits and pieces around, just to see if I can learn anything new about some of the Rust features I’ve been using. But so far, I feel like I’ve got a pretty solid handle on things.

I spent a lot of today splitting my time between polishing Rust code and working at my job. At this stage polishing involves trying to move some duplicate code into macros or functions, and occasionally removing or adding structs. I finally landed on an implementation of ray casting that I’m happy with. As I hoped, it was possible to remove the need for function pointers by defining a ray struct for horizontal intersections and a separate one for vertical intersections. Both structs implement the Iterator trait and will yield an Option<Intersection> with each call to next. Merging the two iterators is then simply a case of calling merge_by and sorting on distance as seen below:

pub fn find_wall_intersections(origin_x: i32, origin_y: i32, direction: i32, sweep: i32, scene: &Scene) -> Vec<Intersection> {
  let ray_h = RayH::new(origin_x, origin_y, direction, sweep, scene);
  let ray_v = RayV::new(origin_x, origin_y, direction, sweep, scene);

  if ray_h.is_undefined() { return ray_v.collect(); }
  if ray_v.is_undefined() { return ray_h.collect(); }

  ray_h.merge_by(ray_v, |a, b| a.dist < b.dist).collect()
}

For me at my level of experience, this feels like a very “Rust” way to do things.

All going well, I guess next week I’ll finally start work on that map editor I’ve been planning to build. It would be nice to allow user to customise the environment a little to suit themselves. And eventually I’ll have to look at placing objects, enemies and doors in the environment. I’m very conscious of my timer for this project counting down. There’s only a few weeks to go (hooray, I’ll be able to walk soon), and there’s loads still to do.