shooting and targets

This commit is contained in:
Baipyrus 2022-12-21 15:40:12 +01:00
parent c134d15b6f
commit 45247c6674
8 changed files with 115 additions and 24 deletions

View File

@ -14,7 +14,8 @@ public class AssaultRifle extends Gun {
case 2 -> 23;
case 3 -> 42;
},
0, type==2
0, type==2,
10, 45f
);
}
}

33
src/Classes/Bullet.java Normal file
View File

@ -0,0 +1,33 @@
package Classes;
import processing.core.PApplet;
import processing.core.PVector;
public class Bullet {
private final PVector position, velocity, offset;
private final float diameter, heading;
private final PApplet processing;
public Bullet(PApplet sketch, float xPos, float xOffset, float yPos, float yOffset, float bulletWidth, float direction, float speed) {
processing = sketch;
position = new PVector(xPos, yPos);
offset = new PVector(xOffset, yOffset);
velocity = PVector.fromAngle(direction).setMag(speed);
diameter = bulletWidth;
heading = direction;
}
public void update() {
position.add(velocity);
}
public void show() {
processing.pushMatrix();
processing.translate(position.x, position.y);
processing.rotate(heading);
processing.translate(offset.x, offset.y);
processing.circle(0, 0, diameter);
processing.popMatrix();
}
}

View File

@ -1,9 +1,11 @@
package Classes;
import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PApplet;
import processing.core.PVector;
import java.util.ArrayList;
public class Gun {
/*
image: Image of the gun in question
@ -11,15 +13,18 @@ public class Gun {
laser: Height of laser measured from the top of the image downwards
rotation: Rotation of the Gun around its center
*/
private final float laser, baseRotation;
private final float laser, baseRotation, speed, diameter;
private final ArrayList<Bullet> bullets;
private final PApplet processing;
private final boolean mirrored;
private final PImage image;
public PVector position;
public float rotation;
public Gun(PApplet sketch, String filePath, int gunWidth, int gunHeight, float gunX, float gunY, float laserOffset, float gunRotation, boolean mirrorGun) {
public Gun(PApplet sketch, String filePath, int gunWidth, int gunHeight, float gunX, float gunY, float laserOffset, float gunRotation, boolean mirrorGun, float bulletDiameter, float shootingSpeed) {
processing = sketch;
bullets = new ArrayList<>();
diameter = bulletDiameter;
image = processing.loadImage(filePath);
image.resize(gunWidth, gunHeight);
@ -27,16 +32,25 @@ public class Gun {
position = new PVector(gunX, gunY);
baseRotation = gunRotation;
mirrored = mirrorGun;
speed = shootingSpeed;
laser = laserOffset;
}
public void Show(float lineLength) {
// Update Gun and children
public void update() {
for (Bullet b : bullets)
b.update();
}
public void show(float lineLength, int lineColor, int bulletColor) {
float halfWidth = image.width/2f;
// Display Laser
processing.noFill();
processing.stroke(lineColor);
processing.pushMatrix();
processing.translate(position.x, position.y);
processing.rotate(baseRotation + rotation);
processing.rotate(rotation);
processing.translate(halfWidth, laser - image.height/2f);
processing.line(0, 0, lineLength, 0);
@ -52,10 +66,21 @@ public class Gun {
processing.scale(-1, 1);
processing.image(image, 0, 0);
processing.popMatrix();
// Display bullets
processing.fill(bulletColor);
processing.stroke(bulletColor);
for (Bullet b : bullets)
b.show();
}
// Rotate Gun towards given target
public void setHeading(PVector target) {
rotation = PVector.sub(PVector.sub(target, new PVector(0, laser - image.height/2f)), position).heading();
}
// Shoot the Gun
public void shoot() {
bullets.add(new Bullet(processing, position.x, image.width/2f, position.y, laser - image.height/2f, diameter, rotation, speed));
}
}

View File

@ -7,14 +7,15 @@ public class Pistol extends Gun {
super(
sketch,
"Sprites/Pistol%d.png".formatted(type),
200, 0,
100, 0,
posX, posY,
switch (type) {
default -> 15;
case 2 -> 11;
case 3 -> 10;
default -> 10;
case 2 -> 7;
case 3 -> 5;
},
0, type!=1
0, type!=1,
7.5f, 30f
);
}
}

View File

@ -7,13 +7,16 @@ public class Rifle extends Gun {
super(
sketch,
"Sprites/Rifle%d.png".formatted(type),
200, 0,
275, 0,
posX, posY,
switch (type) {
default -> 35;
case 2 -> 15;
default -> 47;
case 2 -> 22;
case 3 -> 141;
},
0, type==2
(type == 3) ? sketch.PI / 4 : 0,
type == 2 || type == 3,
15, 60f
);
}
}

19
src/Classes/Target.java Normal file
View File

@ -0,0 +1,19 @@
package Classes;
import processing.core.PApplet;
import processing.core.PVector;
public class Target {
private final PApplet processing;
private final PVector position, dimensions;
public Target(PApplet sketch, float xPos, float yPos, float width, float height) {
processing = sketch;
position = new PVector(xPos, yPos);
dimensions = new PVector(width, height);
}
public void show() {
processing.rect(position.x, position.y, dimensions.x, dimensions.y);
}
}

View File

@ -16,7 +16,8 @@ public class Main extends PApplet {
// VARIABLES
public float MAX_LINE_LENGTH;
Gun test;
Target testTarget;
Gun testGun;
// Override the built-in settings method. Start screen in fullscreen mode.
@ -36,7 +37,8 @@ public class Main extends PApplet {
textAlign(RIGHT, TOP);
textSize(height/24f);
test = new Pistol(this, 3, 100, height/2f);
testTarget = new Target(this, width - 200, height/2f, 100, 100);
testGun = new Rifle(this, 3, 150, height/2f);
}
@ -44,14 +46,21 @@ public class Main extends PApplet {
@Override
public void draw() {
background(255);
noFill();
stroke(255, 0, 0);
test.setHeading(new PVector(mouseX, mouseY));
test.Show(MAX_LINE_LENGTH);
fill(0);
stroke(0);
testTarget.show();
testGun.setHeading(new PVector(mouseX, mouseY));
testGun.show(MAX_LINE_LENGTH, color(255, 0, 0), color(0));
testGun.update();
noStroke();
text(round(frameRate), width, 0);
text("FPS: %d".formatted(round(frameRate)), width, 0);
}
@Override
public void mousePressed() {
testGun.shoot();
}
}

BIN
src/Sprites/Rifle3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB