Day 24: Coding a Game Part 3 Attacks

Dan Esmail - 1/24/2023

Intro

Alright so I have a gameboard and I have classes. Now we have to attack the monster to win the game. Attacks end up taking a lot of time to develop so this post will be all of that.

Attacks

The first thing I thought about when I wanted to build the attack system us how will I have unique skills and how will these translate to the game. First I wanted a deck-building game sort of. Where the player will randomly get skills from their class. This could be like a pyro wizard pulling fireball, or fire punch. Then they would click the skill to cast it. Now how was I going to do that? First off classes again help out.


JS
Attacks Class

                
                    
      
  class Attacks {
  constructor(name, type, sequence, id, dmg, cost) {
    this.name = name;
    this.type = type
    this.sequence = sequence;
    this.id = id;
    this.dmg = dmg;
    this.cost = cost;
  }

            
      
                
            

The constructor for this class is easy because all I have to do is think about what I need for an attack. I have the name of the attack, how much it cost, what the damage, and what's the type. Other than that I have id to tell the system which attacks it is in an array of attacks. Next, how does this attack play out on the board? I decided for my use it would be a sequence of numbers that are related to the cardinal direction. Like one being north and three being east. I followed that scheme all around the compass. An attack that goes north three times would be an array of three ones. Now when building the method to draw the attack I needed to think ahead of how I was going to do that. So if I wanted to draw the attack I would think first we need to see what direction the player was in, then change the sequence if the player isn't facing north, then read the new sequence, then cast the attack.


HTML
New Direction Function

                
                    
      
newDirection(num,direction){

  switch (direction) {
    case 'up':
      return num;
    break;
    case 'right':
      if (num == 0) {
        return 0;
      }else if (num+2>8) {
        return num+2-8
      }else{
        return num+2
      }
    break;
    case 'down':
    if (num == 0) {
      return 0;
    }else if (num+4>8) {
      return num+4-8
    }else{
      return num+4
    }
    break;
    case 'left':
    if (num == 0) {
      return 0;
    }else if (num+6>8) {
      return num+6-8
    }else{
      return num+6
    }
    break;
    default:

  }
}

            
      
                
            

The first thing to do was change the sequence depending on direction. I wrote every sequence as if the player is facing nothing. Then I used a switch to read the player's direction. If they were facing east the sequence would be two plus the original number. That is like if north one and if you look east one plus two which would be three sending the attack one box to the right.


HTML
New Direction Function

                
                    
      
newDirection(num,direction){

  switch (direction) {
    case 'up':
      return num;
    break;
    case 'right':
      if (num == 0) {
        return 0;
      }else if (num+2>8) {
        return num+2-8
      }else{
        return num+2
      }
    break;
    case 'down':
    if (num == 0) {
      return 0;
    }else if (num+4>8) {
      return num+4-8
    }else{
      return num+4
    }
    break;
    case 'left':
    if (num == 0) {
      return 0;
    }else if (num+6>8) {
      return num+6-8
    }else{
      return num+6
    }
    break;
    default:

  }
}

            
      
                
            

The first thing to do was change the sequence depending on direction. I wrote every sequence as if the player is facing nothing. Then I used a switch to read the player's direction. If they were facing east the sequence would be two plus the original number. That is like if north one and if you look east one plus two which would be three sending the attack one box to the right.


JS
Sequence Reader Function

                
                    
      
  sequanceReader(seq,direction){
  let arr = []
  for (var i = 0; i < seq.length; i++) {
    arr.push(this.newDirection(seq[i],direction))

  }
  return arr
}

            
      
                
            

The first thing to do was change the sequence depending on direction. I wrote every sequence as if the player is facing nothing. Then I used a switch to read the player's direction. If they were facing east the sequence would be two plus the original number. That is like if north one and if you look east one plus two which would be three sending the attack one box to the right.

The first thing to do was change the sequence depending on direction. I wrote every sequence as if the player is facing nothing. Then I used a switch to read the player's direction. If they were facing east the sequence would be two plus the original number. That is like if north one and if you look east one plus two which would be three sending the attack one box to the right.


Day 1

Day 1