package com.gebauz.burutaru.game.entities;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.gebauz.bauzoid.app.Consts;
import com.gebauz.bauzoid.graphics.RenderUtil;
import com.gebauz.bauzoid.graphics.sprite.Sprite;
import com.gebauz.bauzoid.graphics.sprite.SpriteInstance;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.math.Vector4;
import com.gebauz.bauzoid.math.collision.Shape;
import com.gebauz.burutaru.GameConsts;
import com.gebauz.burutaru.game.GameLogic;
import com.gebauz.burutaru.game.entities.CollisionWorld.CollisionInfo;
public class PlasmaShots
extends Entity
{
// Constants========================================================================================
public final static int PLASMA_SHOT_DAMAGE =
10;
final static int MAX_PLASMA_SHOTS =
10;
final static int MAX_IMPACTS =
50;
final static float PLASMA_SHOT_WIDTH =
40;
final static float PLASMA_SHOT_HEIGHT =
10;
final static float PLASMA_SHOT_SPEED =
800;
final static float IMPACT_LIFE_TIME = 0.1f
;
final static float IMPACT_BASE_SIZE =
5;
final static float IMPACT_GROWTH =
10;
// Embedded Types===================================================================================
public class Shot
extends Projectile
{
public SpriteInstance spriteInstance =
null;
public Shot
()
{
super(PLASMA_SHOT_DAMAGE
);
//spriteInstance = new SpriteInstance(getGraphics(), mShotSprite, mShotShape);
spriteInstance = mShotSprite.
createSpriteInstance(0);
setAlive
(false);
}
public void spawn
(float x,
float y
)
{
setAlive
(true);
spriteInstance.
transform.
x = x
;
spriteInstance.
transform.
y = y
;
spriteInstance.
transform.
w = PLASMA_SHOT_WIDTH
;
spriteInstance.
transform.
h = PLASMA_SHOT_HEIGHT
;
spriteInstance.
transform.
pivotX =
0;
spriteInstance.
transform.
pivotY = PLASMA_SHOT_HEIGHT/
2;
}
float x
;
float y
;
float w
;
float h
;
public void update
(float deltaTime
)
{
mShotShape.
transform.
set(spriteInstance.
transform);
//CollisionResult result = getGameLogic().getCollisionWorld().checkCollision(mShipCollider, new Vector2(mLastMoveX, mLastMoveY));
//CollisionInfo result = getGameLogic().getCollisionWorld().collideWithResponse(mShotShape, new Vector2(PLASMA_SHOT_SPEED * deltaTime, 0.0f));
Vector2 pos =
new Vector2
(spriteInstance.
transform.
x, spriteInstance.
transform.
y);
Vector2 ray =
new Vector2
(spriteInstance.
transform.
w + 1.5f
*PLASMA_SHOT_SPEED
* deltaTime,
0);
x = spriteInstance.
transform.
x;
y = spriteInstance.
transform.
y;
w = spriteInstance.
transform.
w + PLASMA_SHOT_SPEED
* deltaTime
;
h = spriteInstance.
transform.
h;
CollisionInfo result = getGameLogic
().
getCollisionWorld().
collideRaycast(CollisionWorld.
FILTER_LEVEL_ELEMENTS, pos, ray
);
if (result.
isColliding)
{
// hit a static level object object
if (result.
collidingLevelObject !=
null)
result.
collidingLevelObject.
projectileHit(PLASMA_SHOT_DAMAGE
);
if (result.
collidingEnemyInstance !=
null)
result.
collidingEnemyInstance.
projectileHit(PLASMA_SHOT_DAMAGE
);
spawnImpact
(result.
contactPoint.
x, result.
contactPoint.
y);
setAlive
(false);
return;
}
CollisionInfo result2 = getGameLogic
().
getCollisionWorld().
collideRaycast(CollisionWorld.
FILTER_ENEMIES, pos, ray
);
if (result2.
isColliding)
{
// hit a static level object object
if (result2.
collidingLevelObject !=
null)
result2.
collidingLevelObject.
projectileHit(PLASMA_SHOT_DAMAGE
);
if (result2.
collidingEnemyInstance !=
null)
result2.
collidingEnemyInstance.
projectileHit(PLASMA_SHOT_DAMAGE
);
spawnImpact
(result2.
contactPoint.
x, result2.
contactPoint.
y);
setAlive
(false);
return;
}
spriteInstance.
transform.
x += PLASMA_SHOT_SPEED
* deltaTime
;
//getGameLogic().checkProjectileHit(this);
if (spriteInstance.
transform.
x > GameConsts.
VIRTUAL_SCREEN_WIDTH + PLASMA_SHOT_WIDTH
)
{
setAlive
(false);
}
}
public void render
()
{
spriteInstance.
render();
//RenderUtil.drawArrow(getGraphics(), x, y, x + w, y, new Vector4(1, 0, 0, 1));
//mShotShape.renderDebug(getGraphics(), spriteInstance.transform);
}
}
public class Impact
{
public float time = -
1;
public SpriteInstance spriteInstance =
null;
public Impact
()
{
spriteInstance = mImpactSprite.
createSpriteInstance(0);
}
public void spawn
(float x,
float y
)
{
time = IMPACT_LIFE_TIME
;
spriteInstance.
transform.
x = x
;
spriteInstance.
transform.
y = y
;
spriteInstance.
transform.
w = IMPACT_BASE_SIZE
;
spriteInstance.
transform.
h = IMPACT_BASE_SIZE
;
spriteInstance.
transform.
centerPivot();
}
public void update
(float deltaTime
)
{
time -= deltaTime
;
final float s = 1.0f -
(time / IMPACT_LIFE_TIME
);
spriteInstance.
transform.
w = IMPACT_BASE_SIZE + IMPACT_GROWTH
* s
;
spriteInstance.
transform.
h = IMPACT_BASE_SIZE + IMPACT_GROWTH
* s
;
spriteInstance.
centerPivot();
}
public void render
()
{
spriteInstance.
render();
}
public boolean isAlive
()
{
return (time
>= 0.0f
);
}
}
// Fields===========================================================================================
private Sprite mShotSprite =
null;
private Shape mShotShape =
null;
private Sprite mImpactSprite =
null;
private Shot mShotList
[] =
new Shot
[MAX_PLASMA_SHOTS
];
private Impact mImpactList
[] =
new Impact
[MAX_IMPACTS
];
private Sound mShotSound =
null;
// Methods==========================================================================================
public PlasmaShots
(GameLogic gameLogic
)
{
super(gameLogic
);
mShotSprite =
new Sprite
(getGraphics
(),
"data/textures/plasma_shot.png");
mShotShape =
new Shape(getGame
(),
"data/textures/plasma_shot.shape");
mImpactSprite =
new Sprite
(getGraphics
(),
"data/textures/plasma_shot_impact.png");
}
@
Override
public void initAsync
()
{
mShotSprite.
initAsync();
mShotShape.
initAsync();
mImpactSprite.
initAsync();
}
@
Override
public void init
()
{
mShotSound = getAudio
().
newManagedSound("data/sounds/plasma_shot.wav");
mShotShape.
init();
mShotSprite.
init();
mShotSprite.
getTexture().
setFilter(TextureFilter.
Linear, TextureFilter.
Linear);
mImpactSprite.
init();
mImpactSprite.
getTexture().
setFilter(TextureFilter.
Linear, TextureFilter.
Linear);
for (int i =
0; i
< MAX_PLASMA_SHOTS
; i++
)
{
mShotList
[i
] =
new Shot
();
}
for (int i =
0; i
< MAX_IMPACTS
; i++
)
{
mImpactList
[i
] =
new Impact
();
}
}
@
Override
public void exit
()
{
mShotShape =
null;
if (mShotSprite
!=
null)
{
mShotSprite.
dispose();
mShotSprite =
null;
}
if (mShotSound
!=
null)
{
getAudio
().
removeManagedSound(mShotSound
);
mShotSound =
null;
}
}
@
Override
public void update
(float deltaTime
)
{
for (int i =
0; i
< MAX_PLASMA_SHOTS
; i++
)
{
if (mShotList
[i
].
isAlive())
mShotList
[i
].
update(deltaTime
);
}
for (int i =
0; i
< MAX_IMPACTS
; i++
)
{
if (mImpactList
[i
].
isAlive())
mImpactList
[i
].
update(deltaTime
);
}
}
@
Override
public void render
()
{
for (int i =
0; i
< MAX_PLASMA_SHOTS
; i++
)
{
if (mShotList
[i
].
isAlive())
mShotList
[i
].
render();
}
for (int i =
0; i
< MAX_IMPACTS
; i++
)
{
if (mImpactList
[i
].
isAlive())
mImpactList
[i
].
render();
}
}
public void spawnShot
(float x,
float y
)
{
for (int i =
0; i
< MAX_PLASMA_SHOTS
; i++
)
{
if (!mShotList
[i
].
isAlive())
{
mShotList
[i
].
spawn(x, y
);
//mShotSound.play();
return;
}
}
}
public void spawnImpact
(float x,
float y
)
{
for (int i =
0; i
< MAX_IMPACTS
; i++
)
{
if (!mImpactList
[i
].
isAlive())
{
//Gdx.app.log("Bla", "bla " + x + ", " + y);
mImpactList
[i
].
spawn(x, y
);
//mShotSound.play();
return;
}
}
}
// Getters/Setters==================================================================================
}