Having spent the last post substantially re-writing the code, I was going to code a new danmaku pattern. But then, I thought it would be interesting to be able to cycle through different danmaku patterns, by taking advantage of the functionality to configure danmaku object, that I built in the last post. I could simply store a variety of danmaku configuration parameters in an array, and cycle through them, perhaps at the push of the Return button. Should be straightforward.
In reality, the exercise revealed a lot of problems in the code that required more "clean up".
Method to "reset" a live danmaku
In the previous version of the code, I had build a "reset" functionality into the "setProperties" method. However, as I was building on the cycling through patterns demo, splitting the setting up of properties and "erasing" the properties into 2 different functions felt more intuitive, and slightly easier to read.
Triggering mechanism for danmaku
For the aske of this demo, the danmaku is endless. I have re-jigged the code so that the endless timer event is set up automatically if the loop property is set to -1, hence no need to manually call the triggerRepeatFire method. For the player, we will call the trigger RepeatFire method. The bits and bobs for the intermittent firing mechanism has been removed in its entirely since it was getting too confusing. This is something that will be re-written in the future.
Setting up the array of cannon angles for N-way
There is a convoluted code for setting up an array of cannon angles for N-Way, involving number of if-statements. Although the logic has not been changed, I have moved the entire code to a separate function.
Sound parameter now included as danamku object property
The sound effect when bullets are shot are now included as a property of the danmaku object. If nothing is set, then no sound will play. It means you can implement different shooting sound effects for each danmaku class (or enemy type).
As with pretty much every post, I have changed the name of some properties, the most important one being the amendment of angleVelocity to angularVelocity. This is simply to make it more consistent with the Phaser naming convention.
For this demo, I have got rid of the explosion effects because it was too distracting when viewing the more intense danmaku patterns.
Killing of bullets off screen
I have changed the code to get rid of bullets that have flown off screen. Until now, I used to use a collider event to get rid of the bullet. However that meant that a bullet would disappear as soon as it hit the screen, ie before it was fully outside the screen. I have got rid of the collider event, and instead I now check whether a bullet is completely outside the screen in the preUpdate loop, and then disable it.
preUpdate(time, delta) {
if (this.outOfScreen(this)) {
this.disableBody(true, true);
}
The function to check whether a bullet is out of the screen is below (strictly speaking, we should add or subtract half the display width/height in the criteria).
outOfScreen(bullet) {
return (
bullet.x < 0 || bullet.x > WIDTH || bullet.y < 0 || bullet.y > HEIGHT
);
} // function to detect when bullet out of screen area
At the same time, I have increased the size of the enemy bullet pool to 1000
Trigger to cycle through the danmaku patterns
Anyway, in the end I got the demo working. By clicking on the "<<PREV" and "NEXT>>" buttons on the top half of the screen, you can cycle through the following danmaku patterns, all of which have been covered in prior posts (but the underlying code now looks very different from the one at the beginning of these series):
Directional
Aiming Directional
Directional With Accelerating Bullets
Directional With Decelerating Bullets
Aiming Directional With Accelerating Bullets
Right Spiral
Left Spiral (Tight)
Right Multiple Spiral
Left Multiple Spiral With Accelerating Bullets
Single Right Spiral With Bending Bullets
Single Left Spiral With Accelerating Bending Bullets
Right Multi-Spiral With Bending Bullets
Bi-Directional Multiple Spiral
7-Way
8-Way With Decelerating Bullets
6-Way With Reversing Bullets
7-Way Circle
32-Way Circle
32-Way Gap Circle
32-Way Circle With Bending Bullets
32-Way Circle With Accelerating Bending Bullets
64-Way Semi-Circle
Aiming 9-Way
10-Way Randomised
Rolling 5-Way
Rolling 16-Way Circle
Waving 5-Way
Waving 8-Way Circle
As always the CodePen is available here, for your perusal and comment.
Bình luận