I am making a two-player top down dungeon game. The movement style I settled on is the level moving around the player, giving the illusion the player is always in the middle of the screen. The way it works is by taking an image file and if a pixel has a certain color, it’s coordinates are saved and then in it’s place a TDboxes object is placed. The arrayList of boxes then moves around the player instead of the player moving. My problem is that when I control Player 1 and Player 2 move and vice-versa. Here is my code, it looks a bit weird because it is part of a larger project so there are some functions in one tab that are normally in multiple tabs. Image: https://ibb.co/Syzpt9N
//Topdown
int gamestate = 0, TDboxcounter = 0, TDaddcounter = 0;
boolean tdplayer1Left, tdplayer1Right, tdplayer2Left, tdplayer2Right, tdplayer1Up, tdplayer1Down, tdplayer2Up, tdplayer2Down;
boolean tdgame, loadSecondArray = true;
ArrayList<TDboxes> TDboxholder;
ArrayList<TDboxes> TDboxholder2;
int[] TDboxcoords;
PImage arena1;
color col;
void setup() {
size(1920, 1080);
TDboxholder = new ArrayList<TDboxes>();
TDboxcoords = new int[8000];
tdplayer1 = new TDplayer(width/4);
tdplayer2 = new TDplayer(width/4);
blockLoader(); //reads pixel color and saves coordinates of boxes to arrays
}
void blockLoader() {
arena1 = loadImage("topdownLVL.png");
for (int i = 0; i < arena1.width + 1; i++) {
for (int m = 0; m < arena1.height + 1; m++) {
col = arena1.get(i, m);
if (col == -65515) {
TDboxcoords[TDboxcounter] = i * 30 + 14;
TDboxcoords[TDboxcounter + 1] = m * 30 + 15;
TDboxcounter += 2;
}
}
}
}
void draw() {
showTD();
}
void keyPressed() {
if (tdgame) {
tdplayer1.setDrive(keyCode, true);
tdplayer2.setDrive(keyCode, true);
}
}
void keyReleased() {
if (tdgame) {
tdplayer1.setDrive(keyCode, false);
tdplayer2.setDrive(keyCode, false);
}
}
///-----------GAME------------///
void showTD() {
background(150);
switch(gamestate) {
case 0:
startscreenTD();
break;
//----------
case 1:
gamestateTD();
break;
//----------
case 2:
endstateTD();
break;
}
}
void startscreenTD() {
tdgame = true;
textSize(40);
text("Press T to Play", width/2, height/2 - 100);
text("Press E for Endscreen", width/2, height/2);
if (keyPressed && key == 'T' || key == 't') {
gamestate = 1;
}
if (keyPressed && key == 'E' || key == 'e') {
gamestate = 2;
}
}
void gamestateTD() {
background(155);
noStroke();
tdplayer1.movement();
tdplayer1.display();
tdplayer2.movement();
tdplayer2.display();
tdplayer1.edgeCollision();
tdplayer2.edgeCollision();
for (int i = 0; i < TDboxholder.size(); i++) {
TDboxes gg = TDboxholder.get(i);
if (gg.location.x < width/2) {
gg.show();
}
gg.movementArray1();
gg.collision();
}
if (!loadSecondArray) { //both arrays still moving
for (int i = 0; i < TDboxholder2.size(); i++) {
TDboxes ff = TDboxholder2.get(i);
pushMatrix();
translate(width/2 + 10, 0);
if (ff.location.x > 0) {
ff.show();
}
ff.movementArray2();
ff.collision();
tdplayer2.movement();
tdplayer2.display();
popMatrix();
}
}
if (TDboxholder.size() < TDboxcounter/2) {
TDboxholder.add(new TDboxes(TDboxcoords[TDaddcounter], TDboxcoords[TDaddcounter + 1], 30, 30));
TDaddcounter += 2;
fill(255, 0, 0);
text("Loading Arena...", width/2, height/4);
} else if (loadSecondArray) {
TDboxholder2 = new ArrayList<TDboxes>(TDboxholder);
loadSecondArray = false;
}
rectMode(CORNER);
fill(0);
rect(width/2 - 15, 0, 30, height);
}
void endstateTD() {
text("endstate", width/2, height/2);
}
/////----CLASSES----///////-----------------------------------------------------------------------
TDplayer tdplayer1;
TDplayer tdplayer2;
class TDplayer {
PVector location;
PVector velocity;
PVector speed;
boolean move;
TDplayer(int _x) {
location = new PVector(_x, height/2);
velocity = new PVector();
speed = new PVector(4, 4);
}
void edgeCollision() {
if (location.x > width || location.x < 0 || location.y > height || location.y < 0) {
location.x = width/2;
location.y = height/2;
}
}
void movement() {
// velocity.add(acceleration);
location.add(velocity);
}
boolean setDrive(final int k, final boolean b) { //checks if W has been pressed
switch(k) {
case + 'A':
return tdplayer1Left = b;
case + 'D':
return tdplayer1Right = b;
case + 'W':
return tdplayer1Up = b;
case + 'S':
return tdplayer1Down = b;
case LEFT:
return tdplayer2Left = b;
case RIGHT:
return tdplayer2Right = b;
case UP:
return tdplayer2Up = b;
case DOWN:
return tdplayer2Down = b;
default:
return b;
}
}
void display() {
fill(255, 0, 0);
rectMode(CENTER);
rect(location.x, location.y, 40, 40);
}
}
class TDboxes {
PVector location;
int sizeY;
int sizeX;
PVector velocity;
TDboxes(int _x, int _y, int _sizeX, int _sizeY) {
location = new PVector(_x, _y);
velocity = new PVector();
sizeX = _sizeX;
sizeY = _sizeY;
}
void show() {
fill(0, 255, 255);
rect(location.x, location.y, sizeX, sizeY);
}
void movementArray1() {
location.x -= (tdplayer1.speed.x * 2) *(int(tdplayer1Right) - int(tdplayer1Left));
location.y += (tdplayer1.speed.x * 2) *(int(tdplayer1Up) - int(tdplayer1Down));
}
void movementArray2() {
location.x -= (tdplayer2.speed.x * 2) *(int(tdplayer2Right) - int(tdplayer2Left));
location.y += (tdplayer2.speed.x * 2) *(int(tdplayer2Up) - int(tdplayer2Down));
}
void collision() {
if (tdplayer1.location.x > location.x && tdplayer1.location.x < location.x + 30) { //when hitting box with left side
if ((tdplayer1.location.y > location.y && tdplayer1.location.y < location.y + 30) || (tdplayer1.location.y + 40 > location.y && tdplayer1.location.y + 40 < location.y + 30)) {
tdplayer1.velocity.x = 0;
tdplayer1.location.x = location.x + 42;
}
}
if (tdplayer1.location.x + 40 > location.x && tdplayer1.location.x + 40 < location.x + 30) { //when hitting box with right side
if ((tdplayer1.location.y > location.y && tdplayer1.location.y < location.y + 30) || (tdplayer1.location.y + 40 > location.y && tdplayer1.location.y + 40 < location.y + 30)) {
tdplayer1.velocity.x = 0;
tdplayer1.location.x = location.x - 42;
}
}
if (tdplayer1.location.y > location.y && tdplayer1.location.y < location.y + 38) { //when hitting box from below
if ((tdplayer1.location.x + 40 > location.x && tdplayer1.location.x + 40 < location.x + 30) || (tdplayer1.location.x > location.x && tdplayer1.location.x < location.x + 30)) {
tdplayer1.velocity.y = 0;
tdplayer1.velocity.x = 0;
tdplayer1.location.y = location.y + 50;
}
}
if (tdplayer1.location.y + 40 < location.y && tdplayer1.location.y + 40 > location.y - 8) { //when hitting box from top
if ((tdplayer1.location.x + 40 > location.x && tdplayer1.location.x + 40 < location.x + 30) || (tdplayer1.location.x > location.x && tdplayer1.location.x < location.x + 30)) {
tdplayer1.velocity.y = 0;
tdplayer1.velocity.x = 0;
tdplayer1.location.y = location.y - 49;
}
}
//---------------------PLAYER 2--------------------
if (tdplayer2.location.x > location.x && tdplayer2.location.x < location.x + 30) { //when hitting box with left side
if ((tdplayer2.location.y > location.y && tdplayer2.location.y < location.y + 30) || (tdplayer2.location.y + 40 > location.y && tdplayer2.location.y + 40 < location.y + 30)) {
tdplayer2.velocity.x = 0;
tdplayer2.location.x = location.x + 42;
}
}
if (tdplayer2.location.x + 40 > location.x && tdplayer2.location.x + 40 < location.x + 30) { //when hitting box with right side
if ((tdplayer2.location.y > location.y && tdplayer2.location.y < location.y + 30) || (tdplayer2.location.y + 40 > location.y && tdplayer2.location.y + 40 < location.y + 30)) {
tdplayer2.velocity.x = 0;
tdplayer2.location.x = location.x - 42;
}
}
if (tdplayer2.location.y > location.y && tdplayer2.location.y < location.y + 38) { //when hitting box from below
if ((tdplayer2.location.x + 40 > location.x && tdplayer2.location.x + 40 < location.x + 30) || (tdplayer2.location.x > location.x && tdplayer2.location.x < location.x + 30)) {
tdplayer2.velocity.y = 0;
tdplayer2.velocity.x = 0;
tdplayer2.location.y = location.y + 50;
}
}
if (tdplayer2.location.y + 40 < location.y && tdplayer2.location.y + 40 > location.y - 8) { //when hitting box from top
if ((tdplayer2.location.x + 40 > location.x && tdplayer2.location.x + 40 < location.x + 30) || (tdplayer2.location.x > location.x && tdplayer2.location.x < location.x + 30)) {
tdplayer2.velocity.y = 0;
tdplayer2.velocity.x = 0;
tdplayer2.location.y = location.y - 49;
}
}
}
}```