Regarding targeting and such
in
Programming Questions
•
1 year ago
So I'm making a game where I want a turret object to be able to choose between a random set of targets. The targets are several arrays of similar objects (for example, 5 scout craft, 2 light fighters, 3 heavy cruisers). Once a target is chosen, the turret then fires its weapon at the target at its firing rate. Any suggestion for the best way to go about doing this?
- class Turret {
float xPos, yPos;
float accuracy, hull, dr, shield;
int firingRate, fire;
float xSize, ySize;
boolean isClicked, noTarget;
String name;
// turrets can display, but cannot move
void display() {
//rectMode(CENTER);
fill(255);
rect(xPos, yPos, xSize, ySize);
strokeWeight(3);
stroke(255);
noFill();
ellipse(xPos+xSize/2, yPos+ySize/2, xSize*2, ySize*2);
strokeWeight(1);
stroke(0);
}
void displayHUD() {
if (isClicked==true && hud.isOn==true) {
textFont(smallText);
fill(255, 100, 100);
text(hull, 80, 18);
text(accuracy*100+"%", 100, 36);
text(firingRate/30+"/second", 120, 54);
text(name, width/2, 36);
fill(0);
textFont(font);
}
}
// turrets can take damage
void takeDamage(int dmg) {
// Turrets are immobile targets. However, they can be upgraded with shields and DR, later.
// 1d20+accuracy rating vs. 1d20+defense rating
hull-=dmg;
}
// turrets can target enemy ships
void targetShip() {
if (numSS!=0) {
int temp=int(random(numSS));
for (int i=0; i<numSS; i++) {
if (i==temp && noTarget==true) {
ss[i].setTarget();
noTarget=false;
}
else {
break;
}
}
}
}
// if focus is on this cannon, its attributes will be displayed as text
void checkFocus() {
if (mouseX>=xPos && mouseX <= xSize+xPos) {
if (mouseY<=yPos+ySize && mouseY>=yPos) {
if (isClicked==false && hud.isObjectFocused==false) {
isClicked=true;
hud.isObjectFocused=true;
}
else if (isClicked==true) {
isClicked=false;
hud.isObjectFocused=false;
}
}
}
}
}
class LaserTurret extends Turret {
LaserTurret(float x, float y, float xS, float yS) {
xPos=x;
yPos=y;
xSize=xS;
ySize=yS;
hull=100;
accuracy=0.10;
firingRate=60;
name="Laser Turret";
}
void fireLaser() {
if (fire==firingRate) {
for (int i=0; i<numSS; i++) {
boolean temp=ss[i].isTarget();
if (temp==true) {
l = new Laser(xPos, yPos, ss[i].xPos, ss[i].yPos);
l.display();
ss[i].takeDamage(accuracy, l.dmg);
fire=0;
} else {
}
}
}
else if (fire<firingRate) {
fire++;
}
}
}
- class Ship {
float xPos, yPos; // Every ship has a position on the 2D world plane. In addition, each ship has a height and width.
int xSize, ySize;
float hull, defense, xVector, yVector, dmgbonus, accuracy; // Every ship has a hull value, defense value, and speed values. The dmgbonus value is added in some form to the ship's weapons
int firingRate;
float aluminum, carbon, steel; // The materials the ship is made of and drops upon death
boolean isClicked, isTarget;
String name;
// Every ship can be displayed on the screen
void display() {
fill(255);
ellipse(xPos, yPos, xSize, ySize);
}
// Every ship can move. The AI of the specific ships will determine the current x vector and y vector
void move() {
xPos+=xVector;
yPos+=yVector;
if (xPos>=width) {
xPos+=-xPos;
}
}
// Every ship can take damage
void takeDamage(float acc, int dmg) {
// The attacker's accuracy rating and the defender's defense rating are rolled against each other as:
// 1d20+accuracy rating vs. 1d20+defense rating
float isHit=acc+random(20)-defense-random(20);
if (isHit>0) {
hull-=dmg;
}
if (hull<=0) {
explosion(xPos, yPos, xSize, ySize);
xPos=10000;
yPos=10000;
}
}
// if focus is on this ship, its attributes will be displayed as text
void checkFocus() {
if (mouseX>=xPos-xSize && mouseX <= xSize+xPos) {
if (mouseY>=yPos-ySize && mouseY<=yPos+ySize) {
if (isClicked==false && hud.isObjectFocused==false) {
isClicked=true;
hud.isObjectFocused=true;
}
else if (isClicked==true) {
isClicked=false;
hud.isObjectFocused=false;
}
}
}
}
void setTarget() {
isTarget=true;
}
boolean isTarget() {
return isTarget;
}
// Actions that ships take will be defined in their own classes
}
// The most basic ship, and the first that you will encounter
class ScoutShip extends Ship {
ScoutShip(float x, float y, int h, int w) {
xPos=x;
yPos=y;
xSize=h;
ySize=w;
hull=15;
defense=0.05;
xVector=4;
yVector=0;
dmgbonus=0;
isClicked=false;
name="[Class 0] Scout";
}
void AI() {
// The most basic ship, the scoutship merely attempts to fly around randomly.
// Several for loops will be activated by a random number, each one accelerating the craft in a random direction
}
void fireWeapon() {
// The scoutship has no weapon
}
void displayHUD() {
if (isClicked==true && hud.isOn==true) {
textFont(smallText);
fill(255, 100, 100);
text(hull, 80, 18);
text(accuracy*100+"%", 100, 36);
text(firingRate/30+"/second", 120, 54);
text(name,width/2,36);
fill(0);
textFont(font);
}
}
}
- //display enemy craft
if (numSS!=0) {
for (int i=0; i<numSS; i++) {
ss[i].move();
ss[i].display();
}
} - ........
- // if any exist, display the laser cannons and if they are activated fire them!
if (numCannons!=0) {
for (int i=0; i<numCannons; i++) {
mL[i].display();
mL[i].targetShip();
mL[i].fireLaser();
}
}
1