For hobby I’m making a game. The game has a monster chasing the human (Pacman-like). When the Pacman is stuck, can eat the human or does some move; an event should be raised. This is because my program became not-oop because all the objects had to know eachother what did the cohesion no good.
There is a control like object (called Game) which should respond to the event; human-moved, monster-moved, human-eaten, monster-stuck and eventually let the view know something happened so it repaints. Whats also the point, that the view responds to the keypresses of the actor and that those events should reach the human in some way (also with an event).
-
Can someone help me with how I can best solve this issue? I’ve searched the internet for likewise problems but didn’t came across one.
-
In MVC: does the controller know the view? If so: does the whole program begins with the controller or with the view? (what makes who)
Basically Event Handling mechanism is just a producer-consumer pattern, imagine you are producing some events(an action) and there are set of listeners who needs to be notified about your action.
whether you want to use to use the Java built-in event Handling depends on how much code you have already written, if refactoring your code to use the Java event Handling requires a lot of effort, and you have only limited set of events, then you can write your own message passing system. But obviously, using Java event handling mechanism should be preffered, since it takes cares of notifying all the listeners who are registered for that event, you dont need to worry about notifying each and every listener and later it will help you in debugging if anything goes wrong. I Hope it answers your first question
In short you can write your own events like HumanMovedEvent, MonsterMovedEvent etc.
Coming down to your 2nd question, yes in MVC, controller knows what all views it needs to trigger for any specific action. A controller can choose any specific view for any specific action, lets say if u do some action A, you can call view V.
and yes your program begins with sending a request from your UI to Controller. Controller then chooses what View it needs to render for that specific action.
I hope i made it clear 🙂