Combat UI based on speed.

GC2

How do I create a combat UI when fighting and how do I base turns on speed?
For example: If I have 2 speed, enemy A has 2 speed, and enemy B has 1 speed. Enemy A and I would both get a turn while enemy B needs to wait two turns before having its turn.


There's different ways to handle it, but this would be mine:
Make a list of current combatants (probably at the beginning of combat), and give each an attribute saying how long it is until that character gets a turn. Maybe that's some number divided by their speed, or minus their speed, or some more complex formula.

So, you've got a list of characters, and each character has an attribute saying how long until their next turn.
Then you'd have a turnscript that looks something like the following (pseudocode):

player_turn = false
while (not player_turn) {
  Take a 'current character' from the front of the list
  Add the current character to the end of the list
  subtract one from current character's wait time
  if current character's wait time is zero {
    if (current character is the player) {
      // this exits the loop so the player can take their turn
      player_turn = true
    }
    else {
      decide what the current enemy is going to do, and resolve that action
    }
    recalculate the character's wait time according to their speed
  }
}

Or an alternate method, avoiding the need for a list (which may make a difference in some circumstances), would be having a turnscript like:

// Get a list of enemies/allies/player in the current room
// In this case, I'm getting a list of anything in the current room with a 'speed' attribute; but you can do something different depending on your game
combatants = FilterByNotAttribute (GetDirectChildren (game.pov.parent), "speed", null))

// We probably don't need to take turns if the player is the only person in the room
if (ListCount (combatants) > 1) {

  // keep on looping until it's the player's turn
  while (game.pov.turncounter > 0) {
    foreach (npc, combatants) {

      // If they only just joined combat, they have 360 'ticks' until their first action. Each time through the loop, this number goes down by their speed, so a character with double the speed gets twice as many turns
      // I picked 360 because it's not a massive number, but has lots of factors, which makes the maths smoother
      if (not HasInt (npc, "turncount")) {
        npc.turncount = 360
      }
      npc.turncount = npc.turncount - npc.speed

      // if it's their turn and they're not the player and not dead, they take their turn
      // If you have things like paralysis effects, it might be worth using a function for this test
      if (npc.turncount < 0 and not npc = game.pov and not GetBoolean (npc, "dead")) {
        npc.turncount = npc.turncount + 360
        // This function should decide what action the NPC takes, and do all the damage etc.
        TakeTurn (npc)
      }
    }
  }
}

game.pov.turncount = game.pov.turncount + 360

Log in to post a reply.

Support

Forums