Everett Corvid
11/22/2023, 10:35 PMCLOVIS
11/23/2023, 10:08 AMHashMap<Position, Set<Entity>>
, but each time you wanted to change an entity's position, you would have to remove it from the previous position and add it back to the new position. If you forget to do it somewhere, you risk positions being desynchronized with entities. Also, you would need a stable way to compare entities (to find the one you want to delete), meaning all entities should have a hashCode/equals that doesn't change.
3. Yes, HashMap<Key, Value?>
allows storing null
for a given key. However, I'm not sure that's what you want here: do you want to store empty values? I think you just want to remove the key altogether when there are no entities at that position. Map.get
already returns a nullable value in case they key was not found.
4. The standard library collections do not have this feature, but you could custom-made structures that do. For example, if you know your entire grid is relatively small (= not an issue to store it entirely in memory), you could use an Array<Array<Array<Set<Entity>>>>
where each array is one of the dimensions. This would by far be the fastest, but the memory cost is huge.
What you're asking about is called space partitioning. I encourage you to read about the different data structures and their pro/cons. When you find one you like, don't hesitate to ask for help to implement it 🙂Klitos Kyriacou
11/23/2023, 10:46 AM