State-based cartoon farm (hayday)

Source: Internet
Author: User

The cartoon farm recently launched by supercell, also known as hayday, is very popular. Here I will also write about the main framework of the game.

The core of this game is relatively simple, that is, a state machine is constantly operating. Here we want to use the State mode for implementation. Why should we choose a state machine, because the vast majority of roles in the game are changing their statuses, it is appropriate to use the state machine.

First, analyze the elements in the game to ensure accurate classification, roughly as shown in Figure

 

  Diable Building Free Baking Mature Hungry Gain  
Captive/Stocking                
Plants                
Fish                
Machine (shrimp, machine, ore, Train Station)                
Consumers (trucks, buyers)                
Decoration (stone, treasure chest, decorative tree, other decorations, spread)                
Fruit type                

Different designers may have different starting points for classification. I classify them based on the number and type of States.

 

Using unityengine; using system. collections; public abstract class absstate {// name of the current State Public String strstatename _; // model of the current State Public gameobject curstatemodel _; // The animation segment corresponding to the current State Public animationclip curstateclip _; // The state duration public long lduration _; // The switching state public abstract void switchstate (Unit );}
Using unityengine; using system. collections; // period of the unit. For example, a tree contains the first generation, the second generation, the third generation, and the fourth generation (the fourth generation is the generation rescued by friends) Public Enum enage {en_age_1, en_age_2, en_age_3, en_age_4 ,} // The unit of the state machine involved is public abstract class unit {// The current state is public absstate curstate _; // The unit of multi-task shows that all tasks are completed, for example, if the machine may have multiple grids in the production process, the public bool bfinishtask _ state must be switched only when all the grids have completed the task _; // unit location: Public vector2 position _; // unit age: Public enage age _;}

The above is the main abstract body. One is responsible for the State, and the other is the auxiliary unit. This also meets the design requirements of the state mode. The unit has a connection to the virtual state.

The next step is to process the specific unit and status.

 

using UnityEngine;using System.Collections;public class CAnimal : Unit{}
/// <Summary> /// disable state. /// not activated /// </Summary> using unityengine; using system. collections; public class disablestate: absstate {public override void switchstate (Unit) {If (unit is cmachine) {debug. log ("disablestate-> buildingstate"); unit. curstate _ = new buildingstate ();} else {debug. log ("disablestate-> freestate"); unit. curstate _ = new freestate ();}}}

 

using UnityEngine;using System.Collections;public class FreeState : AbsState{    public override void SwitchState (Unit unit)    {        if(unit is CMachine)        {            Debug.Log("free -> BakingState");            unit.curState_ = new BakingState();        }        else if(unit is CProduct)        {            unit.curState_ = new GainState();            Debug.Log("free-> GainState");        }        else        {            Debug.Log("free -> matrue");            unit.curState_ = new MatureState();        }    }}

The Code of all States and all specific units is omitted here, which is similar.

Client call

Using unityengine; using system. collections; public class client: monobehaviour {unit; // use this for initialization void start () {// Initialize an animal unit = new canimal (); // The Initial animal body is an inactive unit. curstate _ = new disablestate ();} // update is called once per frame void Update () {// test: raise the Space key to switch the status. // most of the game's status is switched by countdown. This does not indicate the mode of each switch. If (input. getkeyup (keycode. space) {unit. curstate _. switchstate (unit );}}}

 

Welcome to shoot bricks

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.