#set of interface instances

1 messages · Page 1 of 1 (latest)

random canopy
#

so I wanna make a hashSet<Components> Components is the interface to store objects from classes that implement component. specifically hashSet cause I don't want 2 objects of the same class to exist together. I imagine, I would have to overwrite hashcode for every single one of those classes to return hash(class) or something. any way around this?

shy oceanBOT
#

<@&987246399047479336> please have a look, thanks.

shy oceanBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

dire jacinth
#

@random canopy Yeah, just have the classes hash their contents

#

you misunderstand hashsets

#

let me ask

#

Are components "values" - their equals is based on the contents of their fields

#

Or are they "objects with identity" - their equals should be the same as ==

random canopy
#

hmmm I'm a bit confused by the question

dire jacinth
#

show me a Component

#

an example one

random canopy
#

ooh boy

#
package Utils.Components;

import Utils.Component;
import Utils.GameObject;
import main.GamePanel;

import java.awt.image.BufferedImage;

public class SpriteRenderer implements Component {
    private BufferedImage currentSprite;
    private GamePanel gamePanel;
    private GameObject parentObject;

    public SpriteRenderer(BufferedImage sprite, GamePanel gamePanel, GameObject parentObject) {
        this.currentSprite = sprite;
        this.gamePanel = gamePanel;
        this.parentObject = parentObject;
    }

    @Override
    public void start() {

    }

    @Override
    public void update() {
        this.gamePanel.drawSprite(currentSprite, parentObject.getTransform().getPosition());
    }

    public BufferedImage getCurrentSprite() {
        return currentSprite;
    }

    public void setCurrentSprite(BufferedImage currentSprite) {
        this.currentSprite = currentSprite;
    }
}
dire jacinth
#

okay so you want a hashset of components

#

why don't you want a List

random canopy
#

right, I understand that hashset is meant more for values, I guess I just wanted to see if there was a collection specifically for unique classes

#

but yeah I'm using list now

dire jacinth
#

well a HashSet would work as is

#

but it would not be ordered

#

i would suggest LinkedHashSet if you want the order of components to stay the same

random canopy
dire jacinth
#

that would still have an order like a list, but would remove the chance of accidentally adding the same components twice

dire jacinth
#

If you want that I would say use

#
LinkedHashMap<Class<? extends Component>, Component>
#

so you map classes to the one component

random canopy
#

oh shit

#

oh that's

#

sexy

#

damn

#

yeah that looks perfect gahdamn

dire jacinth
#

or for faster lookup

#

...nah nvm you got it

random canopy
#

yup, that's awesome man thanks

dire jacinth
#

i was gonna say IdentityHashMap, but idk

#

that loses order

#

btw I am hard assuming that

  1. You are making a game
  2. These are "ECS" components
random canopy
#

definitely making a game, what's ECS lol

#

it's possible I'm doing that rn without knowing it but yeah I'm going for a similar structure to unity gameobjects and stuff

random canopy
dire jacinth
#

the 2nd talk is what introduced me to the concept

#

"Entity Component System"

#

also I'd love to see your game code

#

if you are willing

#

I mostly see shitty website projects on here

random canopy
#

ooo thanks, I'll def check those out since it's kinda my dream to actually get into game dev some day lol

random canopy
# dire jacinth if you are willing

ofcourse, but fair warning, haven't set up like a solid update loop yet, I have frame update set up and I'm just using that for now lol

#

but also I don't really have much of a game going on rn, just setting up that component system

dire jacinth
#

(would be pretty funny if you watch those two videos then just use ashley)

random canopy
#

what, like just rip the code from there or something lol

#

oh it's a whole ass framework nice

#

yeah ngl I've been intentionally trying to stay away from frameworks for this specific thing cause I wanna see how stuff works on the inside I guess

#

also wait here's some game code, again, not much game going on yet, so most of what I have to be proud of is the component stuff lol

shy oceanBOT
random canopy
#

oh yeah, since I'm trying to remake pokemon firered, I also set up this grid class

#
package Utils.Components;

import Utils.Vector2;
import main.Game;

public class Grid {
    private int cellWidth;
    private int cellHeight;

    public Grid(int cellWidth, int cellHeight) {
        this.cellWidth = cellWidth * Game.PIXELS_PER_UNIT;
        this.cellHeight = cellHeight * Game.PIXELS_PER_UNIT;
    }

    public Vector2 gridToWorldPosition(Vector2 gridPosition) {
        return new Vector2(this.cellWidth * gridPosition.getX(), this.cellHeight * gridPosition.getY());
    }

    public Vector2 worldToGridPosition(Vector2 worldPosition) {
        return new Vector2(worldPosition.getX() / this.cellWidth, worldPosition.getY() / this.cellHeight);
    }
}
shy oceanBOT
dire jacinth
random canopy
#

i

#

you what

dire jacinth
#

you can compile the rom yourself

#

change the code and make a modded version

random canopy
#

well ok I mean it doesn't change stuff for me all that much, I just wanna make it for the sake of making it lol

dire jacinth
#

oh yeah sure

#

but if when you are done with this if you want to do another pokemon thing

random canopy
dire jacinth
#

for that there is the source code

dire jacinth
#

but also other battle systems

random canopy
#

man ok that's great, I can actually learn something now

dire jacinth
random canopy
#

thanks em, I saved those, gonna be a huge help

random canopy
#

alright well thanks again em! cya