playMaker

Author Topic: 3 questions regarding tile based game  (Read 3261 times)

haro2424

  • Playmaker Newbie
  • *
  • Posts: 4
3 questions regarding tile based game
« on: January 20, 2017, 09:23:04 AM »
 Hi guys

    Newbie here. I'm currently working on a small base building game that has thousands of tile objects as the game world.

    1)I've named each tile by their world coordinate. When I need to find them, I translate my mouse coordinate->world coordinate->string -> "find game object with name". Is there a more elegant way for this?  :o

   2)I'm struggling to create previews of the objects I wish to instantiate while holding down and dragging my cursor over the map(multiple preview objects). Can anyone offer any tips or resources that may point me in the right direction? I can't seem to find much with regard to playmaker when it comes to rts or tile based world.

   3) Last one! Would MVC design pattern work well with playmaker? I have a mousemanager setup with multiple FSMs that communicates with global variables. I'm thinking of creating another FSM to simply store all mouse related data...and do the same with other manager objects. What do you think?

Any help is appreciated. Thanks! :D :)

Chizzler

  • Junior Playmaker
  • **
  • Posts: 67
Re: 3 questions regarding tile based game
« Reply #1 on: January 21, 2017, 12:35:53 PM »
I'm going to tackle this in reverse order;

3) Sorry, I'm not familiar with MVC Design Patterns. As for the other part, I too seperate my Mouse Input into it's own FSM. It depends on your needs, but it's the best way i've found to avoid it missing an input from the mouse because it wasn't in the right state at the time.

2) In my Project, I used an empty Game Object as my "Previewer" and would use the "Set Property" Action to change the mesh and collider size to match the object I was previewing. I'd also change the material color based on whether it was placeable or not (Simple Green/red). When it came time to place, i could despawn that Preview object and spawn a real one at the position.

1) This is a difficult question to answer as it depends a lot on how your scene is laid out. If your Tiles have colliders, it's as simple as doing a "Mouse Pick" action to fire a raycast out that'll store the hit tile as a variable.

If your tiles don't have colliders (and if we're dealing with 1000's they probably shouldn't) then are the tiles all aligned to a grid? You can use Arrays (Lists of variables) in addition to a little math to convert a world position to a corresponding index (Position in the Array List) of a tile on a grid.
It's not a process I can quickly sum up in a few sentences, but i've attached a Sample of this in action below. The important things to note are the way the grid is set up and that the logic would need more math if it didn't start at (0,0,0) Or the tile size was different.
I may have to do a proper tutorial on this at some point.
« Last Edit: January 21, 2017, 01:58:15 PM by Chizzler »

haro2424

  • Playmaker Newbie
  • *
  • Posts: 4
Re: 3 questions regarding tile based game
« Reply #2 on: January 21, 2017, 03:15:37 PM »
Thanks for the input and sample Chizzler  :) I'll have a tweak at this!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 3 questions regarding tile based game
« Reply #3 on: January 23, 2017, 01:10:51 AM »
Hi,

1: Find GameObject action is a bad action because Unity internal api to find a GameObject by Name is known to be very slow, so no, this is not a good idea :)

use ArrayMaker or FsmArray to store your tiles, use a Hashtable with ArrayMaker or two FsmArray, one for the keys "string", and one for Values: the GameObjects representing the tile.

but all in one, Tile Based Games requires some heavy core features, maybe you'd be better off using a ready made system and build the custom actions to control that system within PlayMaker. It depends your level of expertise really.


2: Can you describe what you mean by preview? maybe an example in a real game or something?


3: MVC works well-ish, because of the component based Unity approach, it's not really a clear fit and so you'll likely end up with a View/Component with the Model being spread out everywhere, but if you are serious about it, it can be 90% in the Component. For an exampl at what MVC would look like ( At least my version of it), check out the "ugui IOS View Controller". If you are familiar with xCode and IOS development, you'll find the same patterns of communication and MVC setup basically.

Bye,

 Jean

haro2424

  • Playmaker Newbie
  • *
  • Posts: 4
Re: 3 questions regarding tile based game
« Reply #4 on: January 23, 2017, 09:09:15 AM »
Hi Jean

    Thank for the tips. ;D And with regards to previews...in the RTS games I've played, when you select an object you wish to build, say a train station. Underneath your cursor, there should be a train station sprite/GO tinted in green. If you wish to build walls, clicking and dragging in a direction should display multiple wall sprite/GO with a tinted color. Upon releasing the mouse, the tinted sprites would be installed at that location to indicate construction queue.