Hello,
im trying to make bot for simple pong game. I have problem with reseting bots position after scoring point. Any suggestions?
//main func.
import game2dai.*;
import game2dai.entities.*;
import game2dai.entityshapes.*;
import game2dai.entityshapes.ps.*;
import game2dai.fsm.*;
import game2dai.graph.*;
import game2dai.maths.*;
import game2dai.steering.*;
import game2dai.utils.*;
import java.util.*;
World world;
StopWatch sw;
Vehicle dart;
LinkedList<Vector2D> route = new LinkedList<Vector2D>();
Plansza pl;
Prawy pr;
Lewy lw;
boolean[] keys;
int mode;
//var lewy
float xlw;
float ylw;
//var pilka
int r = 8;
PVector location;
PVector velocity;
//var prawy
int xpr = 770;
int ypr = 190;
boolean zmiana; // do sprawdzenia czy location.y sie zmienilo
float ploc; //poprzednia lokacja
void setup() {
size(800, 460);
world = new World(width, height);
sw = new StopWatch();
pl = new Plansza();
pr = new Prawy();
lw = new Lewy();
dart = new Vehicle(
new Vector2D(17, 225), // pozycja
40, // kolizja
new Vector2D(0, 0), // przyspieszenie
240, // max predkosc
new Vector2D(1, 0), // kierunek
1, // masa
100, // zwrotnosc
99999999 // max sila
);
PersonPic view = new PersonPic(this);
dart.renderer(view);
dart.AP().pathFactors(10, 1).pathOn();
world.add(dart);
route.add(new Vector2D(dart.pos()));
sw.reset();
keys = new boolean[4];
for (int i = 0; i < keys.length; i++) {
keys[i] = false;
}
location = new PVector(width/2, height/2);
velocity = new PVector(4, 0);
}
void draw() {
double elapsedTime = sw.getElapsedTime();
world.update(elapsedTime);
if (dart.AP().pathRouteLength() == 0) dart.velocity(0, 0);
background(0);
resetDart();
drawRoute();
ballRoute();
ploc = location.y; //zachowaj wartosc w poprzedniej klatce
pl.hit();
pr.gracz();
lw.gracz();
pl.tlo();
pl.pilka();
if (location.y == ploc) {
zmiana = false;
} else {
zmiana = true;
}
world.draw(elapsedTime);
}
void keyPressed() {
if (key == CODED && keyCode == UP)
keys[2] = true;
if (key == CODED && keyCode == DOWN)
keys[3] = true;
}
void keyReleased() {
if (key == CODED && keyCode == UP)
keys[2] = false;
if (key == CODED && keyCode == DOWN)
keys[3] = false;
}
void drawRoute() {
Vector2D p0, p1;
while (route.size () > dart.AP().pathRouteLength() + 1)
route.removeFirst();
}
void ballRoute() {
if (zmiana) {
Vector2D wp = new Vector2D(17, location.y+15);
dart.AP().pathAddToRoute(wp);
route.add(wp);
}
}
void resetDart() { //these should be posiotion reset func.
}
//LEFT PLAYER (bot)
class Lewy {
void gracz() {
Vector2D p0, p1;
p0 = route.get(0);
xlw = (float)p0.x - 7;
ylw = (float)p0.y - 35;
noStroke();
fill(255);
rect(xlw, ylw, 15, 80, 5);
}
}
// RIGHT PLAYER
class Prawy {
void gracz() {
noStroke();
fill(255);
rect(xpr, ypr, 15, 80, 5);
if (keys[2]) {
ypr = ypr - 3;
}
if (keys[3]) {
ypr = ypr + 3;
}
if (ypr < 0) ypr = 0;
if (ypr + 80 > height) ypr = height - 80;
if (location.x > width+r || location.x < 0-r) ypr = 190;
}
}
// GAMEPLAY FUNC
class Plansza {
int l, p;
void tlo() {
strokeWeight(0.5);
stroke(230);
line(width/2, 0, width/2, height);
strokeWeight(0.5);
stroke(230);
noFill();
ellipse(width/2, height/2, 120, 120);
textSize(32);
text(l, width/2 - 100, 100);
text(p, width/2 + 100, 100);
if (location.x > width + r) {
l = l+1;
velocity.x = 5;
}
if (location.x < 0 - r) {
p = p+1;
velocity.x = -5;
}
if (location.x > width + r || location.x < 0 - r) {
location.x = width/2;
location.y = height/2;
velocity.y = 0;
}
}
void pilka() {
fill(255);
ellipse(location.x, location.y, 2*r, 2*r);
location.add(velocity);
if (keyPressed && key == 'r') {
location.x = width/2;
location.y = height/2;
velocity.x = 5;
velocity.y = 0;
l = 0;
p = 0;
}
}
void hit() {
float testX1 = location.x;
float testY1 = location.y;
if (location.x + r < xpr) testX1 = xpr;
else if (location.x + r > xpr) testX1 = xpr;
if (location.y < ypr) testY1 = ypr;
else if (location.y > ypr + 80) testY1 = ypr + 80;
float distX1 = location.x-testX1;
float distY1 = location.y-testY1;
float distance1 = sqrt((distX1*distX1) + (distY1*distY1));
if (distance1 <= r) {
velocity.x = (velocity.x * -1) - 0.10;
}
if (distance1 <= r) {
if (location.y > 0 && location.y < ypr + r) {
velocity.y = -3;
} else if (location.y >= ypr + r && location.y < ypr + 30) {
velocity.y = -1;
} else if (location.y >= ypr + 30 && location.y < ypr + 50) {
velocity.y = 0;
} else if (location.y >= ypr + 50 && location.y < ypr + 70) {
velocity.y = 1;
} else if (location.y >= ypr + 70 && location.y < height) {
velocity.y = 3;
}
}
float testX2 = location.x;
float testY2 = location.y;
if (location.x < xlw) testX2 = xlw;
else if (location.x > xlw + 15) testX2 = xlw + 15;
if (location.y < ylw) testY2 = ylw;
else if (location.y > ylw + 80) testY2 = ylw + 80;
float distX2 = location.x-testX2;
float distY2 = location.y-testY2;
float distance2 = sqrt((distX2*distX2) + (distY2*distY2));
if (distance2 <= r) {
velocity.x = (velocity.x * -1) + 0.10;
}
if (distance2 <= r) {
if (location.y > 0 && location.y < ylw + r) {
velocity.y = -3;
} else if (location.y >= ylw + r && location.y < ylw + 30) {
velocity.y = -1;
} else if (location.y >= ylw + 30 && location.y < ylw + 50) {
velocity.y = 0;
} else if (location.y >= ylw + 50 && location.y < ylw + 70) {
velocity.y = 1;
} else if (location.y >= ylw + 70 && location.y < height) {
velocity.y = 3;
}
}
if (location.y - r < 0) velocity.y = velocity.y * -1;
else if (location.y + r > height) velocity.y = velocity.y * -1;
}
}