Alright, so i have this program that all it does so far is a path finding AI that just follows you based on your coordinates. Now, what i want to do is be able to have MULTIPLE AI’s on the screen. How would i go about doing that? I heard that i have to use an ArrayList, then what? I just shove them in there and have them drawn? If someone could provide a detailed discription of what to do that would be awesome. :c
My code for my Player and main game:
package src;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
public class Game extends JFrame implements Runnable{
// Global Variables
static Rectangle player = new Rectangle(30, 50, 15, 15);
static Rectangle object = new Rectangle(275, 175, 15, 15);
private Image dbImage;
private Graphics dbg;
static AI AI = new AI(object, player);
Color Player = new Color(255,202,102);
int xDirection, yDirection;
// Setting up the Game Window
public Game(){
setSize(700,400);
setResizable(false);
setLocationRelativeTo(null);
setTitle("ZOMBIE BLOCK");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
addKeyListener(new AL());
}
// Double-Buffer Graphics
@Override
public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
// Drawing the Player and AI
private void paintComponent (Graphics g) {
g.setColor(Player);
g.fillRect(player.x, player.y, player.width, player.height);
AI.draw(g);
repaint();
}
// Collision Detection
public void detectEdges(){
if(player.x <= 0)
setXDirection(1);
if(player.x >= 700-player.width)
setXDirection(-1);
if(player.y <= 25)
setYDirection(1);
if(player.y > 400-player.height)
setYDirection(-1);
}
// Letting the player move
public void move(){
player.x += xDirection;
player.y += yDirection;
}
public void setYDirection(int ydir){
yDirection = ydir;
}
public void setXDirection(int xdir){
xDirection = xdir;
}
// Key Inputs
public class AL extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_A){
setXDirection(-1);
}
if(keyCode == e.VK_D){
setXDirection(1);
}
if(keyCode == e.VK_W){
setYDirection(-1);
}
if(keyCode == e.VK_S){
setYDirection(1);
}
}
@Override
public void keyReleased(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_A){
setXDirection(0);
}
if(keyCode == e.VK_D){
setXDirection(0);
}
if(keyCode == e.VK_W){
setYDirection(0);
}
if(keyCode == e.VK_S){
setYDirection(0);
}
}
}
// Starting the threads
public static void main(String[] args) {
Game Game = new Game();
Thread t = new Thread(Game);
t.start();
Thread t1 = new Thread(AI);
t1.start();
}
// Letting the threads run.
@Override
public void run(){
try{
while(true){
move();
detectEdges();
Thread.sleep(5);
}
}catch(Exception e){
System.err.println(e.getMessage());
}
}
}
And this is the code for my AI or my Zombie:
package src;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
public class AI implements Runnable{
Rectangle AI, target;
boolean resting;
boolean shouldSetRandDir = true;
static int xDirection, yDirection;
Color Zombie = new Color(0,149,0);
public AI (Rectangle r, Rectangle t) {
AI = r;
target = t;
}
public void draw(Graphics g){
g.setColor(Zombie);
if (AI != null) {
// Draw Rectangle
g.fillRect(AI.x, AI.y, AI.width, AI.height);
}
}
public void findPathToTarget(){
if (AI.x < target.x) {
setxDir(1);
}
if (AI.x > target.x) {
setxDir(-1);
}
if (AI.y < target.y) {
setyDir(1);
}
if (AI.y > target.y) {
setyDir(-1);
}
}
public void detectEdges(){
if(AI.x <= 0)
setxDir(1);
if(AI.x >= 700-AI.width)
setxDir(-1);
if(AI.y <= 25)
setyDir(1);
if(AI.y > 400-AI.height)
setyDir(-1);
}
public void setxDir(int xdir){
xDirection = xdir;
}
public void setyDir(int ydir){
yDirection = ydir;
}
public void move(){
AI.x += xDirection;
AI.y += yDirection;
}
@Override
public void run() {
try {
while(true){
if (!resting) {
long start = System.currentTimeMillis();
long end = start + 1*1000;
while(System.currentTimeMillis() < end ) {
findPathToTarget();
move();
detectEdges();
// Speed in which it moves
Thread.sleep(15);
}
resting = true;
} else {
Thread.sleep(50);
shouldSetRandDir = true;
resting = false;
}
}
}catch(Exception e) {
System.out.println("ERROR");
}
}
}