How to implement plants in a Plant vs Zombie type game? Unity

I want to try to make a game like Plants vs Zombies. But I do not understand how best and most correctly to implement plants. In the original game, there are about 50-plus pieces - I don't need so many , I need 10-15 pieces. What is the best way to implement this?

Can make a general class Plant, in which to create variables for the name of the plant, its damage, health, sprite, etc., and then for each object **create an instance of a class, specifying its own variables for each plant? But what to do then? I will create a prefab of the plant and what script in this case should I hang on it?

Or you can for each type of plant write your own script, with your own variables. Each plant should be prescribed your behavior (sunflower-gives the sun, other plants-shoot, mine-explode when a zombie comes), and then just hang the script, for example, sunflower on the sunflower prefab. But in this case, I will have to create a lot of scripts: 1 for each plant. I think this is wrong.

Or you can... * do something else. *

P.S. Please don't write that "Here's another one who thinks he can implement a game that a team of professional developers has been making for several months/years.", it's better to just skip this question, because: I DON'T will make an exact copy, I WILL NOT I will go somewhere Putting this game out, I AM NOT going to put it anywhere at all, I just want to... test your strength or something... and try to do something similar. Maybe learn some new things and suddenly I got stuck on such a question. I hope for help, thank you in advance :)))

Author: Максим Фисман, 2020-07-30

1 answers

You will have to write more than one class, but not for every plant.

The GameUnit script simply contains a field with the skin's prefab and a link to the Skin script, the object it will create from the prefab, the line number field, the number of lives, the maximum lives, and the damage and death functions.

The responsibility of the Plant class (inherited from GameUnit) is simple: the price, the construction function, and the cell number field.

Skin with the functions play rise, idle, walk, attack, dead customized for the skin for both plants and zombies.

PlantBehaviour simply finds the Plant class and stores a reference to it, to get a reference to the skin and tile(line and cell).

AttackBehaviour the ability to shoot enemies on your line that makes you shoot is inherited from PlantBehaviour. Plants that have some unusual attacks (for example, attack enemies on all lines or neighboring ones) have their own script inherited from AttackBehaviour and change the functions of the enemy's fawn or the spawn of the projectile.

Getters have a script HarvestBehaviour, inherited all from the same PlantBehaviour.

The mines have their own script, and the walls do not need the behavior at all.

Zombie also inherited from GameUnit. Implements the attack of another GameUnit.

Zombies have their own scripts for movement as a behavior of plants and additional features such as a shield, etc.

Shells can also have different scripts.

Something like that.

 5
Author: Yaroslav, 2020-07-30 22:47:20