package model_pkg.gameobject_pkg;
import model_pkg.GfxState;
import def_classes.*;
import display_pkg.*;
import def_classes.Box;
import java.util.Random;
/**
* An enemy type that will randomly move around the world and shoot smoke.
* @author Kullman K.
*/
public class Towelie extends GameObject{
private int minSpeed = 9; //
private int maxSpeed = 29; //
private int maxHoriSpeed = 10; //
private int maxVertSpeed = 10; //
private int cooldown = 40; // [frames]
private int cooldownCounter = cooldown; //
private int moveTime; //
private Random randG; //
/**
* Creates a Towelie
* @param x start x-position
* @param y start y-position
* @param bbox bounding box
* @param sprSheet the sprite sheet containing the graphics for this object
*/
public Towelie(int x, int y, Box bbox, SpriteSheet sprSheet) {
super(x, y, bbox);
currentAnim = new GfxState(sprSheet, 0);
randG = new Random();
}
/**
* Collisionhandler for this specific object. Being ethereal he's unaffected
* by any collisions.
*
*/
public Vector2D collisionOccured(GameObject obj, Vector2D offsetDisplacement){
return getMovement();
}
/**
* Typical object update
*/
public void updateObject() {
updateSpeed();
reboundVelocity();
updatePosition();
processShooting();
updateSpriteBase();
}
/**
* Shoots smoke if the cooldown has come down to 0
*/
private void processShooting() {
cooldownCounter -= 1;
int nShots;
if (cooldownCounter <= 0) {
cooldownCounter = cooldown;
Point pos = new Point(25, 25);
pos.add(getWorldPosition());
TowelieSmokeFactory.setTemplatePosition(pos);
nShots = randG.nextInt(1);
for (int i = 0; i <= nShots; i++) {
TowelieSmokeFactory.setTemplateVelocity(new Vector2D(getVelocity().getX() + randG.nextInt(5), getVelocity().getY() + randG.nextInt(5)));
myWorld.createInstance(TowelieSmokeFactory.getFactory());
}
}
}
/**
* Limits the velocity
*/
private void reboundVelocity() {
getVelocity().approximateToZero();
if (getVelocity().getX() < -maxHoriSpeed) {
setVelocityX(-maxHoriSpeed);
} else if (getVelocity().getX() > maxHoriSpeed) {
setVelocityX(maxHoriSpeed);
}
if (getVelocity().getY() < -maxVertSpeed) {
setVelocityY(-maxVertSpeed);
} else if (getVelocity().getY() > maxVertSpeed) {
setVelocityY(maxVertSpeed);
}
}
/**
* Adds random speed to the enemy object
*/
private void updateSpeed() {
if (moveTime == 0) {
getVelocity().addRelX(randG.nextInt(maxSpeed-minSpeed)-minSpeed);
getVelocity().addRelY(randG.nextInt(maxSpeed-minSpeed)-minSpeed);
moveTime = 25;
} else {
getVelocity().scalarMultRel(0.98);
moveTime--;
}
setMovement(this.getVelocity());
}
/**
* Check so that the movement doesn't take the enemy outside the screen
* and updates the position. It bounces on the edges of the world.
*/
private void updatePosition() {
setMovement(myWorld.reboundMovement(getBoundingBox(), getWorldPosition(), getMovement()));
addToWorldPosition(getMovement());
setVelocity(getMovement());
}
}