77 lines
2.3 KiB
Java
Executable File
77 lines
2.3 KiB
Java
Executable File
package player.gun.bullets;
|
|
|
|
import static sjgs.utils.Utils.isPositive;
|
|
import java.awt.Graphics2D;
|
|
import core.Main;
|
|
import sjgs.enums.Facing;
|
|
import sjgs.graphics.Animation;
|
|
import sjgs.physics.structs.CollisionResponse;
|
|
import sjgs.utils.data_structures.vectors.Point2f;
|
|
import textures.player.PlayerGunTextures;
|
|
|
|
public class DefaultBullet extends PlayerBullet {
|
|
|
|
public static final String sound = "/tir.mp3";
|
|
|
|
private static final int w = 8, h = 1, health_timer = 18;
|
|
private final Animation animation;
|
|
private final Point2f p;
|
|
private final Facing facing;
|
|
|
|
/** @constructor: velX / velY are the player's velocity at initial shot */
|
|
public DefaultBullet(final Point2f p, final Facing facing, final float velX, final float velY) {
|
|
// note the +2 and +1, leave them!
|
|
super(p.x + 2, p.y + 1, facing == Facing.RIGHT || facing == Facing.LEFT ? w : h, facing == Facing.RIGHT || facing == Facing.LEFT ? h : w, velX, velY, 0, health_timer);
|
|
this.p = p; this.facing = facing;
|
|
switch(facing) {
|
|
case RIGHT: animation = PlayerGunTextures.default_gun_right.clone(); break;
|
|
case LEFT: animation = PlayerGunTextures.default_gun_left.clone(); break;
|
|
case ABOVE: animation = PlayerGunTextures.default_gun_up.clone(); break;
|
|
default: animation = PlayerGunTextures.default_gun_down.clone(); break; // (down)
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void init() {}
|
|
|
|
@Override
|
|
public void tick() {
|
|
final CollisionResponse response = getCollidedObjects();
|
|
|
|
// do collision stuff here
|
|
|
|
response.discard();
|
|
|
|
animation.playAnimationOnce();
|
|
|
|
if(isMoving()) {
|
|
// if player changed directions since last shot, set the bullet velocity to zero
|
|
final float pvx = Main.player.getVelX(), pvy = Main.player.getVelY(), vx = getVelX(), vy = getVelY();
|
|
if(pvx == 0 || isPositive(vx) ^ isPositive(pvx)) setVelX(0);
|
|
if(pvy == 0 || isPositive(vy) ^ isPositive(pvy)) setVelY(0);
|
|
// update animation position
|
|
p.x += getVelX();
|
|
p.y += getVelY();
|
|
}
|
|
|
|
// NOTE YOU DO NEED THIS, it updates the bounds pos when running to match animation
|
|
updatePosition();
|
|
|
|
if(timer.tick()) destroy();
|
|
}
|
|
|
|
@Override
|
|
public void render(final Graphics2D g2d) {
|
|
if(animation != null) animation.drawAnimation(g2d, p);
|
|
|
|
// g2d.setColor(Color.blue);
|
|
// getFullBounds().draw(g2d);
|
|
}
|
|
|
|
@Override
|
|
protected void destroy() {
|
|
removeFromHandler();
|
|
}
|
|
|
|
}
|