package com.gebauz.burutaru.game.entities;
import com.gebauz.bauzoid.graphics.sprite.AtlasSpriteInstance;
import com.gebauz.bauzoid.graphics.sprite.AtlasSprite;
import com.gebauz.bauzoid.graphics.sprite.Sprite;
import com.gebauz.bauzoid.math.MathUtil;
import com.gebauz.burutaru.GameConsts;
import com.gebauz.burutaru.game.GameLogic;
public class Ship
extends Entity
{
// Constants========================================================================================
public final int SHIP_WIDTH =
64;
public final int SHIP_HEIGHT =
64;
public final int NUM_UP_FRAMES =
5;
public final int NUM_DOWN_FRAMES =
5;
public final int NUM_SHIP_FRAMES =
1 + NUM_UP_FRAMES + NUM_DOWN_FRAMES
;
public final int SHIP_SPRITE_SIZE =
128;
public final int NUM_SHIP_FRAMES_X =
4;
public final int NUM_SHIP_FRAMES_Y =
4;
public final float UPDOWN_ANIM_SPEED = 4.0f
;
public final float UPDOWN_THRESHOLD = 20.0f
;
public final float MUZZLEFLASH_DURATION = 0.15f
;
public final float MUZZLEFLASH_SIZE = 40.0f
;
public final float EXHAUST_WIDTH =
28;
public final float EXHAUST_HEIGHT =
16;
// Embedded Types===================================================================================
public enum MoveDirection
{
NONE,
UP,
DOWN
}
// Fields===========================================================================================
private float mPositionX =
100;
private float mPositionY =
100;
private AtlasSprite mShipAtlas =
null;
private AtlasSpriteInstance mShipFrames
[] =
new AtlasSpriteInstance
[NUM_SHIP_FRAMES
];
private int mCurrentShipFrame =
0;
private MoveDirection mMoveDirection = MoveDirection.
NONE;
private Sprite mMuzzleFlash =
null;
private float mMuzzleFlashTimer = -1.0f
;
private Sprite mEngineExhaust =
null;
private float mEngineExhaustScale = 1.0f
;
private float mMoveDirectionCoeff = 0.0f
;
private float mMoveDirectionCoeffTarget = 0.0f
;
// Methods==========================================================================================
public Ship
(GameLogic gameLogic
)
{
super(gameLogic
);
mShipAtlas =
new AtlasSprite
(getGraphics
(),
"data/textures/shipframes.png",
"data/textures/ship.txt");
mMuzzleFlash =
new Sprite
(getGraphics
(),
"data/textures/plasma_muzzleflash.png");
mEngineExhaust =
new Sprite
(getGraphics
(),
"data/textures/engine_exhaust.png");
}
@
Override
public void initAsync
()
{
mShipAtlas.
initAsync();
mMuzzleFlash.
initAsync();
mEngineExhaust.
initAsync();
}
@
Override
public void init
()
{
mShipAtlas.
init();
mShipAtlas.
createSpriteInstances(mShipFrames
);
mMuzzleFlash.
init();
mEngineExhaust.
init();
}
@
Override
public void exit
()
{
for (int i =
0; i
< NUM_SHIP_FRAMES
; i++
)
{
mShipFrames
[i
] =
null;
}
if (mShipAtlas
!=
null)
{
mShipAtlas.
dispose();
mShipAtlas =
null;
}
if (mMuzzleFlash
!=
null)
{
mMuzzleFlash.
dispose();
mMuzzleFlash =
null;
}
if (mEngineExhaust
!=
null)
{
mEngineExhaust.
dispose();
mEngineExhaust =
null;
}
}
@
Override
public void update
(float deltaTime
)
{
if (mMoveDirection == MoveDirection.
UP)
{
// move towards -1
mMoveDirectionCoeff -= deltaTime
* UPDOWN_ANIM_SPEED
;
if (mMoveDirectionCoeff
< mMoveDirectionCoeffTarget
)
mMoveDirectionCoeff = mMoveDirectionCoeffTarget
;
}
else if (mMoveDirection == MoveDirection.
DOWN)
{
// move towards +1
mMoveDirectionCoeff += deltaTime
* UPDOWN_ANIM_SPEED
;
if (mMoveDirectionCoeff
> mMoveDirectionCoeffTarget
)
mMoveDirectionCoeff = mMoveDirectionCoeffTarget
;
}
else if (mMoveDirection == MoveDirection.
NONE)
{
// move towards 0
if (mMoveDirectionCoeff
> 0)
mMoveDirectionCoeff = MathUtil.
clamp(mMoveDirectionCoeff - deltaTime
* UPDOWN_ANIM_SPEED,
0,
1);
else if (mMoveDirectionCoeff
< 0)
mMoveDirectionCoeff = MathUtil.
clamp(mMoveDirectionCoeff + deltaTime
* UPDOWN_ANIM_SPEED, -
1,
0);
}
mMoveDirectionCoeff = MathUtil.
clamp(mMoveDirectionCoeff, -
1,
1);
if (mMoveDirectionCoeff
> 0)
{
mCurrentShipFrame =
(int)(1 +
(NUM_DOWN_FRAMES-
1) * (mMoveDirectionCoeff
));
}
else if (mMoveDirectionCoeff
< 0)
{
mCurrentShipFrame =
(int)(1 + NUM_DOWN_FRAMES +
(NUM_UP_FRAMES-
1) * (-mMoveDirectionCoeff
));
}
else
{
mCurrentShipFrame =
0;
}
//Gdx.app.log(GameConsts.LOG_TAG, "coeff = " + mMoveDirectionCoeff + ", frame = " + mCurrentShipFrame + ", dir = " + mMoveDirection);
mCurrentShipFrame = MathUtil.
clamp(mCurrentShipFrame,
0, NUM_SHIP_FRAMES-
1);
mEngineExhaustScale = 0.8f + MathUtil.
sin(getGame
().
getTime() * 4000.0f
) * 0.05f
;
if (mMuzzleFlashTimer
> 0.0f
)
mMuzzleFlashTimer -= deltaTime
;
}
@
Override
public void render
()
{
mEngineExhaust.
x = mPositionX - EXHAUST_WIDTH/
2;
mEngineExhaust.
y = mPositionY -
1;
mEngineExhaust.
w = EXHAUST_WIDTH
* mEngineExhaustScale
;
mEngineExhaust.
h = EXHAUST_HEIGHT
;
mEngineExhaust.
pivotX = mEngineExhaust.
w;
mEngineExhaust.
pivotY = mEngineExhaust.
h/
2;
mEngineExhaust.
render();
mShipFrames
[mCurrentShipFrame
].
x = mPositionX
;
mShipFrames
[mCurrentShipFrame
].
y = mPositionY
;
mShipFrames
[mCurrentShipFrame
].
w = SHIP_WIDTH
;
mShipFrames
[mCurrentShipFrame
].
h = SHIP_HEIGHT
;
mShipFrames
[mCurrentShipFrame
].
centerPivot();
mShipFrames
[mCurrentShipFrame
].
render();
if (mMuzzleFlashTimer
> 0.0f
)
{
mMuzzleFlash.
x = mPositionX + SHIP_WIDTH/
2;
mMuzzleFlash.
y = mPositionY
;
mMuzzleFlash.
w = MUZZLEFLASH_SIZE
;
mMuzzleFlash.
h = MUZZLEFLASH_SIZE
;
mMuzzleFlash.
pivotX = mMuzzleFlash.
w/
3 + 1.0f
;
mMuzzleFlash.
pivotY = mMuzzleFlash.
h/
2;
mMuzzleFlash.
alpha = mMuzzleFlashTimer/MUZZLEFLASH_DURATION
;
mMuzzleFlash.
render();
}
}
public void moveBy
(float x,
float y,
float deltaTime
)
{
mPositionX += x
*deltaTime
;
mPositionY += y
*deltaTime
;
// TODO: scale with amount of y
if (y
< -UPDOWN_THRESHOLD
)
{
mMoveDirection = MoveDirection.
UP;
//mMoveDirectionCoeffTarget = -1.0f;
mMoveDirectionCoeffTarget =
(y + UPDOWN_THRESHOLD
) * 0.01f
;
}
else if (y
> UPDOWN_THRESHOLD
)
{
mMoveDirection = MoveDirection.
DOWN;
mMoveDirectionCoeffTarget =
(y - UPDOWN_THRESHOLD
) * 0.01f
;
}
else
{
mMoveDirection = MoveDirection.
NONE;
mMoveDirectionCoeffTarget = 0.0f
;
}
mPositionX = MathUtil.
clamp(mPositionX, 0.0f, GameConsts.
VIRTUAL_SCREEN_WIDTH);
mPositionY = MathUtil.
clamp(mPositionY, 0.0f, GameConsts.
VIRTUAL_SCREEN_HEIGHT);
}
public void resetNoUpDownMovement
()
{
mMoveDirection = MoveDirection.
NONE;
mMoveDirectionCoeffTarget = 0.0f
;
}
public void shootPlasma
()
{
getGameLogic
().
getPlasmaShots().
spawnShot(mPositionX + SHIP_WIDTH/
2, mPositionY
);
mMuzzleFlashTimer = MUZZLEFLASH_DURATION
;
}
// Getters/Setters==================================================================================
}