split classes into files

This commit is contained in:
Baipyrus 2022-12-21 14:46:40 +01:00
parent 1f92868c51
commit c134d15b6f
5 changed files with 123 additions and 102 deletions

View File

@ -0,0 +1,20 @@
package Classes;
import processing.core.PApplet;
public class AssaultRifle extends Gun {
public AssaultRifle(PApplet sketch, int type, float posX, float posY) {
super(
sketch,
"Sprites/AssaultRifle%d.png".formatted(type),
200, 0,
posX, posY,
switch (type) {
default -> 15;
case 2 -> 23;
case 3 -> 42;
},
0, type==2
);
}
}

61
src/Classes/Gun.java Normal file
View File

@ -0,0 +1,61 @@
package Classes;
import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PVector;
public 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
*/
private final float laser, baseRotation;
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) {
processing = sketch;
image = processing.loadImage(filePath);
image.resize(gunWidth, gunHeight);
position = new PVector(gunX, gunY);
baseRotation = gunRotation;
mirrored = mirrorGun;
laser = laserOffset;
}
public void Show(float lineLength) {
float halfWidth = image.width/2f;
// Display Laser
processing.pushMatrix();
processing.translate(position.x, position.y);
processing.rotate(baseRotation + rotation);
processing.translate(halfWidth, laser - image.height/2f);
processing.line(0, 0, lineLength, 0);
processing.popMatrix();
// Display Gun over top
processing.pushMatrix();
processing.translate(position.x, position.y);
processing.rotate(baseRotation + rotation);
// Mirror Gun about the x-axis
if (mirrored)
processing.scale(-1, 1);
processing.image(image, 0, 0);
processing.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();
}
}

20
src/Classes/Pistol.java Normal file
View File

@ -0,0 +1,20 @@
package Classes;
import processing.core.PApplet;
public class Pistol extends Gun {
public Pistol(PApplet sketch, int type, float posX, float posY) {
super(
sketch,
"Sprites/Pistol%d.png".formatted(type),
200, 0,
posX, posY,
switch (type) {
default -> 15;
case 2 -> 11;
case 3 -> 10;
},
0, type!=1
);
}
}

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

@ -0,0 +1,19 @@
package Classes;
import processing.core.PApplet;
public class Rifle extends Gun {
public Rifle(PApplet sketch, int type, float posX, float posY) {
super(
sketch,
"Sprites/Rifle%d.png".formatted(type),
200, 0,
posX, posY,
switch (type) {
default -> 35;
case 2 -> 15;
},
0, type==2
);
}
}

View File

@ -1,5 +1,5 @@
import Classes.*;
import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PVector;
public class Main extends PApplet {
@ -36,7 +36,7 @@ public class Main extends PApplet {
textAlign(RIGHT, TOP);
textSize(height/24f);
test = new Pistol(3, 100, height/2f);
test = new Pistol(this, 3, 100, height/2f);
}
@ -48,109 +48,10 @@ public class Main extends PApplet {
noFill();
stroke(255, 0, 0);
test.setHeading(new PVector(mouseX, mouseY));
test.Show();
test.Show(MAX_LINE_LENGTH);
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
);
}
}
}