package com.gebauz.burutaru.game.entities;
import java.util.Vector;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.gebauz.bauzoid.graphics.sprite.AtlasSprite;
import com.gebauz.bauzoid.graphics.sprite.AtlasSpriteFrame;
import com.gebauz.bauzoid.graphics.sprite.AtlasSpriteInstance;
import com.gebauz.bauzoid.math.MathUtil;
import com.gebauz.bauzoid.math.Matrix4;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.math.collision.Shape;
import com.gebauz.bauzoid.math.collision.ShapeUtil;
import com.gebauz.burutaru.game.GameLogic;
import com.gebauz.burutaru.game.entities.PowerUps.Type;
public class BlobEnemies
extends EnemyList
<BlobEnemies.
Instance>
{
// Constants========================================================================================
public static final int BLOB_HEALTH =
10;
public static final int INITIAL_NUM_INSTANCES =
16;
public static final int NUM_FRAMES_X =
4;
public static final int NUM_FRAMES_Y =
4;
public static final int NUM_FRAMES = NUM_FRAMES_X
* NUM_FRAMES_Y
;
public static final int FRAME_SIZE =
128;
public static final int SPRITE_SIZE =
80;
public static final float ANIM_SPEED =
15;
public static final float MOVE_SPEED_X_MIN =
125;
public static final float MOVE_SPEED_Y_MIN =
175;
public static final float MOVE_SPEED_X_MAX =
170;
public static final float MOVE_SPEED_Y_MAX =
240;
// Embedded Types===================================================================================
public class Instance
extends Enemy
{
public AtlasSpriteInstance spriteInstance =
null;
public float lifeTime = 0.0f
;
public float moveSpeedX = 300.0f
;
public float moveSpeedY = 450.0f
;
public float phaseFactor = 1.0f
;
public Instance
(float x,
float y
)
{
super(BLOB_HEALTH
);
lifeTime = 0.0f
;
moveSpeedX = getGame
().
getRandomFloat(MOVE_SPEED_X_MIN, MOVE_SPEED_X_MAX
);
moveSpeedY = getGame
().
getRandomFloat(MOVE_SPEED_Y_MIN, MOVE_SPEED_Y_MAX
);
phaseFactor = getGame
().
getRandomFloat(0.7f, 1.5f
);
spriteInstance =
new AtlasSpriteInstance
(getGraphics
(), mBlobEnemySprite, mBlobFrames, mBlobShape
);
spriteInstance.
param.
x = x
;
spriteInstance.
param.
y = y
;
spriteInstance.
param.
w = SPRITE_SIZE
;
spriteInstance.
param.
h = SPRITE_SIZE
;
spriteInstance.
param.
pivotX =
0;
spriteInstance.
param.
pivotY = spriteInstance.
param.
h/
2;
spriteInstance.
param.
mirrorX =
true;
}
public void update
(float deltaTime
)
{
lifeTime += deltaTime
;
float moveY = MathUtil.
sin(lifeTime
* 360.0f
* phaseFactor
) * moveSpeedY
;
spriteInstance.
param.
y += moveY
* deltaTime
;
spriteInstance.
param.
x -= moveSpeedX
* deltaTime
;
// when reaching the left corner, the enemies are removed
// TODO: better definition
if (spriteInstance.
param.
x < -100.0f
)
vanish
();
}
public void render
()
{
int frame = MathUtil.
clamp((int)((lifeTime
* ANIM_SPEED
) % NUM_FRAMES
),
0, NUM_FRAMES-
1);
spriteInstance.
renderFrame(frame
);
}
@
Override
public void die
()
{
getGameLogic
().
getExplosions().
spawn(spriteInstance.
param.
x, spriteInstance.
param.
y);
}
@
Override
public boolean checkHit
(Shape shape, Matrix4 shapeTransform
)
{
// TODO: since blob enemy is simpler, we can just use simpler test
// shape is from bullet
Matrix4 myTransform = spriteInstance.
param.
getTransform();
Matrix4.
multiply(myTransform, shapeTransform, myTransform.
getInverse());
return shape.
intersects(spriteInstance.
getShape(), myTransform
);
}
@
Override
public float getX
()
{
return spriteInstance.
param.
x;
}
@
Override
public float getY
()
{
return spriteInstance.
param.
y;
}
}
// Fields===========================================================================================
private Shape mBlobShape =
null;
private AtlasSprite mBlobEnemySprite =
null;
private AtlasSpriteFrame mBlobFrames
[] =
null; //new AtlasSpriteInstance[NUM_FRAMES];
// Methods==========================================================================================
public BlobEnemies
(GameLogic gameLogic
)
{
super(gameLogic
);
mBlobEnemySprite =
new AtlasSprite
(getGraphics
(),
"data/textures/blob_enemy.png",
"data/textures/blob_enemy.txt");
}
@
Override
public void initAsync
()
{
mBlobEnemySprite.
initAsync();
}
@
Override
public void init
()
{
mBlobEnemySprite.
init();
mBlobEnemySprite.
getTexture().
setFilter(TextureFilter.
Linear, TextureFilter.
Linear);
mBlobFrames = mBlobEnemySprite.
createSpriteInstances();
mBlobShape = ShapeUtil.
createShapeFromFile("data/textures/blob_enemy.shape");
}
@
Override
public void exit
()
{
mBlobShape =
null;
for (int i =
0; i
< NUM_FRAMES
; i++
)
mBlobFrames
[i
] =
null;
mBlobFrames =
null;
if (mBlobEnemySprite
!=
null)
{
mBlobEnemySprite.
dispose();
mBlobEnemySprite =
null;
}
}
public void spawn
(float x,
float y
)
{
mEnemies.
add(new Instance
(x, y
));
}
public boolean checkForHit
(Shape shape, Matrix4 transform
)
{
boolean hitFound =
false;
/* int i = 0;
while (i < mEnemies.size())
{
Instance enemy = mEnemies.get(i);
if (enemy.checkForHit(x, y, w, h))
{
getGameLogic().getExplosions().spawn(enemy.x, enemy.y);
// spawn powerup
// TEMP:
int n = getGame().getRandomInt(1, 10);
if (n == 5)
{
int rand = getGame().getRandomInt(PowerUps.Type.SPEED_UP.ordinal(), PowerUps.Type.MISSILE.ordinal());
getGameLogic().getPowerUps().spawn(enemy.x, enemy.y, Type.values()[rand]);
}
mEnemies.remove(i);
hitFound = true;
continue;
}
++i;
}*/
return hitFound
;
}
/*public boolean checkForHit(float x, float y, float w, float h)
{
boolean hitFound = false;
int i = 0;
while (i < mEnemies.size())
{
Instance enemy = mEnemies.get(i);
if (enemy.checkForHit(x, y, w, h))
{
getGameLogic().getExplosions().spawn(enemy.x, enemy.y);
// spawn powerup
// TEMP:
int n = getGame().getRandomInt(1, 10);
if (n == 5)
{
int rand = getGame().getRandomInt(PowerUps.Type.SPEED_UP.ordinal(), PowerUps.Type.MISSILE.ordinal());
getGameLogic().getPowerUps().spawn(enemy.x, enemy.y, Type.values()[rand]);
}
mEnemies.remove(i);
hitFound = true;
continue;
}
++i;
}
return hitFound;
}*/
// Getters/Setters==================================================================================
}