package main;
import gfx.SpriteSheet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class trigTester extends Canvas implements Runnable {
AffineTransformOp op;
AffineTransform tx;
AffineTransform tx1;
double rotationRequired;
double locationX;
double locationY;
public boolean stopMovingMiss;
public int once = 0;
public float angle = 0;
public float targAngle;
public float angleStep = 0.1f;
public int animInc;
public boolean running;
public float targetX;
public float targetY;
public float targXVel;
public float targYVel;
public float missileX;
public float missileY;
public float missSpeed;
public float a;
public float b;
public float c;
public float disc;
public float t1;
public float t2;
public float t;
public float aimX;
public float aimY;
public float yDiff;
public float xDiff;
public float yNorm;
public float xNorm;
public float hyp;
public float a1;
public float b1;
public float c1;
public float disc1;
public float towT1;
public float towT2;
public float towT;
public float aimX1;
public float aimY1;
public float yTowDiff;
public float xTowDiff;
public float yNorm1;
public float xNorm1;
public float hyp1;
public static final int WIDTH=200;//480;
public static final int HEIGHT = 200;//480;//WIDTH/ 12 * 9;
public static final int SCALE = 5;
public static final String name= "tester";
public JFrame frame;
public BufferedImage tower = null;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
private int[] towPixels;
public trigTester()
{
try
{
tower = ImageIO.read(SpriteSheet.class.getResource("/spriteSheet/testTower.png"));
}
catch(IOException e)
{
e.printStackTrace();
}
//towPixels = ((DataBufferInt)tower.getRaster().getDataBuffer()).getData();
towPixels = image.getRGB(0, 0, tower.getWidth(), tower.getHeight(), null, 0, tower.getWidth());
rotationRequired = Math.toRadians(45);
locationX = tower.getWidth() / 2;
locationY = tower.getHeight() / 2;
tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
tx1 = new AffineTransform();
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
stopMovingMiss = false;
running= false;
targetX = 150;
targetY = 5;
targXVel = -1;
targYVel = 0;
missileX = 70;
missileY = 60;
missSpeed = 2;
Dimension dimension= new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
setMinimumSize(dimension);
setMaximumSize(dimension);
setPreferredSize(dimension);
frame = new JFrame(name);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
start();
}
public void run()
{
long lastTime = System.nanoTime();
long nowTime;
long delta;
long incrementTime = 900000000;
long lastTimer = System.nanoTime();
while(running)
{
nowTime = System.nanoTime();
delta = nowTime = lastTime;
if (delta >= incrementTime)
{
tick();
lastTime = nowTime;
}
render();
}
}
public void start()
{
Thread start = new Thread(this);
running = true;
start.run();
}
public void tick()
{
if (animInc== 100)
{
targetX+=targXVel;
if (once == 0)
{
a = (targXVel* targXVel) + (targYVel * targYVel) - (missSpeed * missSpeed);
b = 2 *(targXVel * (targetX - missileX) + targYVel * (targetY - missileY));
c = (targetX - missileX) * (targetX - missileX) + (targetY - missileY)* (targetY - missileY);
//System.out.println("a:" + a + ", b: "+ b+ "c: " + c);
disc= b*b - 4 * a* c;
//System.out.println(disc);
t1 = (-b + (float)Math.sqrt(disc))/ (2*a);
t2 = (-b- (float)Math.sqrt(disc))/ (2*a);
if (t1 > 0 && t1 < t2)t = t1;
else if (t2 > 0 && t2 < t1) t = t2;
else if (t1 <0 && t2 >0) t = t2;
else if (t2 < 0 && t1 > 0)t = t1;
//System.out.println("t:" + t);
aimX = t * targXVel + targetX;
aimY = t * targYVel + targetY;
// System.out.println("t:" + t);
//System.out.println(aimX + ":" + aimY);
xDiff = aimX - missileX;
yDiff = aimY - missileY;
hyp = (float)Math.sqrt(xDiff*xDiff + yDiff* yDiff);
//System.out.println("hyp: "+ hyp);
xNorm = xDiff/ hyp;
yNorm = yDiff/hyp;
//System.out.println("yNorm:" + yNorm);
once++;
}
animInc =0;
if ((int)(targetX - missileX) !=0 && (int)(targetY - missileY)!=0)
{
missileX += missSpeed*xNorm;
missileY += missSpeed*yNorm;
// System.out.println("hello!");
//missileX = (float)targetX;
//missileY = (float)targetY;
}
a1 = (targXVel* targXVel) + (targYVel * targYVel) - (missSpeed * missSpeed);
b1 = 2 *(targXVel * (targetX - 70) + targYVel * (targetY - 60));
c1 = (targetX - 70) * (targetX - 70) + (targetY - 60)* (targetY - 60);
disc1= b1*b1 - 4 * a1* c1;
towT1 = (-b1 + (float)Math.sqrt(disc1))/ (2*a1);
towT2 = (-b1- (float)Math.sqrt(disc1))/ (2*a1);
if (towT1 > 0 && towT1 < towT2)towT = towT1;
else if (towT2 > 0 && towT2 < towT1) towT = towT2;
else if (towT1 <0 && towT2 >0) towT = towT2;
else if (towT2 < 0 && towT1 > 0)towT = towT1;
aimX1 = towT * targXVel + targetX;
aimY1 = towT * targYVel + targetY;
if (aimX1 >= WIDTH || aimY1 >= HEIGHT)
{
aimX1 = 0;
aimY1 = 0;
}
xTowDiff = aimX1 -70;
yTowDiff = aimY1 - 60;
hyp1 = (float)Math.sqrt(xTowDiff*xTowDiff + yTowDiff* yTowDiff);
//rotationRequired = Math.asin(yTowDiff/hyp1);
//System.out.println(rotationRequired);
rotationRequired = Math.asin(xTowDiff/hyp1);
//rotationRequired+=.1;
if (rotationRequired >= 3.14 *2) rotationRequired =0;
tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
//tx1 = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
//op = new AffineTransformOp(tx1, AffineTransformOp.TYPE_BILINEAR);
//tower = op.filter(tower, null);
//towPixels = tower.getRGB(0, 0, tower.getWidth(), tower.getHeight(), null, 0, tower.getWidth());
}
else
animInc++;
}
/*
xDiff = targetX- missileX;
yDiff = targetY- missileY;
if ((int)xDiff!=0 && (int)yDiff!=0)
{
hyp = (float)Math.sqrt(xDiff * xDiff + yDiff * yDiff);
xNorm = xDiff/hyp;
yNorm = yDiff/hyp;
missileX += 3*xNorm;
missileY += 3*yNorm;
}
targetX++;
animInc = 0;
}
else animInc++;
}
*/
public void clear()
{
for (int i = 0; i < WIDTH*HEIGHT; i++)
{
pixels[i]= 0;
}
}
public void render()
{
BufferStrategy bs = getBufferStrategy();
if (bs == null)
{
createBufferStrategy(3);
return;
}
clear();
Graphics g = bs.getDrawGraphics();
Color c = Color.blue;
int col = c.getRGB();
pixels[(int)missileX + (int)missileY * WIDTH] = col;
pixels[(int)targetX + (int)targetY * WIDTH] = col;
//image.setRGB(targetX, 50, col);
//System.out.println("missileX: " + missileX + "missileY: " + missileY);
//image.setRGB((int)missileX, (int)missileY, col);
//image.setRGB((int)targetX , (int)targetY, col);
image.setRGB(70, 60, col);
g.drawImage(image, 0 , 0 ,getWidth(), getHeight(), null);
//g.drawImage(tower, 100, 100, 30, 30,null);
g.drawRect(70* SCALE, 60* SCALE, 9, 9);
/*
for (int i = 0; i < 30; i++){
for (int j = 0; j < 30; j++){
System.out.println("i: "+ (i+ 70) + " j: " + (j+ 60));
pixels[j + i*WIDTH] = towPixels[j + i *tower.getWidth()];
}
}
*/
g.drawImage(op.filter(tower, null), 70*SCALE-15, 60*SCALE-15, null);
g.dispose();
bs.show();
}
public static void main(String args[])
{
trigTester test = new trigTester();
}
}