package ru.vagrant_ai.questionmarkgame.obj.add;
import java.util.Random;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;
import ru.vagrant_ai.questionmarkgame.main.GameplayState;
import ru.vagrant_ai.questionmarkgame.util.Util;
public class Money {
private Image[] util_img = new Image[2];
private float x, y;
private float impulse_x, impulse_y;
private boolean direction;
private byte quantity;
public byte state = 1;
private byte frame = 0;
private byte util_iter = 9;
private float scale = 0f;
private float alpha = 1f;
public Money(int x, int y, byte quantity)
{
this.x = x;
this.y = y;
this.quantity = quantity;
impulse_y = new Random().nextFloat()*2f+4;
impulse_x = new Random().nextFloat()*1;
direction = new Random().nextBoolean();
util_img[0] = Util.loadImage("particle/money0");
util_img[1] = Util.loadImage("particle/money1");
}
public void render()
{
if (state == 3)
{
float offset_x = util_img[0].getWidth()*scale/2;
float offset_y = util_img[0].getHeight()*scale/2;
Image img = util_img[0].getScaledCopy(1+scale);
img.setAlpha(alpha);
img.draw((int)x-offset_x, (int)y-offset_y);
}
else if (state != 0)
{
switch(frame)
{
case 0:
case 1:
case 2:
util_img[0].draw((int)x, (int)y);
break;
case 3:
util_img[1].draw((int)x, (int)y);
break;
}
}
}
public void update()
{
if (state != 0 && state != 3)
{
util_iter--;
if (util_iter < 1)
{
util_iter = 9;
frame++;
if (frame > 3)
frame = 0;
}
}
if (state != 0 && state != 3 && GameplayState.player.hitbox.intersects(new Rectangle(x, y, 10, 16)))
onCollect();
switch(state)
{
case 0:
case 2:
break;
case 1:
x += impulse_x*(direction?1:-1);
if (x > 790)
x = 790;
else if (x < 5)
x = 5;
y -= impulse_y-1;
if (impulse_y > -2)
{
impulse_y -= 0.25f;
if (impulse_y < -2)
impulse_y = -2;
}
if (y > GameplayState.ground_level+25)
{
y = GameplayState.ground_level+25;
state++;
}
break;
case 3:
scale += 0.2f;
alpha -= 0.009f;
if (scale > 2.3f)
state = 0;
break;
}
}
private void onCollect()
{
GameplayState.gui.money_offset = 17;
GameplayState.player.addMoney(quantity);
state = 3;
}
}