I often use several
else combos. Don’t neglect the power of:
Great! In fact, I actually pretty much ignore
findByType() and build my own arrays off of
findEnemies(). This can really help with advanced auto-targeting. Be careful with the
while loop. They are best handled for small operations (array sorting, etc.)
Yes, but that can get a little complicated. Typically, I kill enemies and if there are no enemies, then I collect items. However, you can indeed separate your “destination” from your “target”, which I do in a lot of my level code. What I do is probably very different from others, but here is a simplification:
// Automatic Actions (if any)
// Choose Destination
else if item:
// Choose Target
enemies = self.findEnemies()
nearest = self.findNearest(enemies)
// Loop through enemies to choose target, and after done:
// If there is no target, choose nearest.
target = nearest
// Control Allies (if I have them)
// Attack Target (if nearby)
if self.distanceTo(target) <= this.attackRange || 5
else if self.distanceTo(target) <= 10
else if nearest:
if self.distanceTo(nearest) <= this.attackRange || 5
// Move toward Destination
else if target:
self.move(home) // Used if you need a "center point"
Now, the above is pseudo-code, which means that it’s more about the process than the code itself. I couldn’t demonstrate the for/while loop for enemies because I don’t really understand CoffeeScript. I use this basic structure for almost every level (even multiplayer levels) and it allows for a lot of freedom. I can move as I need or around a
target killing everything in my path along the way.
From your statements and your code, it sounds like you want something similar to this process. Using this, you simply have to realize that each cycle will update with the newest information (even if targets and destination don’t change). Just add conditions to the branch that you need… for instance, if you need to heal by going to a particular point, add
else if to the “moving” branch.
I know its a lot, but I hope this helps a bit. Again, I don’t know CoffeeScript, so I can’t give a code answer… just a thinking one.