package com.gebauz.burutaru.game;
import java.util.Vector;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.gebauz.bauzoid.game.Game;
import com.gebauz.bauzoid.game.GameObject;
import com.gebauz.bauzoid.game.entities.EntityManager;
import com.gebauz.bauzoid.graphics.sprite.Sprite;
import com.gebauz.bauzoid.graphics.sprite.SpriteInstance;
import com.gebauz.bauzoid.graphics.sprite.SpriteParameters;
import com.gebauz.bauzoid.math.Matrix4;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.math.collision.AABoundingBox;
import com.gebauz.bauzoid.math.collision.Shape;
import com.gebauz.burutaru.GameConsts;
import com.gebauz.burutaru.game.entities.BeamShots;
import com.gebauz.burutaru.game.entities.BlobEnemies;
import com.gebauz.burutaru.game.entities.CarrotEnemies;
import com.gebauz.burutaru.game.entities.Enemy;
import com.gebauz.burutaru.game.entities.Explosions;
import com.gebauz.burutaru.game.entities.ImpactExplosions;
import com.gebauz.burutaru.game.entities.PlasmaShots;
import com.gebauz.burutaru.game.entities.PowerUpBomb;
import com.gebauz.burutaru.game.entities.PowerUpMissile;
import com.gebauz.burutaru.game.entities.PowerUps;
import com.gebauz.burutaru.game.entities.Projectile;
import com.gebauz.burutaru.game.entities.Ship;
import com.gebauz.burutaru.game.entities.StarField;
import com.gebauz.burutaru.game.entities.VirtualPad;
public class GameLogic
extends GameObject
{
// Constants========================================================================================
public static final float SPAWN_INTERVAL_MIN = 0.2f
;
public static final float SPAWN_INTERVAL_MAX = 1.5f
;
// Embedded Types===================================================================================
// Fields===========================================================================================
private EntityManager mEntities =
new EntityManager
();
private Ship mShip =
null;
private ShipController mShipController =
null;
private ShipParameters mShipParameters =
null;
private PlasmaShots mPlasmaShots =
null;
private BeamShots mBeamShots =
null;
private BlobEnemies mBlobEnemies =
null;
private CarrotEnemies mCarrotEnemies =
null;
private Explosions mExplosions =
null;
private VirtualPad mVirtualPad =
null;
private PowerUps mPowerUps =
null;
private PowerUpMissile mPowerUpMissile =
null;
private PowerUpBomb mPowerUpBomb =
null;
private ImpactExplosions mImpactExplosions =
null;
private StarField mStarField =
null;
// TEMP:
private Sprite mBackground =
null;
private float mSpawnTimer = 0.0f
;
private float mGlobalScrollX = 0.0f
;
private float mGlobalScrollY = 0.0f
;
// Methods==========================================================================================
public GameLogic
(Game game
)
{
super(game
);
// TEMP:
mBackground =
new Sprite
(getGraphics
(),
"data/textures/background.png");
}
public void createEntities
()
{
mShip = mEntities.
addEntity(new Ship
(this));
mPlasmaShots = mEntities.
addEntity(new PlasmaShots
(this));
mBeamShots = mEntities.
addEntity(new BeamShots
(this));
mBlobEnemies = mEntities.
addEntity(new BlobEnemies
(this));
mCarrotEnemies = mEntities.
addEntity(new CarrotEnemies
(this));
mExplosions = mEntities.
addEntity(new Explosions
(this));
mVirtualPad = mEntities.
addEntity(new VirtualPad
(this));
mPowerUps = mEntities.
addEntity(new PowerUps
(this));
mPowerUpMissile = mEntities.
addEntity(new PowerUpMissile
(this));
mPowerUpBomb = mEntities.
addEntity(new PowerUpBomb
(this));
mImpactExplosions = mEntities.
addEntity(new ImpactExplosions
(this));
mStarField = mEntities.
addEntity(new StarField
(this));
}
public void initAsync
()
{
createEntities
();
mEntities.
initAsync();
// TEMP:
mBackground.
initAsync();
}
public void init
()
{
mEntities.
init();
// TEMP:
mBackground.
init();
mBackground.
getTexture().
setFilter(TextureFilter.
Linear, TextureFilter.
Linear);
mShipController =
new ShipController
(this);
mShipParameters =
new ShipParameters
(this);
//mCarrotEnemies.spawn(300.0f, 200.0f);
}
public void exit
()
{
mShipController =
null;
mShipParameters =
null;
mEntities.
exit();
}
public void update
(float deltaTime
)
{
mGlobalScrollX = -75.0f
;
//mGlobalScrollY = -15.0f;
mShipController.
handleInput(deltaTime
);
mShip.
update(deltaTime
);
mPlasmaShots.
update(deltaTime
);
mBeamShots.
update(deltaTime
);
mBlobEnemies.
update(deltaTime
);
mCarrotEnemies.
update(deltaTime
);
mExplosions.
update(deltaTime
);
mPowerUps.
update(deltaTime
);
mPowerUpMissile.
update(deltaTime
);
mPowerUpBomb.
update(deltaTime
);
mImpactExplosions.
update(deltaTime
);
mStarField.
update(deltaTime
);
// TEMP:
mSpawnTimer -= deltaTime
;
if (mSpawnTimer
< 0.0f
)
{
int which = getGame
().
getRandomInt(0,
1);
if (which ==
0)
mBlobEnemies.
spawn(GameConsts.
VIRTUAL_SCREEN_WIDTH + 100.0f, getGame
().
getRandomFloat(100.0f, GameConsts.
VIRTUAL_SCREEN_HEIGHT - 100.0f
));
else if (which ==
1)
mCarrotEnemies.
spawn(GameConsts.
VIRTUAL_SCREEN_WIDTH + 100.0f, getGame
().
getRandomFloat(100.0f, GameConsts.
VIRTUAL_SCREEN_HEIGHT - 100.0f
));
mSpawnTimer = getGame
().
getRandomFloat(SPAWN_INTERVAL_MIN, SPAWN_INTERVAL_MAX
);
}
}
public void render
()
{
// TEMP:
mBackground.
param.
x =
0;
mBackground.
param.
y =
0;
mBackground.
param.
w = GameConsts.
VIRTUAL_SCREEN_WIDTH;
mBackground.
param.
h = GameConsts.
VIRTUAL_SCREEN_HEIGHT;
mBackground.
param.
pivotX =
0;
mBackground.
param.
pivotY =
0;
mBackground.
render();
// render background
mStarField.
render();
// render powerups
mPowerUps.
render();
// render weapons
mPlasmaShots.
render();
mBeamShots.
render();
mPowerUpMissile.
render();
mPowerUpBomb.
render();
// render enemies
mBlobEnemies.
render();
mCarrotEnemies.
render();
// render ships
mShip.
render();
// render effects
mExplosions.
render();
mImpactExplosions.
render();
// render UI
mVirtualPad.
render();
}
public boolean checkProjectileHit
(Projectile projectile
)
{
// TODO: maybe use more allocation-efficient data structures
Vector<Enemy
> enemies =
new Vector<Enemy
>();
mCarrotEnemies.
fillEnemyList(enemies
);
mBlobEnemies.
fillEnemyList(enemies
);
AABoundingBox b = projectile.
getBounds();
boolean found =
false;
for (int i =
0; i
< enemies.
size(); i++
)
{
Enemy enemy = enemies.
get(i
);
// check if the two things are in closer proximity
AABoundingBox a = enemy.
getBounds();
if (!a.
intersetcs(b
))
{
// even bounding boxes do not intersect, so no need to test further
continue;
}
// detail check
if (enemy.
checkHit(projectile.
getShape(), projectile.
getShapeTransform()))
{
projectile.
processHit(enemy
);
found =
true;
}
}
return found
;
}
/*public boolean checkForHit(Shape shape, Matrix4 transform)
{
boolean result = false;
if (mBlobEnemies.checkForHit(shape, transform))
result = true;
if (mCarrotEnemies.checkForHit(shape, transform))
result = true;
return result;
}*/
/*
public boolean checkForHit(float x, float y, float w, float h)
{
boolean result = false;
if (mBlobEnemies.checkForHit(x, y, w, h))
result = true;
if (!result && mCarrotEnemies.checkForHit(x, y, w, h))
result = true;
// TODO: more enemy types here:
return result;
}*/
// TODO: maybe it's time to generalize an Enemy interface
public boolean getNearestEnemyPosition
(float x,
float y, Vector2 result
)
{
float distanceSqr = -
1;
boolean found = mBlobEnemies.
getNearestEnemy(x, y, result, distanceSqr
);
found = mCarrotEnemies.
getNearestEnemy(x, y, result, distanceSqr
) || found
;
return found
;
}
public boolean isOutOfScreen
(SpriteParameters param
)
{
return ((param.
x > (GameConsts.
VIRTUAL_SCREEN_WIDTH + param.
w)) ||
(param.
x < (-param.
w)) ||
(param.
y > (GameConsts.
VIRTUAL_SCREEN_HEIGHT + param.
h)) ||
(param.
y < (-param.
h)));
}
// Getters/Setters==================================================================================
public final Ship getShip
() { return mShip
; }
public final PlasmaShots getPlasmaShots
() { return mPlasmaShots
; }
public final BeamShots getBeamShots
() { return mBeamShots
; }
public final Explosions getExplosions
() { return mExplosions
; }
public final VirtualPad getVirtualPad
() { return mVirtualPad
; }
public final PowerUps getPowerUps
() { return mPowerUps
; }
public final PowerUpMissile getPowerUpMissile
() { return mPowerUpMissile
; }
public final PowerUpBomb getPowerUpBomb
() { return mPowerUpBomb
; }
public final ImpactExplosions getImpactExplosions
() { return mImpactExplosions
; }
public final ShipParameters getShipParameters
() { return mShipParameters
; }
public final float getGlobalScrollX
() { return mGlobalScrollX
; }
public final float getGlobalScrollY
() { return mGlobalScrollY
; }
}