I'm currently developing a game with a top down map and units on it with Vue3. I have a Unit Store where I keep all my unit objects in an array and each unit is placed on the map based on its coordinates. I have a method in my store to retrieve all units on the same coordinates:
public getUnitsAt(coordinates: Coordinates): Array<Unit> {
return this.state.units.filter((unit) => unit.coordinates.x === coordinates.x && unit.coordinates.y === coordinates.y);
}
In most cases this is enough to display these units in a list. Now I introduced towns with their own town view component and I want to display the units that are "outside" of the town, but still on the same coordinates. So I use this:
const outsideUnits = computed(() => unitStore.getUnitsAt(town.coordinates));
So far so good, but now I want to be able to sort these outsideUnits, by telling a specific unit to move to the front. The problem is that this array is already a filtered array copy from the store. So I can probably sort this computed property, but as soon another unit is added to the store this gets recomputed and my order is lost. Any ideas how I can still have an up to date array of units, but be able to sort?
If a unit moves from a nearby field to the same field, it should automatically be in front.