package com.gardeneaters.game_v0.playingfield;
import com.google.gwt.canvas.dom.client.CanvasGradient;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;
import com.google.gwt.dom.client.CanvasElement;
// A hexagon does not know location and number of other hexagons
public class Hexagon {
/*
6/ \1
5| |2
4\ /3
*/
private MushroomGroup mushroomGroup;
private int x,y,hexWidth,hexHeight;
private Context2d context;
private int pointingTo;
private int team;
//private int isActive;
private double currentPow, finalPow;// there will be gradual change of currentPow to reach finalPow
public boolean calculating = false;
CssColor textWriteColor;
protected Hexagon(Context2d _context, int _x, int _y, int _hexWidth, int _hexHeight){
this.context = _context;
mushroomGroup = new MushroomGroup(context,_x, _y, _hexWidth, _hexHeight,2);
setTeam(0,true);
//team = 0;
//mushroomGroup.setType(team);
currentPow = 0;
pointingTo = 0;
//changeColors();
textWriteColor = CssColor.make("rgba(0,0,0,1)");
resize(_x,_y,_hexWidth,_hexHeight);
}
public int getPointingTo(){
return pointingTo;
}
protected void resize(int _x, int _y, int _hexWidth, int _hexHeight){
this.x = _x;
this.y = _y;
this.hexWidth = _hexWidth;
this.hexHeight = _hexHeight;
drawHex();
mushroomGroup.resize(_x, _y, _hexWidth, _hexHeight);
}
protected void draw() {
//don't redraw hexagons again here..
//if(team!=0)
gradualPowerAdjustment();
if(team!=0)
{
drawHex();
mushroomGroup.draw(currentPow);
point();
}
//context.setFillStyle(textWriteColor);
//context.fillText(String.valueOf(currentPow).substring(0,3), x+hexWidth/2, y+hexHeight/2);
}
protected void gradualPowerAdjustment(){
//TODO make it according to percentage increase or according to difference b/w final n current pow
double diff = Math.abs(finalPow-currentPow);
if(diff<1.0)
diff=1;// multiplication will not result in a very small number
if(currentPow < finalPow)
currentPow += 0.01*diff;
else if(currentPow > finalPow)
currentPow -= 0.01*diff;
}
protected void point(int side) {
if(team!=0)
{
this.pointingTo = side;
point();
}
}
private void point() {
context.save();
// drawHex();
if(pointingTo!=0)
{
double angle = (Math.atan(0.5)+Math.PI/2)/2;
double rotateAngle = -angle;
for(int i =1; i<pointingTo ; i++)// side is not greater than 6
{
rotateAngle += angle;
}
// System.out.println(rotateAngle);
context.translate(x+hexWidth/2, y+hexHeight/2);
context.rotate(rotateAngle);
CanvasElement arrow = Draw.arrow(hexWidth/4, hexHeight/4);
context.drawImage(arrow, -arrow.getWidth()/2,-arrow.getHeight()/2);
}
context.restore();
//System.out.println(calculating);
}
private void drawHex(){
// int rndRedColor = 20;
// int rndGreenColor = 200;
// int rndBlueColor = 0;//Random.nextInt(255);
CanvasGradient canvasGradient = context.createLinearGradient(x, y, x+hexWidth, y+hexHeight);
// canvasGradient.addColorStop(0, "rgba(" + rndRedColor + ", " + rndGreenColor + "," + rndBlueColor + ", 1)");
// canvasGradient.addColorStop(0.6, "rgba(" + (rndRedColor+5) + ", " + (rndGreenColor+5) + "," + (rndBlueColor+5) + ", 1)");
canvasGradient.addColorStop(0, "rgba(20,200,0,1)");
canvasGradient.addColorStop(0.6, "rgba(30,210,0,1)");
canvasGradient.addColorStop(1, "rgba(20,100,0,1)");
context.setFillStyle(canvasGradient);
context.beginPath();
context.moveTo(x, y+hexHeight/4);
context.lineTo(x+hexWidth/2 , y);
context.lineTo(x+hexWidth, y+hexHeight/4);
context.lineTo(x+hexWidth, y+hexHeight*3/4);
context.lineTo(x+hexWidth/2, y+hexHeight);
context.lineTo(x, y+hexHeight*3/4);
context.lineTo(x, y+hexHeight/4);
context.closePath();
context.stroke();
context.fill();
}
public double getCurrentPow()
{
return currentPow;
}
public void setFinalPow(double _finalPow)
{
if(Double.compare(6.0, _finalPow)==1)
this.finalPow = _finalPow;
else
this.finalPow=6.0;
}
public double getFinalPower(){
return finalPow;
}
public void setTeam(int _team, boolean overRide){
if(overRide || _team==0 || this.currentPow>=0.3)
{
this.team = _team;
mushroomGroup.setType(team);
if(_team==0)
pointingTo=0;
}
//changeColors();
}
public int getTeam(){
return this.team;
}
// private void changeColors(){
// //mushroomGroup.changeType();
// if(team==0)
// {
// rndRedColor = 255;
// rndGreenColor = 255;
// rndBlueColor = 255;
// }
// else if(team==1)
// {
// rndRedColor = 0;
// rndGreenColor = 0;
// rndBlueColor = 255;
// }
// else if(team==2)
// {
// rndRedColor = 255;
// rndGreenColor = 0;
// rndBlueColor = 255;
// }
// else if(team==3)
// {
// rndRedColor = 255;
// rndGreenColor = 0;
// rndBlueColor = 0;
// }
// }
public void setPointingTo(int side){
this.pointingTo = side;
}
public void bringToLife(int _team){
setTeam(_team,true);
this.currentPow = 1;
this.finalPow = 1;
//changeColors();
}
}