This post relates to this one:
https://coderanch.com/t/617207/java/java/Long-list-statements (I mostly figured out and took the suggestions there but now I have another issue that is a bit different)
I've been making a game for fun/practice where the player moves around a 2-d array 'board' and encounters different spaces on that board.
I have an interface:
Various creatures (and events) implement that interface:
etc.
I then fill my board with the creatures/events:
Each creature has a doAction method, so depending on what space the player is currently on (this is controlled by other classes/methods) something happens:
I have my 'player' object contained in the same Class (encounter class) as where 'whatHappens = spaces[row][column].doAction(row, column);' takes place.
To make the code cleaner I'd like to be able to doAction() and have it effect the player object in the encounter class. However, I'm not sure how to do this. Right now doAction in a particular creature class just returns that creature to the encounter class where combat or talking or whatever happens. This causes the encounter class to be really crammed full of code and is a list kind of like this:
Is there a way to do something to the player object from within one of the creature classes? There is no player object present in the creature classes so I can do something like encounter.player.giveRope(); from there. Since I return a creature object when I doAction() I thought I might be able to get information about that object using an accessor. So, for example, if doAction(int row, int column) is in a creature class and row = something and column = something I set some attribute of that creature to lets say '1' whereas by default the vaule is 0. Then, in the encounter class I could do something like (remember whatHappens is the creature type) int thing = whatHappens.getAttribute(). Then use thing to go on and do stuff using other methods without having such a laundry list of if/else if statements. But I couldn't seem to get this to work? I was only able to whatHappens.getClass() and break it up that way. It seemed only attributes of the Super Class would be returned, not attributes of the specific Sub Class .
I hope my question makes sense...I tried not to leave out too many details while still making is mildly succinct. Still new to
Java and learning my way around, trying to get these
polymorphism ideas down. I appreciate any help or let me know if more details are needed.
TLDR: I have Class A. Class B holds an object of Class A. I have Classes that implement an interface. There is an array of interface objects in Class B. Can I (within the classes that implement the interface) do things to Class A?