Sign in to follow this  
Melkon

Question about TD game's code design

Recommended Posts

Hello!

 

I have a half finished TD and i decided to finish it. When i did that project i used pretty bad design, and i had a circular dependency, now i want to fix that.

 

The idea is:

Make a GameController class, the event loop run here, but GC won't handle it, it just decide who should handle it (the User interface or the map), and convert the mouseclick coordinate depending on the view of the handling class.

 

If there is an actual command from the UI (like Build XY Tower) the gamecontroller get it and drop down to the map to execute it.

 

I am sure it can work, but i am not entirely sure that's the best solution i can do.

 

Any opinion is appreciated!

Melkon

Share this post


Link to post
Share on other sites
I'm on my phone so this answer will have to be much shorter than I would like to give.

Basically you want an event manager. One design is where The Input Manager would send a MouseDown event to the game view. The game view passes the event down to the screen layers, top to bottom.

The top screen layer is your GUI so it does a hit test to see if a button was pressed. If the "fire arrow tower" button was hit, send an event to the TowerSpawner system, which sets it's internal state to spawn fire arrow towers when it receives a MouseDown event.

It gets the MouseDown event when you click on a screen area not covered by the GUI. Basically the View picks up the MouseDown event from the InputManager again, then sends it to the top screen layer. The GUI Screen layer does a hit test again, it fails because no button was pressed, so the view passes the event down to the next screen layer, the scene view. The scene view passes it to subscribed system along with screen coordinate data, in this case the TowerSpawner system and in some games possibly the camera system, they react appropriately.

The TowerSpawner tries to spawn a tower at the current screen coordinates (or asks for a raycast from the screen coordinates if it's a 3d game) using the currently tower state that was set by the earlier button press.

Something like that would be a fairly common approach.

Share this post


Link to post
Share on other sites

I'm on my phone so this answer will have to be much shorter than I would like to give.

Basically you want an event manager. One design is where The Input Manager would send a MouseDown event to the game view. The game view passes the event down to the screen layers, top to bottom.

The top screen layer is your GUI so it does a hit test to see if a button was pressed. If the "fire arrow tower" button was hit, send an event to the TowerSpawner system, which sets it's internal state to spawn fire arrow towers when it receives a MouseDown event.

It gets the MouseDown event when you click on a screen area not covered by the GUI. Basically the View picks up the MouseDown event from the InputManager again, then sends it to the top screen layer. The GUI Screen layer does a hit test again, it fails because no button was pressed, so the view passes the event down to the next screen layer, the scene view. The scene view passes it to subscribed system along with screen coordinate data, in this case the TowerSpawner system and in some games possibly the camera system, they react appropriately.

The TowerSpawner tries to spawn a tower at the current screen coordinates (or asks for a raycast from the screen coordinates if it's a 3d game) using the currently tower state that was set by the earlier button press.

Something like that would be a fairly common approach.

 

Your solution is a bit more complicated than the solution i imagined. For example i didn't want a TowerSpawner system, as i imagined the GUI have a command, and i just get that command from the event loop and pass down to the map. The command can be also a Spell cast, not just Build tower, i don't exactly know yet how much thing can it be. I will think about that.

 

Thanks for your answer!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this