How can I make an abstract class that will implement their methods and create 2 or more objects that need to be added to the list later? Do not paint with words how to do it, in theory I know, in practice I can not implement it! To whom it is not difficult, chew on a dumb student every detail, for how much I have not tried, only mistakes come out.
For example, here is the first class:
import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; public class BasicEnemy extends GameObject { private Handler handler; public BasicEnemy(float x, float y, ID id, Handler handler) { super(x, y, id); velX = 5; velY = 5; this.handler = handler; } public Rectangle getBounds(){ return new Rectangle((int)x,(int) y, 16, 16); } public void tick() { x += velX; y += velY; if(y <= 0 || y >= Game.HEIGHT - 48) velY *= -1; if(x <= 0 || x >= Game.WIDTH - 20) velX *= -1; handler.addObject(new Trail(x, y, ID.Trail, Color.red, 16, 16, 0.02f, handler)); } public void render(Graphics g) { g.setColor(Color.red); g.fillRect((int)x, (int)y, 16, 16); } } And the second:
import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; public class SmartEnemy extends GameObject { private Handler handler; private GameObject player; public SmartEnemy(int x, int y, ID id, Handler handler) { super(x, y, id); this.handler = handler; for (int i = 0; i < handler.object.size(); i++){ if(handler.object.get(i).getId() == ID.Player) player = handler.object.get(i); } } public Rectangle getBounds(){ return new Rectangle((int)x, (int)y, 16, 16); } public void tick() { x += velX; y += velY; float diffX = x - player.getX() - 8; float diffY = y - player.getY() - 8; float distance = (float) Math.sqrt((x-player.getX())*(x-player.getX()) + (y - player.getY())*(y-player.getY())); velX = (float) ((-1.0/distance) * diffX); velY = (float) ((-1.0/distance) * diffY); if(y <= 0 || y >= Game.HEIGHT - 48) velY *= -1; if(x <= 0 || x >= Game.WIDTH - 20) velX *= -1; handler.addObject(new Trail(x, y, ID.Trail, Color.green, 16, 16, 0.02f, handler)); } public void render(Graphics g) { g.setColor(Color.green); g.fillRect((int)x, (int)y, 16, 16); } } Their methods are the same, we need an abstract class! Previously, they were created in this way:
handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 50), r.nextInt(Game.HEIGHT - 50), ID.BasicEnemy, 5, 5, handler)); The handler class itself, which lists:
package com.wavegame.main; import java.awt.Graphics; import java.util.LinkedList; public class Handler { LinkedList<GameObject> object = new LinkedList<GameObject>(); public void tick(){ for (int i = 0; i < object.size(); i++){ GameObject tempObject = object.get(i); tempObject.tick(); } } public void render(Graphics g){ for (int i = 0; i < object.size(); i++){ GameObject tempObject = object.get(i); tempObject.render(g); } } public void clearEnemys(){ for (int i = 0; i < object.size(); i++){ GameObject tempObject = object.get(i); object.clear(); if (Game.gameState != Game.STATE.End) addObject(new Player((int)tempObject.getX(), (int)tempObject.getY(), ID.Player, this)); } } public void deleteEnemy(ID id){ for (int i = 0; i < object.size(); i++){ GameObject tempObject = object.get(i); if(tempObject.getId() == id){ removeObject(tempObject); } } } public boolean objectAmountFrom2To4(ID id){ int amount = 0; for (int i = 0; i < object.size(); i++){ GameObject tempObject = object.get(i); if (tempObject.getId() == id){ amount++; } } if (amount > 2 && amount < 4){ return true; } return false; } public boolean checkNotEnough(ID id){ int amount = 0; for (int i = 0; i < object.size(); i++){ GameObject tempObject = object.get(i); if (tempObject.getId() == id){ amount++; } } if (amount < 2){ return true; } return false; } public void addObject(GameObject object){ this.object.add(object); } public void removeObject(GameObject object){ this.object.remove(object); } } And the GameObject class that opponents inherit:
package com.wavegame.main; import java.awt.Graphics; import java.awt.Rectangle; public abstract class GameObject { protected float x, y; protected ID id; protected float velX, velY; public GameObject(float x, float y, ID id){ this.x = x; this.y = y; this.id = id; } public abstract void tick(); public abstract void render(Graphics g); public abstract Rectangle getBounds(); public void setX(int x){ this.x = x; } public void setY(int y){ this.y = y; } public float getX(){ return x; } public float getY(){ return y; } public void setId(ID id){ this.id = id; } public ID getId(){ return id; } public void setVelX(int velX){ this.velX = velX; } public void setVelY(int velY){ this.velY = velY; } public float getVelX(){ return velX; } public float getVelY(){ return velY; } } ___________________________________________________ UPDT:
In this case, the factory method works great, thanks again for it, but I need to use an abstract class to create objects and add to the gameplay itself, how can I do this? The abstract class has been created, but I don’t see the point of using it here, but I specifically need it, could you describe how to apply it? All data necessary for work is given above.
import java.awt.Graphics; import java.awt.Rectangle; public abstract class Enemy extends GameObject { //all other enemies extends "Enemies"!!!!!!!!! public Enemy(float x, float y, ID id) { super(x, y, id); } public abstract void tick(); public abstract Rectangle getBounds(); public abstract void render(Graphics g); }