Controls

a  d for left and rgiht

left mouse click or spacebar for shoot

About

Invaders speed up as you kill more of them.

Invaders shoot more as there are less of them.

Code

https://github.com/katpavan/space-invaders

Learned/Notes

  • restart game if all invaders are killed
    • using UnityEngine.SceneManagement;
    • if (this.invadersKilled >= totalInvaders)
              {            SceneManager.LoadScene(SceneManager.GetActiveScene().name);         }
  • used a curve to speed up the animation of invaders as you kill more of them
    • in Invaders.cs
      • public AnimationCurve speedCurve;
        • it's basically an x y graph
      • public int invadersKilled { get; private set; }
      • public int totalInvaders => this.rows * this.cols;
      • public float percentKilled => (float)this.invadersKilled / (float)this.totalInvaders;
      • this.transform.position += _direction * this.speedCurve.Evaluate(percentKilled) * Time.deltaTime;
    • then in the editor, you click on speedCurve in the Inspector section when you click on the Invaders game object.
      • and you select a curve
      • you right click -> edit key -> set a value for the time
        • 0 is the beginning of the animation
        • 1 is the end of the animation
  • public int invadersKilled { get; private set; }
    • public getter, private setter
    • meaning any method can get this but only this script can set it
  • public int totalInvaders => rows * cols; 
    • this is how we set a calculated variable
  • I forgot to set Is Trigger on the boundary box colliders and that was the reason the laser kept going into infinity and not stop at the boundary.
  • needed a callback to tell my player script that the projectile (laser) has collided
    • so I made an action and used c#'s delegate pattern
      • Projectile.cs
        • public System.Action destroyed;
        • this.destroyed.Invoke();
      • Player.cs
        • projectile.destroyed += LaserDestroyed;
          • we call LaserDestroyed when the destroyed delegate action gets invoked
          • this allows  scripts to register when an event happens from another script


  • I did another callback to tell my invaders script that my invader has been killed by a laser
    • Invader.cs
      • public System.Action killedInvader;
      • this.killedInvader.Invoke();
    • Invaders.cs
      • invader.killedInvader += IncreaseSpeed;
        • we call IncreaseSpeed when the killedInvader delegate action gets invoked from Invader.cs
        • did this in the for loop
  • after you change the sprite in the sprite renderer component, you can reset the box collider component by clicking on the 3 dots and hitting reset 
    • after you change the sprite in the sprite renderer component, you can reset the box collider component by clicking on the 3 dots and hitting reset 
  • after you change the sprite in the sprite renderer component, you can reset the box collider component by clicking on the 3 dots and hitting reset 
  • after you change the sprite in the sprite renderer component, you can reset the box collider component by clicking on the 3 dots and hitting reset 
    • this will resize the box collider according to this new sprite
  • sprite settings
    • changed the pixels per unit of my sprites from 100 to 16
      • 100 is too large because they were designed to be 16 pixels per unit
    • set filter mode to point
      • This setting allows you to control how the texture is filtered when stretched in the 3D space. 
        • Point (No Filter): Makes the texture appear block pixelated from the closest pixel. 
        • Bilinear: Makes the texture appear blurrily pixelated from the closest pixel
        • Trilinear: Blurs the image between different MIP levels.
    • changed Max Size to 64
    • changed Compression to None
  • Made a prefab for all of the invader and made three prefab variants for specific invaders 
  • Animated sprites in code

private void Start()
{        
InvokeRepeating(nameof(AnimateSprite), this.animationTime, this.animationTime);    

}        
private void AnimateSprite()    
{        
_animationFrame++;        
if(_animationFrame >= this.animationSprites.Length)        
{            
_animationFrame = 0;        
}        
_spriteRenderer.sprite = this.animationSprites[_animationFrame];    
}

Leave a comment

Log in with itch.io to leave a comment.