package com.gebauz.pingK.game;
import java.util.Vector;
import android.util.Log;
import com.gebauz.framework.util.Sprite2D;
import com.gebauz.framework.util.Vector2;
import com.gebauz.pingK.R;
public class Background
{
public class Monkey
{
private float x
;
private float y
;
private float mRotation
;
private float mSize
;
private float mIntensity
;
private float mMoveX
;
private float mMoveY
;
private Sprite2D mSprite =
null;
public Monkey
(float posX,
float posY,
float size,
float intensity, Sprite2D sprite
)
{
x = posX
;
y = posY
;
mRotation = mGameLogic.
getRandomFloat(0.0f, 360.0f
);
mSize = size
;
mIntensity = intensity
;
mMoveX = mGameLogic.
getRandomFloat(-GameConsts.
MONKEY_MOVE_SPEED, GameConsts.
MONKEY_MOVE_SPEED);
mMoveY = mGameLogic.
getRandomFloat(-GameConsts.
MONKEY_MOVE_SPEED, GameConsts.
MONKEY_MOVE_SPEED);
mSprite = sprite
;
}
public void update
(float deltaTime
)
{
x += mMoveX
* deltaTime
;
y += mMoveY
* deltaTime
;
mRotation += mMonkeySpeed
* deltaTime
;
float maxX = GameConsts.
VIRTUAL_SCREEN_WIDTH + 50.0f
;
float maxY = GameConsts.
VIRTUAL_SCREEN_HEIGHT + 50.0f
;
if (x
< -50.0f
) { x = maxX
; }
if (y
< -50.0f
) { y = maxY
; }
if (x
> maxX
) { x = -50.0f
; }
if (y
> maxY
) { y = -50.0f
; }
}
public void render
()
{
Sprite2D theSprite = mSprite
;
if (mGameLogic.
getPowerUpEffect().
isFlowerActive())
{
theSprite = mFlower
;
theSprite.
setColor(GameConsts.
MONKEY_COLOR.
x, GameConsts.
MONKEY_COLOR.
y, GameConsts.
MONKEY_COLOR.
z, mIntensity
* GameConsts.
FLOWER_MAX_INTENSITY);
}
else
{
theSprite.
setColor(GameConsts.
MONKEY_COLOR.
x, GameConsts.
MONKEY_COLOR.
y, GameConsts.
MONKEY_COLOR.
z, mIntensity
* GameConsts.
MONKEY_MAX_INTENSITY);
}
theSprite.
x = x
;
theSprite.
y = y
;
theSprite.
angle = mRotation
;
theSprite.
w = mSize
;
theSprite.
h = mSize
;
theSprite.
pivotX = mSize / 2.0f
;
theSprite.
pivotY = mSize / 2.0f
;
theSprite.
render();
}
}
public static final int SPRITE_MONKEY =
0;
public static final int SPRITE_BANANA =
1;
private GameLogic mGameLogic =
null;
private Sprite2D mSprites
[] =
new Sprite2D
[2];
private Vector<Monkey
> mMonkeys =
new Vector<Monkey
>();
private float mMonkeySpeed = 0.0f
;
private Sprite2D mFlower =
null;
public Background
(GameLogic gameLogic
)
{
mGameLogic = gameLogic
;
mSprites
[SPRITE_MONKEY
] =
new Sprite2D
();
mSprites
[SPRITE_BANANA
] =
new Sprite2D
();
mSprites
[SPRITE_MONKEY
].
init(R.
drawable.
monkey);
mSprites
[SPRITE_BANANA
].
init(R.
drawable.
banana);
mFlower =
new Sprite2D
();
mFlower.
init(R.
drawable.
flower);
createMonkeys
();
}
public void createMonkeys
()
{
Vector2 interval =
new Vector2
((GameConsts.
VIRTUAL_SCREEN_WIDTH + 50.0f
) /
(float)GameConsts.
MONKEY_COUNT_X,
(GameConsts.
VIRTUAL_SCREEN_HEIGHT + 50.0f
) /
(float)GameConsts.
MONKEY_COUNT_Y);
for (int i =
0; i
< 2; i++
)
{
for (int x =
0; x
< GameConsts.
MONKEY_COUNT_X; x++
)
{
for (int y =
0; y
< GameConsts.
MONKEY_COUNT_Y; y++
)
{
if ( (((x
% 2) ==
(y
% 2)) && (i ==
0)) ||
(((x
% 2) !=
(y
% 2)) && (i ==
1)) )
{
float posX = -50.0f + interval.
x * (float)x + mGameLogic.
getRandomFloat(0.0f, interval.
x * GameConsts.
MONKEY_POSITION_SPREAD_FACTOR);
float posY = -50.0f + interval.
y * (float)y + mGameLogic.
getRandomFloat(0.0f, interval.
y * GameConsts.
MONKEY_POSITION_SPREAD_FACTOR);
Monkey monkey =
new Monkey
(posX, posY, mGameLogic.
getRandomFloat(GameConsts.
MONKEY_MAX_SIZE, GameConsts.
MONKEY_MAX_SIZE), mGameLogic.
getRandomFloat(0.7f, 1.0f
), mSprites
[i
]);
mMonkeys.
add(monkey
);
}
}
}
}
}
public void update
(float deltaTime
)
{
for (int i =
0; i
< mMonkeys.
size(); i++
)
{
mMonkeys.
get(i
).
update(deltaTime
);
}
}
public void render
()
{
for (int i =
0; i
< mMonkeys.
size(); i++
)
{
mMonkeys.
get(i
).
render();
}
}
public void setMonkeyRotationSpeed
(float angularSpeed
)
{
//Log.v(GameConsts.LOG_TAG, "monkeyspeed=" + angularSpeed);
mMonkeySpeed = angularSpeed
* GameConsts.
MONKEY_SPEED_FACTOR;
}
}