public void takeDamage(int damage) { super.takeDamage(damage / 2); System.out.println("Supersoldier took only " + damage / 2 + " damage!"); }
/** * Handles hard physical collisions * * @param other The object to check collisions against * @return 1 if there was a collision, -1 otherwise */ protected int handleCollisions(PhysObject other) { int code = -1; Vec2 newPosition = position.add(velocity.multiply(Globals.timeStep)); // If the new coords cause a collision if (this.circularIntersects(other, newPosition)) { code = 1; Vec2 fromOther = this.position.subtract(other.position); double massRatio = other.mass / this.mass; // If they were to our left and heading to the right // or if they were to our right and heading to the left if ((fromOther.x > 0 && other.velocity.x > 0) || (fromOther.x < 0 && other.velocity.x < 0)) { // Add their x velocity to ours // If we are moving in the same direction, don't apply the ratio if ((this.velocity.x > 0 && other.velocity.x > 0) || (this.velocity.x < 0 && other.velocity.x < 0)) { this.velocity.x += other.velocity.x; } else // If we are moving in different directions, apply the ratio { this.velocity.x += other.velocity.x * massRatio; } } // If they were to our left and heading to the left // or if they were to our right and heading to the right else { // Bounce this.velocity.x = -(this.velocity.x / 4); } // If they were above us and heading down // or if they were below us and heading up if ((fromOther.y > 0 && other.velocity.y > 0) || (fromOther.y < 0 && other.velocity.y < 0)) { // Add their x velocity to ours // If we are moving in the same direction, don't apply the ratio if ((this.velocity.y > 0 && other.velocity.y > 0) || (this.velocity.y < 0 && other.velocity.y < 0)) { this.velocity.y += other.velocity.y; } else // If we are moving in different directions, apply the ratio { this.velocity.y += other.velocity.y * massRatio; } } // If they were above us and heading up // or if they were below us and heading down else { // Bounce this.velocity.y = -(this.velocity.y / 4); } if (this.getClass() == Player.class) { ((Player) this).takeDamage(other.mass); } else if (this.getClass() == Enemy.class) { ((Enemy) this).takeDamage(other.mass); } } return code; }