2
\$\begingroup\$

I am thinking of creating a sprite base 2d top down farm simulation game. The game uses tiles and I was wondering (conceptually) what is the best way to handle the farm tiles. Each tiles should have the following list of features.

  • The player should be able to add seeds to each tile

  • The player should be able to plant a variety of vegetables.

  • Plants should have different stages of growth

  • The player should be able to interact with each tile (dig, cover holes, water, add fertilizer, etc).

  • The farm layout can have a variety of shapes (i.e. Not just a square (9 x 9) or rectangle (9 x 15) a shaped farm.)

Currently I have a script that generates a square or rectangle shaped like farm, each tile is a sprite object with a 2D collider. I have come up with a solution for planting and the different stages of growth. However, it isn't the most elegant solution. My current solution is to create prefab for each plant. Then once a seed is planted onto a tile, create a plant object in the location of the tile (using it's vector position), and make the plant a child object to that tile. Each plant will have an age counter that increase when a new day is triggered. To show the growth stage swap the texture depending on the plant's age.

My biggest problem with the implementation is that if the players farm is very large the game will have to create a hundreds object, which seem very taxing. Is that a different way achieve these features without the use of so many objects? If so, how would I implement it (conceptual of course). Thanks!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Seems very taxing? Are you sure that it actually will be? Have you tried it? Developers are notoriously bad at estimating performance bottlenecks. \$\endgroup\$
    – Philipp
    Apr 27, 2016 at 7:49
  • \$\begingroup\$ Your approach is fine. You need to keep coding and determine when it is time for you to look at optimizing. At this point, you will need to profile and determine what to optimize, and then profile again and see how effective your optimizations were. It sounds like you are on the right track for now at least. \$\endgroup\$
    – jgallant
    Apr 27, 2016 at 11:13
  • \$\begingroup\$ @Philipp I have tried this and it's actually working fine. But yes your link is very relevant. I am worrying about pre-optimizing too much. \$\endgroup\$
    – Pav Dis
    Apr 27, 2016 at 12:28
  • \$\begingroup\$ @Jon Thanks for the feedback. I will definitely keep that in mind while I continue with this project. I suppose I just needed a second opinion on my implementation. I just wanted to make sure I'm not "reinventing the wheel". If there already is a well known solution. \$\endgroup\$
    – Pav Dis
    Apr 27, 2016 at 12:32

2 Answers 2

1
\$\begingroup\$

As your game appears to use rectangular tiles; for any arbitrarily shaped farm, there is an optimised subdivision of that farm into rectangles. Often, far fewer than you'd think. So, for now, let's ignore your last requirement.

Second, you want to be able to have different images on different tiles depending on plant type and growth stage. From a graphics perspective, there is not really a difference between these two.

  • Subdivide your farm into rectangles.
  • Generate a mesh, such that each tile has 4 owned corner vertices.
  • Set for the texture of the mesh an extended tilemap (as an image, not an actual unity tilemap), and manipulate vertex UVs to vary what each individual tile shows.
  • For Collision, use a simple rectangle collider and then apply some relative coordinate math to determine the exact tile that is affected.

There, all one object per subdivision. Each of these steps can have some tricky components, but this should be enough to get you started. For huge maps, be sure to keep mesh vertex limits in mind.

\$\endgroup\$
-5
\$\begingroup\$

I'm going to give you a rather stupid answer but what the heck. First of all hundreds of objects in a game is no problem. Secondly there is this guy called quil 18 that has great tutorials.

Start with the Basics of 2d tile maps and then you can watch this gigantic series about a base building game here

\$\endgroup\$
3
  • \$\begingroup\$ First is a generalization which greatly depends on what these 100 objects are doing and how they are doing it. Secondly does not add much value either. \$\endgroup\$
    – Philipp
    Apr 27, 2016 at 7:55
  • \$\begingroup\$ They are obviously going to be 2d tiles each with a behavior script and possibly one or two animations or just multiple sprite states. Sorry i did not state that in my answer, because I thought it was made clear by the question itself. And secondly one of my links is to a rather high quality and yet short tutorial on exactly what the OP wants to know. Why should I waste time typing out a 2 page answer. \$\endgroup\$
    – Uri Popov
    Apr 27, 2016 at 8:04
  • \$\begingroup\$ You don't really answer anything. This should be a comment on the question. \$\endgroup\$
    – nathan
    Apr 27, 2016 at 8:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .