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.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 Entity
{
// Constants========================================================================================
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
{
public float x
;
public float y
;
public float lifeTime = 0.0f
;
public float moveSpeedX = 300.0f
;
public float moveSpeedY = 450.0f
;
public float phaseFactor = 1.0f
;
public boolean isAlive =
false;
public Instance
(float _x,
float _y
)
{
x = _x
; y = _y
;
lifeTime = 0.0f
;
isAlive =
true;
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
);
}
public void update
(float deltaTime
)
{
lifeTime += deltaTime
;
float moveY = MathUtil.
sin(lifeTime
* 360.0f
* phaseFactor
) * moveSpeedY
;
y += moveY
* deltaTime
;
x -= moveSpeedX
* deltaTime
;
// when reaching the left corner, the enemies are removed
// TODO: better definition
if (x
< -100.0f
)
isAlive =
false;
}
public boolean checkForHit
(float _x,
float _y,
float w,
float h
)
{
return (((_x
) > (x - SPRITE_SIZE/
2)) &&
((_x
) < (x + SPRITE_SIZE/
2)) &&
((_y
) > (y - SPRITE_SIZE/
2)) &&
((_y
) < (y + SPRITE_SIZE/
2)));
/*return (((_x + w/2) > (x - SPRITE_SIZE/2)) &&
((_x - w/2) < (x + SPRITE_SIZE/2)) &&
((_y + h/2) > (y - SPRITE_SIZE/2)) &&
((_y - h/2) < (y + SPRITE_SIZE/2)));*/
}
public void render
()
{
int frame = MathUtil.
clamp((int)((lifeTime
* ANIM_SPEED
) % NUM_FRAMES
),
0, NUM_FRAMES-
1);
mBlobFrames
[frame
].
param.
x = x
;
mBlobFrames
[frame
].
param.
y = y
;
mBlobFrames
[frame
].
param.
w = SPRITE_SIZE
;
mBlobFrames
[frame
].
param.
h = SPRITE_SIZE
;
mBlobFrames
[frame
].
centerPivot();
mBlobFrames
[frame
].
render();
}
}
// Fields===========================================================================================
private Vector<Instance
> mEnemies =
new Vector<Instance
>();
private Shape mBlobShape =
null;
private AtlasSprite mBlobEnemySprite =
null;
private AtlasSpriteInstance 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;
}
}
@
Override
public void update
(float deltaTime
)
{
int i =
0;
while (i
< mEnemies.
size())
{
Instance enemy = mEnemies.
get(i
);
enemy.
update(deltaTime
);
if (!enemy.
isAlive)
{
// unspawn
mEnemies.
remove(i
);
continue;
}
++i
;
}
}
@
Override
public void render
()
{
for (int i =
0; i
< mEnemies.
size(); i++
)
{
mEnemies.
get(i
).
render();
}
}
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
;
}
public boolean getNearestEnemy
(float x,
float y, Vector2 result,
float previousDistanceSqr
)
{
boolean found =
false;
float distanceSqr = previousDistanceSqr
;
for (int i =
0; i
< mEnemies.
size(); i++
)
{
Instance enemy = mEnemies.
get(i
);
if (enemy.
isAlive)
{
float diffX = enemy.
x - x
;
float diffY = enemy.
y - y
;
float currentDistanceSqr = diffX
*diffX + diffY
*diffY
;
if ((distanceSqr
< 0) ||
(currentDistanceSqr
< distanceSqr
))
{
found =
true;
distanceSqr = currentDistanceSqr
;
result.
x = enemy.
x;
result.
y = enemy.
y;
}
}
}
return found
;
}
// Getters/Setters==================================================================================
}