Bullet collision detection
Detecting bullet collisions is fairly straightforward. We loop through all the existing Bullet
objects held by our MachineGun
object. Next, we convert the points of each bullet into a RectHitBox
object and test it using intersects()
against each object in our viewport.
If we get a hit, we check to see what type of object it has hit. We then switch to handle each type of object that we care about. If it is a Guard
object, we knock it back a bit, if it is a Drone
object, we destroy it, and if it is anything else, we just make the bullet disappear and play a kind of thudding/ricochet sound.
We simply place this logic we discussed after our switch
block that handles collisions with the player, but before, we call update()
on all our unclipped objects as shown next:
default:// Probably a regular tile if (hit == 1) {// Left or right lm.player.setxVelocity(0); lm.player.setPressingRight(false); } if (hit == 2) {// Feet lm.player.isFalling...