Enemies Answers

110. In order that the tasks are described:

 
 

So here we first subtract damage from hp, while accounting for the possibility that damage is nil and in that case defaulting to 100 (damage or 100). After that we check to see if HP is below 0 or not, if it is we kill the object by setting dead to true and then spawn an Ammo resource (as all enemies should do when killed). If it isn't less than 0 then we set hit_flash to true and set it back to false 0.2 seconds later using timer:after.

The final thing we should do is change the Rock:draw function to take into account the hit_flash attribute:

 

Here we just added the second line as the 4 others were already there.


111. With this question we can literally just copy the code from ProjectileDeathEffect in its entirety and just change the name to EnemyDeathEffect. The only thing we have to do to make it look like it does in the gifs is make sure that when calling it, we pass the appropriate w value, otherwise it will look too small:

 

A good question to ask here would be why not just use the ProjectileDeathEffect object instead of creating a new one? And I really don't have a good answer for that. I personally prefer to create different objects if they are serving different purposes, even though they might share the same code (and in this extreme case, their code is actually exactly the same). This is just my preference though and I have no real rational explanation to it. It's perfectly reasonable to just skip creating this new EnemyDeathEffect object and just make use of the effect that already exists.


112. We want to capture collision events between objects of the Projectile collision class and the Enemy collision class. The only object of the Projectile collision class we have in the game right now is the Projectile object itself, and so we can start placing our code there:

 

At first we have the default collision capturing code. The object variable here will contain a reference to the enemy that we collided with. First we want to check if this variable actually exists (it isn't nil), and then we want to do what the exercise asks us to do (call hit on it and then call die on self):

 

Here we use self.damage which the exercise asks us to define as 100 in the Projectile object's constructor.


113. In order that the tasks are described:

Add a function named hit to the Player class

 

It should receive a damage argument, and in case it isn't defined it should default to 10

 

This function should not do anything whenever the invincible attribute is true

 

In general whenever you want to not operate on some condition, place that condition at the top of function and return if its fulfilled.

Between 4 and 8 ExplodeParticle objects should be spawned

 

The addHP (or removeHP function if you decided to add this one) should take the damage attribute and use it to remove HP from the Player. Inside the addHP (or removeHP) function there should be a way to deal with the case where hp hits or goes below 0 and the player dies.

 

And the removeHP function looks like this:

 

It simply removes the amount of HP passed in and then calls die whenever the hp attribute goes below 0.

If the damage received is equal to or above 30, then the invincible attribute should be set to true and 2 seconds later it should be set to false. On top of that, the camera should shake with intensity 6 for 0.2 seconds, the screen should flash for 3 frames, and the game should be slowed to 0.25 for 0.5 seconds. Finally, an invisible attribute should alternate between true and false every 0.04 for the duration that invincible is set to true, and additionally the Player's draw function shouldn't draw anything whenever invisible is set to true.

 

Note that the for loop that calls timer:after a bunch of times could have been swapped for an every call instead with the for loop removed. Both are valid ways of achieving the same goal.

If the damage received is below 30, then the camera should shake with intensity 6 for 0.1 seconds, the screen should flash for 2 frames, and the game should be slowed to 0.75 for 0.25 seconds.

 

This hit function should be called whenever the Player collides with an Enemy. The player should be hit for 30 damage on enemy collision.

 

Here we use the same code as we did for the projectile whenever if hits an enemy. In general whenever we want to capture collision events between two entities the code will look kinda like this.


114. This one is pretty straightforward and it just follows from the previous exercises:

 

There's really no mystery to it. We can just use the general collision event template and then replace the meaningful part with what we want to do, which in this case is calling die on both objects.


115. The direction attribute is named improperly because it doesn't refer to the direction that the object is moving towards, which is what one would naturally assume. It refers to the direction in which the object is spawned (left or right), and so when we want to use it thinking it would be referring to the direction of movement of the object it might be confusing. A better name for it would be spawn_direction, and then it follows that direction of movement of the object would always be -spawn_direction.