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.ICollidable;
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;
import com.gebauz.burutaru.game.level.Level;
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;
private Level mLevel =
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));
mLevel = mEntities.
addEntity(new Level(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
);
mLevel.
update(deltaTime
);
checkShipHit
();
// 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
));
//mCarrotEnemies.spawn(GameConsts.VIRTUAL_SCREEN_WIDTH + 100.0f, 200);
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 level
// TODO: might be required to render it in separated passes (foreground/background)
mLevel.
renderBackground();
// 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();
mLevel.
renderForeground();
// render UI
mVirtualPad.
render();
}
public boolean checkShipHit
()
{
// TODO: maybe use more allocation-efficient data structures
Vector<ICollidable
> collidables = getCollidableList
();
AABoundingBox b = mShip.
getBounds();
boolean found =
false;
for (int i =
0; i
< collidables.
size(); i++
)
{
ICollidable collidableObject = collidables.
get(i
);
// check if the two things are in closer proximity
AABoundingBox a = collidableObject.
getBounds();
if (a ==
null)
continue;
if (!a.
intersetcs(b
))
{
// even bounding boxes do not intersect, so no need to test further
continue;
}
// detail check
if (collidableObject.
checkShipHit(mShip
))
{
mShip.
processHit(collidableObject
);
found =
true;
}
}
// check static level element collisions
// get level's ICollidables
// hmm..
// check static level element collisions
/*if (mLevel.checkShipHit())
found = true;*/
return found
;
}
public boolean checkProjectileHit
(Projectile projectile
)
{
// TODO: maybe use more allocation-efficient data structures
Vector<ICollidable
> collidables = getCollidableList
();
AABoundingBox b = projectile.
getBounds();
boolean found =
false;
for (int i =
0; i
< collidables.
size(); i++
)
{
ICollidable collidableObject = collidables.
get(i
);
// check if the two things are in closer proximity
AABoundingBox a = collidableObject.
getBounds();
if (a ==
null)
continue;
if (!a.
intersetcs(b
))
{
// even bounding boxes do not intersect, so no need to test further
continue;
}
// detail check
if (collidableObject.
checkHit(projectile.
getShape(), projectile.
getShapeTransform()))
{
projectile.
processHit(collidableObject
);
found =
true;
}
}
// check static level element collisions
/*if (mLevel.checkProjectileHit(projectile))
found = true;*/
return found
;
}
public boolean getNearestEnemyPosition
(float x,
float y, Vector2 result
)
{
ICollidable nearestEnemy = getNearestEnemy
(x, y
);
if (nearestEnemy ==
null)
return false;
result.
x = nearestEnemy.
getX();
result.
y = nearestEnemy.
getY();
return true;
}
public Enemy getNearestEnemy
(float x,
float y
)
{
Enemy foundEnemy =
null;
float distanceSqr =
Float.
MAX_VALUE;
Vector<Enemy
> enemies = getEnemyList
();
for (int i =
0; i
< enemies.
size(); i++
)
{
Enemy enemy = enemies.
get(i
);
if (enemy.
isAlive())
{
float diffX = enemy.
getX() - x
;
float diffY = enemy.
getX() - y
;
float currentDistanceSqr = diffX
*diffX + diffY
*diffY
;
if (currentDistanceSqr
< distanceSqr
)
{
foundEnemy = enemy
;
distanceSqr = currentDistanceSqr
;
}
}
}
return foundEnemy
;
}
public Vector<ICollidable
> getCollidableList
()
{
Vector<ICollidable
> collidables =
new Vector<ICollidable
>();
mCarrotEnemies.
fillCollidableList(collidables
);
mBlobEnemies.
fillCollidableList(collidables
);
mLevel.
fillCollidableList(collidables
);
return collidables
;
}
public Vector<Enemy
> getEnemyList
()
{
Vector<Enemy
> enemies =
new Vector<Enemy
>();
mCarrotEnemies.
fillEnemyList(enemies
);
mBlobEnemies.
fillEnemyList(enemies
);
return enemies
;
}
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
; }
}