playMaker

Author Topic: Tower Defense game  (Read 5268 times)

Mark_T

  • 1.2 Beta
  • Junior Playmaker
  • *
  • Posts: 72
    • View Profile
Tower Defense game
« on: February 08, 2012, 06:15:58 AM »

One of the projects I`m working now is a tower defense game.
Well, to be honest I`m in the starting phase, and right now  I`m trying to see how this would be possible with Playmaker. This is one of the reasons I was happy seeing JeanFabre`s new Pool Manager 2 actions, which it`ll come very handy to spawn the creeps/units. My biggest problem so far is handling the towers AI: detecting the incoming units, firing to the proper units in the tower`s fire range, etc.
Any help in the right direction will be highly appreciated. :)

Cheers,

speedything

  • Playmaker Newbie
  • *
  • Posts: 13
    • View Profile
Re: Tower Defense game
« Reply #1 on: February 08, 2012, 07:13:22 AM »
There's probably quite a few ways to achieve this. If I was making it I'd probably do something like...

1. Make a detector sphere, and set the collider to "Trigger"
2. Create an FSM and add two states - "Not_Firing" and "Firing"
3. Use the "Trigger Event" action to switch between the two. When a unit enters the detector it will trigger the "Firing" state and when it leaves it will return to "Not_Firing". It will also store the collider object.
4. Somewhere else (maybe the same FSM or another one altogether) you're going to have to track the collider object with Get Position and use this to aim your bullets. The toughest bit will be predicting where it will be when the bullet hits, but I imagine there's some help on how to do this elsewhere on the net.
5. You will also have to be aware of the objects in the trigger radius so that it moves to the next when the first one enters.

Having written all that maybe (to take multiple enemies into account) it would be best to add an FSM to each one and reverse the system. They area the ones looking out for the trigger event and when they hit it they tell another FSM (on a different object) that they,
a. Are in the range of a tower
b. What tower they are in the range of
c. What there priority is (e.g. toughest, fastest, etc.)

This FSM will then get the postion of the highest priority one and then tell the tower to fire at that one until it dies or leaves the radius.


Mark_T

  • 1.2 Beta
  • Junior Playmaker
  • *
  • Posts: 72
    • View Profile
Re: Tower Defense game
« Reply #2 on: February 08, 2012, 07:47:07 AM »
Thanks speedything.
I was thinking the triggers way will the first one to try.
I`m not a coder, so no fancy tricks here. :)
Considering I might have in one moment 100 units and 100 towers on the map, my fear is that it might be a bit expensive.I don`t know, I`m just guessing here. My initial target are the mobile platforms.
Are there any other methods?

Again, thanks a lot for your input.
I`ll give it a go to see what I come up with.


speedything

  • Playmaker Newbie
  • *
  • Posts: 13
    • View Profile
Re: Tower Defense game
« Reply #3 on: February 08, 2012, 08:14:37 AM »
Mobile optimisation isn't my strong point, but the two things I can think of are -

1. Use "Get Distance" from each of your towers or units, but use it as infrequently as you can get away with.
2. Stick with the physics, but use the layers and Unity Physics settings to make sure that the units only have connections with detection zones - nothing else. (I was making a PC game with 1000s of people, and doing this not only sped it up significantly, but also stopped it crashing!)

Of course, 100s of towers and units has always got the potential to cause a few speed issues.


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14342
  • Official Playmaker Support
    • View Profile
Re: Tower Defense game
« Reply #4 on: February 10, 2012, 02:28:44 AM »
Hi Mark_t

 Bear with me for the pool manager 2 actions, When Unity and Playmaker will go out of beta, I will have more time to finalize them actions ( I have an issue with a pool manager action currently).

As for distance check, colliders are very performant, so don't fear using them ( and Speedything tips are definitely useful). If you really want to get the distance, use .sqrDistance instead, it will be quicker internally ( saves the square root expensive computation). If you know this from scratch, you simply have to raise your distance triggers variables to the power of two.

Bye,

 Jean



 Bye,

 Jean

Mark_T

  • 1.2 Beta
  • Junior Playmaker
  • *
  • Posts: 72
    • View Profile
Re: Tower Defense game
« Reply #5 on: February 16, 2012, 02:48:32 PM »
I made a simple scene and I was able to setup Playmaker to make the tower to damage one unit, nothing special here.
Indeed JeanFabre, I don`t see this possible without PM2 actions. that`s why I was so happy seeing that it`s coming. :)

I setup a similar scene with PM2 and, of course, nothing is working, but I have to say I was very impressed. Even though it didn`t worked, I was able to setup a scene with 4,000 instances and it worked on my Galaxy Note. Just the instances (simple spheres with 528 triangles) of course, nothing else in the scene.

I used the SimpleSpawner provided by Path-O-Logical in the demo scene.
I also made some simple tests.
I created one scene with a FSM attached to the instanciated unit and one scene with no FSM on the prefab unit. The funny thing is that the instanciated prefab with the FSM attached was creating 3 draw calls/unit and the prefab with no FSM attached was giving only 1 draw call/unit.

I think I would love to have a Playmaker Spawner action capable to attach at runtime prefab FSMs to the instances and to create this way different behaviours to the instances. Something similar to this: http://hutonggames.com/playmakerforum/index.php?topic=1059.msg4555#msg4555
And I suppose, some of the actions might need to be redesigned for such a purpose to be able to target the owner at runtime.
I tried to use the spawner provided here: http://u3d.as/content/corrupted-smile-studio/spawner-free/2C3, but even though it was stated by the developer that is supporting Pool Manager 2, I wasn`t able to make it work. I liked the idea of being able to spawn different units in multiple configurable waves.

Thanks again for your feedback and encouragement.

Mark_T

  • 1.2 Beta
  • Junior Playmaker
  • *
  • Posts: 72
    • View Profile
Re: Tower Defense game
« Reply #6 on: March 18, 2012, 04:05:38 PM »
Hi,

I setup a scene where the towers are sending an event to the moving unit (a tank). The tank`s FSM is looking like this:


At start it plays the animation and is going in the "do nothing" state, where is waiting for the enter trigger event from the tower. After that it checks if the health is 0 or grater and if it`s greater is adding -10 every 3 seconds.
When health is "0", the tank gets destroyed. If the tank lives the Tower`s collider area, it receives an trigger exit event and the FSM will go into the "do nothing 2" state.

In the next step, I involved Pool Manager 2 and things are getting complicated a bit. So, I have a few questions:
1. Every instance is getting a new name: Tank(Clone)001, Tank(Clone)002, Tank(Clone)003, etc.
The previous setup it doesn`t work anymore because it was setup for a game object named "Tank".
I created another FSM where, after get spawned the instance name is changed back to the initial name "Tank", so all the instances are named "Tank". I used "Get Name" action, stored the name into a variable and than "Set Name" action. Even so, the setup doesn`t work. The towers are sending the event to the object, but because in the scene are more than one "Tank" objects things are broken.
I was thinking that I need to identify the Tank that is entering the Tower`s collider, and than the tower to send the event to that freshly identified object. And the same for the next one, etc. So, it will be like an dynamic  identifier send event action. The tower will send the event to every object that collides. The question is: How do I identify the object that enters the Tower`s collider?

2. The second question is: How do I switch the destination object to which the Tower will send the event.

3. I tried to deSpawn the instances, but I wasn`t able to do it. Can someone post a screenshot or guide me in the right direction?

I think this is the most basic possible setup. Later, will move to more advanced things, but now I`m just trying to take things slow, so that I know what I`m doing. :)

Any help is highly appreciated.
Cheers,

« Last Edit: March 18, 2012, 04:07:19 PM by Mark_T »

justifun

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 228
    • View Profile
Re: Tower Defense game
« Reply #7 on: March 20, 2012, 02:40:46 PM »
To answer question 1.

If you use a "trigger event" action to determine when a tank gets near a tower and tag your enemies with a tag of "enemy" for example. You can store the collider into a variable, thus no matter which tank enters (tank001 tank002 etc, ) the result will be dynamic as you suggested.

I'm a bit confused as what you want to do with question 2.  can you eloborate?