I'm currently doing some minor tests for a RTS-style game, and I've successfully coded the visual means for selecting player-controlled units (though this doesn't actually select them yet, it's just drawing the box correctly where the user clicks).
I've kind of fudged in a way for it to not select anything outside of the screen by limiting the size of the box to the screen's edges, but preferably I want to stop the cursor from leaving the screen at all, as then I can eventually have mouse-based map scrolling like many other RTS games have.
I have been creating a game in Processing and have been having a little trouble working out movement controls for the player character.
To try and get my head around it I've created a second sketch to test some ideas, and have managed to cobble together some simple collision detection between boxes (My game makes heavy use of bounding boxes to perform collision detection).
In its current state I'm able to move the player box up to the environment box, and it detects the collision when there is one, but I'm a little confused about how to go about allowing the player to slide against the box. In its current state, if you move diagonally against the bounding box, it will stop for the duration of the collision being detected. While I can see why it does this, I'm struggling to think of a way to solve the issue...
I've included my code below, thanks in advance for any assistance! :)
Collision
Box player1, environment;
final static int NORTH = 1;
final static int EAST = 2;
final static int SOUTH = 4;
final static int WEST = 8;
int result;
void setup()
{
player1 = new Box(20, 20, 50, 50);
environment = new Box(200, 200, 50, 50);
size(300,300);
}
void draw()
{
background(0);
player1.move();
player1.drawBox();
environment.drawBox();
}
int isColliding(Box box1, Box box2)
{
if (!((box1.posX + box1.sizeX) >= box2.posX)) return 0;
if (!(box1.posX <= (box2.posX + box2.sizeX))) return 0;
if (!((box1.posY - box1.sizeY) <= box2.posY)) return 0;
if (!(box1.posY >= (box2.posY - box2.sizeY))) return 0;
return 1;
}
void keyPressed(){
switch(key) {
case('w'):case('W'):result |=NORTH;break;
case('d'):case('D'):result |=EAST;break;
case('s'):case('S'):result |=SOUTH;break;
case('a'):case('A'):result |=WEST;break;
}
}
void keyReleased(){
switch(key) {
case('w'):case('W'):result ^=NORTH;break;
case('d'):case('D'):result ^=EAST;break;
case('s'):case('S'):result ^=SOUTH;break;
case('a'):case('A'):result ^=WEST;break;
}
}
Box
class Box
{
int posX, posY, sizeX, sizeY;
Box(int x1, int y1, int x2, int y2)
{
posX = x1;
posY = y1;
sizeX = x2;
sizeY = y2;
}
void drawBox()
{
if (isColliding(player1, environment) == 1)
{
fill(255, 0, 0);
}
else
{
fill(255);
}
rect(posX, posY, sizeX, sizeY);
}
void move()
{
switch(result)
{
case NORTH:
posY--;
if (isColliding(player1, environment) == 1) posY++;
break;
case EAST:
posX++;
if (isColliding(player1, environment) == 1) posX--;
break;
case SOUTH:
posY++;
if (isColliding(player1, environment) == 1) posY--;
break;
case WEST:
posX--;
if (isColliding(player1, environment) == 1) posX++;
EDIT: Made the title and final question a little clearer.
In the example, an ArrayList of Ball objects is created. Then in the draw, it takes the values and puts each one into another Ball object called 'ball', and performs move() and display() methods.
I'm trying to make a simple top down shooter game, and was considering using an ArrayList to hold Enemy objects, which have an enemyMovement() method. If I were to extract them from the ArrayList in the same manor as the Ball example...:
(Not actual part of my code just yet, just renamed variables to show what my plan is)
Enemy enemy = (Enemy) enemyList.get(i)
enemy.enemyMovement(); // x = x+1; y = y+1; for example
enemy.drawEnemy();
if (enemy.health == 0)
{
enemy.remove(i);
}
...are changes to the Enemy objects, done by enemy.enemyMovement(), automatically updated in the original object in the ArrayList or must I do that myself with an extra command?