for context: im making a tower defense game using awt and im trying to make a system where you drag the tower to the field, and while what i have works, i have to do it for each tower, is there a way i can add the tower subclass to the method to speed it up, or even better, do it for each subclass of tower and fully automate the process?
in other words make them a part of the tower class, then call it for each tower.
ill give code examples after this message because of character limit
#Help with subclasses and super classes
49 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @hollow pumice! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
damn
ill rewrite it lol
/**
* Method for placing black holes on the screen
*/
public void placeBlackHoles() {
/* I need to make it so you can't place towers on path or off the screen */
// variable to hold mouse location
Point mouseLocation = new Point(gamePanel.mouseX, gamePanel.mouseY);
// moves the tower object as mouse moves
if (gamePanel.mouseX > 650 && gamePanel.mouseX < 750 &&
gamePanel.mouseY > 250 && gamePanel.mouseY < 350 &&
gamePanel.mouseIsPressed && scoreCounter >= 100) { // if mouse is pressed on tower icon, create a new
// object
placingBlackHole = true;
newBlackHole = new BlackHole(mouseLocation);
} else if (gamePanel.mouseX > 0 && gamePanel.mouseX < 600 &&
gamePanel.mouseY > 0 && gamePanel.mouseY < 600 &&
gamePanel.mouseIsPressed && placingBlackHole
&& line.distanceToPath(gamePanel.mouseX, gamePanel.mouseY) > 60) { // if mouse is pressed on game
// screen, place tower on game screen
newBlackHole.setPosition(mouseLocation);
towers.add(new BlackHole(mouseLocation));
scoreCounter -= 100;
newBlackHole = null;
placingBlackHole = false;
}
// moves tower object with mouse movements
if (newBlackHole != null) {
newBlackHole.setPosition(mouseLocation);
}
}
/**
* Method for placing suns on the screen
*/
public void placeSuns() {
/* I need to make it so you can't place towers on path or off the screen */
// variable to hold mouse location
Point mouseLocation = new Point(gamePanel.mouseX, gamePanel.mouseY);
// moves the tower object as mouse moves
if (gamePanel.mouseX > 650 && gamePanel.mouseX < 750 &&
gamePanel.mouseY > 400 && gamePanel.mouseY < 500 &&
gamePanel.mouseIsPressed && scoreCounter >= 300) { // if mouse is pressed on tower icon, create a new
// object
placingSun = true;
newSun = new Sun(mouseLocation);
} else if (gamePanel.mouseX > 0 && gamePanel.mouseX < 600 &&
gamePanel.mouseY > 0 && gamePanel.mouseY < 600 &&
gamePanel.mouseIsPressed && placingSun
&& line.distanceToPath(gamePanel.mouseX, gamePanel.mouseY) > 60) { // if mouse is pressed on game
// screen, place tower on game screen
newSun.setPosition(mouseLocation);
towers.add(new Sun(mouseLocation));
scoreCounter -= 300;
newSun = null;
placingSun = false;
}
// moves tower object with mouse movements
if (newSun != null) {
newSun.setPosition(mouseLocation);
}
}
public void draw(Graphics g){
// draw box around blackhole icon
g.setColor(new Color(224, 224, 224));
g.fillRect(650, 250, 100, 100);
// draw tower in menu area
BlackHole blackhole = new BlackHole(new Point(700, 300));
blackhole.draw(g);
// draw box around sun icon
g.setColor(new Color(224, 224, 224));
g.fillRect(650, 400, 100, 100);
// draw tower in menu area
Sun sun = new Sun(new Point(700, 450));
sun.draw(g);
// draws blackhole object with mouse movements
if (newBlackHole != null)
newBlackHole.draw(g);
// draws sun object with mouse movements
if (newSun != null)
newSun.draw(g);
}```
above me are the parts of Game.java that use sun
not all, but the parts im wanting to merge at least
here is the Tower Superclass
abstract public class Tower {
/* instance variables */
protected Point position; // holds position of tower
protected Image tower; // holds tower image
protected int anchorX; // shifts X coordinate
protected int anchorY; // shifts Y coordinate
protected double timeSinceLastFire;// time since last effect was fired
protected int id; // id of the tower
public void draw(Graphics g) {
// Draws tower object to location specified by user
g.drawImage(tower, position.x + anchorX, position.y + anchorY, null);
// Draws dot on Enemy's (x, y) coordinates
// g.setColor(Color.WHITE);
// g.fillOval(position.getX(), position.getY(), 5, 5);
}
/**
*
* @param c
*/
public void setPosition(Point c) {
position = c;
}
abstract void interact(Game game, double deltaTime);
abstract void drawMaker(Graphics g);
}
here is subclass BlackHole tower, for reference/example
public class BlackHole extends Tower {
/**
* Constructor
*/
public BlackHole(Point pos) {
ImageLoader loader = ImageLoader.getLoader();
this.tower = loader.getImage("resources/blackhole.png");
this.position = pos;
this.anchorX = -40;
this.anchorY = -40;
}
/**
*
*/
public void interact(Game game, double deltaTime) { // tracks time that effect has existed
timeSinceLastFire += deltaTime;
// if time less than 1.5 seconds, don't interact
if (timeSinceLastFire < 1)
return;
List<Enemy> enemies = game.enemies; // new list of enemies
// Gives position of an enemy in enemy list
for (Enemy e : enemies) {
// holds position of enemy
Point enemyPos = e.getPosition().getPos();
// Compute distance of enemy to tower
double dx, dy, dist; // change in x, y, and total distance
// calculates change in x and y position
dx = enemyPos.x - position.x; // x position of enemy - tower
dy = enemyPos.y - position.y; // y position of enemy - tower
// use Pythagorean theorem to calculate distance
dist = Math.sqrt((dx * dx) + (dy * dy));
// holds position of effect
Point pos = new Point(position.x, position.y);
// if enemy is in range, fire salt
if (dist < 80) {
StarDust stardust = new StarDust(pos, enemyPos);
game.effects.add(stardust);
timeSinceLastFire = 0;
return;
}
}
}
}
Sounds like that's not a correct way to do it in the first place
But from what I see, the only difference in the two methods is the constructor to call (the constructor to Blackhole or the constructor to Sun)
You could just have the method take a parameter Function<Point, Tower> that, given a mouse position, builds a Tower that is at that position
That way you'll trust that this function builds a Tower of the wanted class
Then placeBlackHoles() would just call placeTowers(Blackhole::new) and placeSuns() would call placeTowers(Sun::new)
As for automating it all, you'd need that whatever calls placeBlackHoles() or placeSuns(), would instead know what is it supposed to place, and the corresponding Function<Point, Tower> for that. You'd typically make a list of associations of buttons and functions, and when the user clicks a button, placing is called with the associated function
Can you give an example please?
placeTowers(Blackhole::new) and placeTowers(Sun::new)
No the part i pinged
private void placeTowers(Function<Point, Tower> towerBuilder) {
// everything like the methods you have
Tower newTower = towerBuilder.apply(mousePosition);
// everything again the same
}
Thanks, ill try this and share how it goes
ok im sorry ive tried for like 20 minutes can you fully write placeTowers for me? ive never used Function before.
at least tell me when // everything like the methods you have ends
@cinder meteor
maybe not fully write but at least give me some assistance with writing it
You're supposed to tell at what point you're not successful doing it yourself
It's just a copy/paste with the line change I made
But I must go to bed anyway
but where does Tower newTower.... go
its in between code
not at the start or end
It goes where Sun newSun went of course
when you are back online
what can i do about if (gamePanel.mouseX > 650 && gamePanel.mouseX < 750 &&
gamePanel.mouseY > 400 && gamePanel.mouseY < 500 &&
gamePanel.mouseIsPressed && scoreCounter >= 300)
because thats the coordinates of the button
also can you elaborate what the Function<Point, Tower> towerBuild does? i want to understand my code, and i tried google
thanks
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
As the name suggests, it bakes a strawberry crumble.
It builds a tower curiously enough. It takes a Point as parameter, and calls the constructor for a tower that takes that Point
Here we're running into the classical consideration of, when you have a class ChessPiece, does it represent an instance of a piece on the board, with where it is and its color, or does it represent how that piece work? Those are different notions, and it's the same with your tower types
You may need to make the difference between instances of towers in play, with where they are and what kind of towers it is, distinguished to what are the kinds of towers
The kind of tower could be in charge of describing how the tower operates and where to find its button
While a tower instance would only contain a position and a type of tower
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.