package units;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import physics.PhysicsUtility;
import physics.Vector2D;
import serverMessages.ClientEventOut;
import serverMessages.NewConnectionEvent;
import spells.FireBall;
import Client.ClientGameState;
import Global_Package.Entity;
import Global_Package.Enums;
import Server.GameState;
import Server.UdpBroadCast;
import clientMessages.NewUnit;
public class BasicTestUnit extends Unit{
public final static int type=Enums.basicTestUnit;
private final static double moveForce=500;
private final static double maxMoveSpeed=3;
private final static double mass=40;
protected final static double maxHP=100;
protected final static double maxMana=100;
private final static BufferedImage image=Enums.rainbowSmug;
public BasicTestUnit(int ID,int OwnerId, Point2D initialLoc) {
super(ID,OwnerId, type, maxHP, maxHP, maxMana, maxMana, moveForce, maxMoveSpeed, mass, initialLoc);
}
protected void MoveUnit(GameState g){ //move to some where else....
if (moveTo != null && PhysicsUtility.distanceBetween(loc, moveTo) < 2) {
v.set(0,0);
}else{
try {
apply(new Vector2D(loc, moveTo).resize(moveForce));
} catch (Exception e) {
// Unit already at location, do nothing
return;
}
if (v.length()>maxMoveSpeed){
try {
v=v.resize(maxMoveSpeed);
} catch (Exception e) {
System.out.println("I have just proved that "+maxMoveSpeed+" < 0");
}
}
}
}
public void tick(GameState g){
MoveUnit(g);
super.tick(g);
if(!((float)loc.getX()==(float)oldLoc.getX()&&(float)loc.getY()==(float)oldLoc.getY()))
networkServerMessageOut(g.udp,loc.getX(),oldLoc.getY());
}
public void render(Graphics2D g, ClientGameState game) {
BufferedImage b=Enums.rainbowSmug;
int x=(int) loc.getX();
int y=(int) loc.getY();
g.translate(x, y);
g.rotate(getTheta());
g.translate(-x, -y);
g.drawImage(b,(int) loc.getX()-b.getWidth(),(int) loc.getY()-b.getHeight(),b.getWidth(),b.getHeight(),null);
g.translate(-x, -y);
g.rotate(-getTheta());
g.translate(x,y);
}
public void networkServerMessageOut(UdpBroadCast udp) {}
public void networkServerMessageOut(UdpBroadCast udp,double x, double y) {
udp.moveUnit(ID, x, y);
}
public void networkServerMessageIn(ClientEventOut in, GameState g) {
NewConnectionEvent i=(NewConnectionEvent)in;
g.getUnits().put(i.ID, new BasicTestUnit(i.ID,i.sourceId,new Point2D.Double(Math.random()*1350,Math.random()*1350)));
System.out.print("player Created\n");
g.udp.newUnit(i.ID, i.sourceId, i.type, g.getUnits().get(i.ID).getLoc().getX(),g.getUnits().get(i.ID).getLoc().getY());
}
public boolean checkType(int in) {
if(in==type)
return true;
else
return false;
}
public BufferedImage getImage() {
return image;
}
/**
* Called when something collides with you
*/
public void onCollide(Entity u, GameState game) {
// TODO Auto-generated method stub
}
public void remove(GameState g) {
g.getUnits().remove(ID);
g.udp.removeUnit(ID);
}
public void networkClientMessageOut(ClientGameState tcp){}
public void networkClientMessageOut(ClientGameState g,double x, double y){
try {
g.Sockets.pushPlayerMotion(g.Player.ID, (int)x, (int)y);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void mouseDown(MouseEvent e, ClientGameState game) {}
public void respondToKeyEvent(KeyEvent e) {}
public void keyDown(KeyEvent e, ClientGameState game) {}
public void newUnit(NewUnit a, ClientGameState g) {
g.getUnits().put(a.ID,new BasicTestUnit(a.ID,a.OwnerID,new Point2D.Double(a.x, a.y)));
g.Player=g.getUnits().get(a.ID);
}
@Override
public void alterUnit(int prop, int val) {
// TODO Auto-generated method stub
}
@Override
protected void mouseDown(Point2D e, ClientGameState game) {
networkClientMessageOut(game,e.getX(),e.getY());
}
}