displaying different kinds of guns
125
src/Main.java
|
@ -1,4 +1,6 @@
|
|||
import processing.core.PApplet;
|
||||
import processing.core.PImage;
|
||||
import processing.core.PVector;
|
||||
|
||||
public class Main extends PApplet {
|
||||
// Statically serving this class
|
||||
|
@ -13,6 +15,8 @@ public class Main extends PApplet {
|
|||
// CONSTANTS
|
||||
|
||||
// VARIABLES
|
||||
public float MAX_LINE_LENGTH;
|
||||
Gun test;
|
||||
|
||||
|
||||
// Override the built-in settings method. Start screen in fullscreen mode.
|
||||
|
@ -25,17 +29,128 @@ public class Main extends PApplet {
|
|||
// Override the built-in setup method. Initialize program parameters.
|
||||
@Override
|
||||
public void setup() {
|
||||
textSize(height/16f);
|
||||
fill(255);
|
||||
textAlign(CENTER, CENTER);
|
||||
MAX_LINE_LENGTH = sqrt(pow(width, 2) + pow(height, 2));
|
||||
|
||||
strokeWeight(3);
|
||||
imageMode(CENTER);
|
||||
textAlign(RIGHT, TOP);
|
||||
textSize(height/24f);
|
||||
|
||||
test = new Pistol(3, 100, height/2f);
|
||||
}
|
||||
|
||||
|
||||
// Override the built-in draw method. Periodically call contents.
|
||||
@Override
|
||||
public void draw() {
|
||||
background(0);
|
||||
background(255);
|
||||
|
||||
text("Hello, World!", width / 2f, height / 2f);
|
||||
noFill();
|
||||
stroke(255, 0, 0);
|
||||
test.setHeading(new PVector(mouseX, mouseY));
|
||||
test.Show();
|
||||
|
||||
fill(0);
|
||||
noStroke();
|
||||
text(round(frameRate), width, 0);
|
||||
}
|
||||
|
||||
private class Gun {
|
||||
/*
|
||||
image: Image of the gun in question
|
||||
position: Position of the gun on the screen
|
||||
laser: Height of laser measured from the top of the image downwards
|
||||
rotation: Rotation of the Gun around its center
|
||||
*/
|
||||
public PImage image;
|
||||
public PVector position;
|
||||
public boolean mirrored;
|
||||
public float laser, rotation, baseRotation;
|
||||
|
||||
public Gun(String filePath, int gunWidth, int gunHeight, float gunX, float gunY, float laserOffset, float gunRotation, boolean mirrorGun) {
|
||||
image = loadImage(filePath);
|
||||
image.resize(gunWidth, gunHeight);
|
||||
|
||||
position = new PVector(gunX, gunY);
|
||||
baseRotation = gunRotation;
|
||||
mirrored = mirrorGun;
|
||||
laser = laserOffset;
|
||||
}
|
||||
|
||||
public void Show() {
|
||||
float halfWidth = image.width/2f;
|
||||
|
||||
// Display Laser
|
||||
pushMatrix();
|
||||
translate(position.x, position.y);
|
||||
rotate(baseRotation + rotation);
|
||||
|
||||
translate(halfWidth, laser - image.height/2f);
|
||||
line(0, 0, MAX_LINE_LENGTH, 0);
|
||||
popMatrix();
|
||||
|
||||
// Display Gun over top
|
||||
pushMatrix();
|
||||
translate(position.x, position.y);
|
||||
rotate(baseRotation + rotation);
|
||||
|
||||
// Mirror Gun about the x-axis
|
||||
if (mirrored)
|
||||
scale(-1, 1);
|
||||
image(image, 0, 0);
|
||||
popMatrix();
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
||||
public class Pistol extends Gun {
|
||||
public Pistol(int type, float posX, float posY) {
|
||||
super(
|
||||
"Sprites/Pistol%d.png".formatted(type),
|
||||
200, 0,
|
||||
posX, posY,
|
||||
switch (type) {
|
||||
default -> 15;
|
||||
case 2 -> 11;
|
||||
case 3 -> 10;
|
||||
},
|
||||
0, type!=1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public class AssaultRifle extends Gun {
|
||||
public AssaultRifle(int type, float posX, float posY) {
|
||||
super(
|
||||
"Sprites/AssaultRifle%d.png".formatted(type),
|
||||
200, 0,
|
||||
posX, posY,
|
||||
switch (type) {
|
||||
default -> 15;
|
||||
case 2 -> 23;
|
||||
case 3 -> 42;
|
||||
},
|
||||
0, type==2
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public class Rifle extends Gun {
|
||||
public Rifle(int type, float posX, float posY) {
|
||||
super(
|
||||
"Sprites/Rifle%d.png".formatted(type),
|
||||
200, 0,
|
||||
posX, posY,
|
||||
switch (type) {
|
||||
default -> 35;
|
||||
case 2 -> 15;
|
||||
},
|
||||
0, type==2
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
BIN
src/Sprites/AssaultRifle1.png
Normal file
After Width: | Height: | Size: 117 KiB |
BIN
src/Sprites/AssaultRifle2.png
Normal file
After Width: | Height: | Size: 31 KiB |
BIN
src/Sprites/AssaultRifle3.png
Normal file
After Width: | Height: | Size: 69 KiB |
BIN
src/Sprites/Pistol1.png
Normal file
After Width: | Height: | Size: 25 KiB |
BIN
src/Sprites/Pistol2.png
Normal file
After Width: | Height: | Size: 60 KiB |
BIN
src/Sprites/Pistol3.png
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
src/Sprites/Rifle1.png
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
src/Sprites/Rifle2.png
Normal file
After Width: | Height: | Size: 7.5 KiB |