shooting works

This commit is contained in:
nora 2020-12-11 20:29:03 +01:00
parent 0d5397dbe3
commit e2483b30ff
8 changed files with 45 additions and 23 deletions

View file

@ -3,6 +3,7 @@ package core;
import objects.DebugPos;
import objects.ships.BattleShip;
import objects.GameObject;
import objects.ships.Turret;
import objects.world.Grid;
import javax.swing.*;
@ -38,9 +39,9 @@ public class Master extends JPanel {
BattleShip battleShip = new BattleShip(Color.DARK_GRAY);
BattleShip bs = new BattleShip(70, 10, 5, 80, Color.GREEN);
/*for (int i = 0; i < 10; i++) {
for (int i = 0; i < 10; i++) {
bs.addTurret(new Turret(bs, 25, 10 * i + 1, 50, i % 5));
}*/
}
objects.add(bs);
objects.add(battleShip);
}

View file

@ -1,5 +1,6 @@
package core;
/**
* A 2-dimensional Vector that can be used to store position or velocity
*/
@ -45,7 +46,7 @@ public class Vector2D {
}
/**
* Mulitply this vector with a simple scalar, modifies this object
* Multiply this vector with a simple scalar, modifies this object
*
* @param a The scalar
* @return this after the multiplication
@ -111,6 +112,9 @@ public class Vector2D {
/**
* Rotate a point around another point
*
* This method can now be trusted
*
* @param center The center of the rotation
* @param value The point to be rotated, absolut to the center
* @param rotation The rotation angle in radians
@ -121,12 +125,18 @@ public class Vector2D {
Vector2D dif = Vector2D.subtract(value, center);
double rotatedX = Math.cos(rotation * rotationDirection) * dif.x;
double rotatedY = Math.sin(rotation * rotationDirection) * dif.x;
rotation = rotationDirection * rotation;
double rotatedX = dif.x * Math.cos(rotation) - dif.y * Math.sin(rotation);
double rotatedY = dif.x * Math.sin(rotation) + dif.y * Math.cos(rotation);
return new Vector2D(rotatedX + center.x, rotatedY + center.y);
}
/**
* Copy this object
* @return A copy of this object
*/
public Vector2D copy(){
return new Vector2D(x, y);
}

View file

@ -17,7 +17,7 @@ public class DebugPos extends GameObject {
@Override
public void draw(Graphics2D g2d, int w, Master master) {
g2d.setPaint(Color.green);
g2d.drawOval((int) (position.x + size.x / 2), (int) (position.y + size.y / 2), (int) size.x, (int) size.y);
g2d.drawOval((int) (position.x - size.x / 2), (int) (position.y - size.y / 2), (int) size.x, (int) size.y);
}
@Override

View file

@ -17,9 +17,9 @@ public class BattleShip extends GameObject {
public BattleShip(Color mainColor) {
super(20, 20, 5, 40);
turrets = new ArrayList<>();
turrets.add(new Turret(this, 25, 25, 50, 1));
//turrets.add(new Turret(this, 25, 10, 50, 2));
//turrets.add(new Turret(this, 25, 70, 50, 2));
turrets.add(new Turret(this, 25, 25, 50, 3));
turrets.add(new Turret(this, 25, 10, 50, 2));
turrets.add(new Turret(this, 25, 70, 50, 2));
this.mainColor = mainColor;
}

View file

@ -92,9 +92,7 @@ public class Turret extends GameObject {
lastShot = System.currentTimeMillis();
Vector2D shellVel = new Vector2D(xCenterAbs - msLoc.x, yCenterAbs - msLoc.y).normalized().negative().multiply(SHELL_SPEED);
Vector2D pos = Vector2D.rotateAround(new Vector2D(xCenterAbs, yCenterAbs), new Vector2D(barrelX, frontPosY), rotation, Vector2D.CLOCKWISE);
master.debugPos(pos);
Vector2D pos = Vector2D.rotateAround(new Vector2D(xCenterAbs, yCenterAbs), new Vector2D(barrelX, frontPosY), rotation, Vector2D.COUNTERCLOCKWISE);
master.create(new Shell(pos, new Vector2D(10, 10), shellVel));
}