So, I'm writing a game, as a hobby, and I'm trying to come up with an easy way to handle the logic of collisions.
It's a tile based game, very simple, simple enough not to require any ECS I think.
So I have a GameState, which has a "map" field, where map is a Vec of tiles. Each tile has some internal data like velocity, points, location, etc..
The problem I've been struggling for weeks now is the following function:
pub fn handle_move_tiles(level: &mut GameState) {
Where level is the game state: I need it to be mutable in order to change the score, for example.
What this function needs to do is:
1-Iterate over all tiles in the map, which, as said, is a Vec (self.level.map)
2-Check for collisions in any of the 8 possible adjacent tiles
3-If a collision has been detected, I'd like to invoke a method on the colliding tile,
passing it the list of tiles it collies with:
impl Tile {
pub fn handle_collision(&mut self, neighbors: Vec<&Tile>){}
..
}
I've tried implementing this logic in several ways, but the borrow checker always yells at me.
This is the last version so far:
pub fn handle_move_tiles(level: &mut GameState) {
// A a list of collisions, where the 1st element is the one that collides
// and the 2nd element is the list of tiles it collides with
//
// pub fn get_collisions(&self)
let collisions: Vec<usize, Vec<&Tile>) = level.get_collisions(); <---------------
for collision in &collisions { |
|
// Cannot borrow level.map as mutable because its also borrowed as inmutable |
let collider = level.map.get_mut(collision.0).unwrap(); -------|
collider.handle_collision(&collision.1);
}
}
The question is.. how should I approach this problem? Is it doable without Rc and/or Cells?