Basic Game Framework Introduction

Source: Internet
Author: User


1. Game controller. The game controller is the center that controls the communication between the different parts of the game, and will make up the various components of the game sticking together.
2. Scene Manager. This script mainly handles the task of loading the game scenes to save the game scene.
3.UI Manager. The UI manager draws some user interfaces in the game to display some information about the user.
4. Sound Manager. The sound manager handles playback controls for sound, and so on.
5. Players. Players are sometimes directly present in the scene, sometimes through the game controller to instantiate the player.
6.AI and enemies. As with players, some enemies are in the beginning, and some are loaded in real time.
Creating a Core Framework script
BaseGameController.cs main functions:
1. Track the game state: Save the game state, provide a function method to change the game state, change the game state by calling some functions like endgame ().
2. Create a Player object
3. Set up the camera (player follow)
4. State management, user management, and communication between game behavior
5. Communication between some standalone scripts or non-essential rewrite scripts
A typical game should have some of the following core states:
The game starts, pauses, loads, ends.
Basic game controller Script

 Public classBasegamecontroller:monobehaviour {BOOLPaused PublicGameobject Explosionprefab; Public Virtual void Playerlostlife()    {    } Public Virtual void Spawnplayer()    {    } Public Virtual void respawn()    {    } Public Virtual void Startgame()    {    } Public void Explode(Vector3 aposition)    {Instantiate (Explosionprefab, aposition, quaternion.identity); } Public Virtual void enemydestroyed(Vector3 Apositon,intPointsvalue,intHitbyid) {} Public Virtual void bossdestroyed()    {    } Public Virtual void restartgamebuttonpressed() {application.loadlevel (application.loadedlevelname); } Public BOOLPaused {Get{returnPaused }Set{paused =value;if(paused) {Time.timescale =0; }Else{Time.timescale =1; }        }    }}

Scene Management

 Public classScenemanager:monobehaviour { Public string[] levelname; Public intGamelevelnum;voidStart () {dontdestroyonload ( This. Gameobject); } Public void Loadlevel(stringScenename) {application.loadlevel (scenename); } Public void Gonextlevel()    {if(Gamelevelnum >= levelname.length) Gamelevelnum =0;        Loadlevel (Gamelevelnum);    gamelevelnum++; }Private void Loadlevel(intIndexnum) {loadlevel (levelname[indexnum]); } Public void Resetgame() {Gamelevelnum =0; }}

Extending Monobehavior is a good way to avoid repeating definitions of functions and variables

 Public classExtendedcustommonobehaviour:monobehaviour { PublicTransform Mytransform; PublicGameobject Mygo; PublicRigidbody Mybody; Public BOOLDidinit; Public BOOLCancontrol; Public intId [System.nonserialized] PublicVector3 Tempvec; [System.nonserialized] PublicTransform temptr; Public Virtual void SetId(intAnID) {id = AnID; }}

Basic User Management
Store player information: Name, score, highest score, registration, whether customs clearance and other information

 Public classBaseusermanager:monobehaviour {Private intScore;Private intHighscore;Private intLevelPrivate intHealthPrivate BOOLisfinished; Public stringPlayerName ="Kitty"; Public Virtual void Getdefaultdata() {playername ="Kitty"; Score =0; Level =1; Health =3; Highscore =0; isfinished =false; } Public string GetName()    {returnPlayerName; } Public void SetName(stringAName) {playername = AName; } Public int Getlevel()    {returnLevel } Public void SetLevel(intnum) {level = num; } Public int Gethighscore()    {returnHighscore; } Public int Getscore()    {returnScore; } Public Virtual void Addscore(intAmount) {score + = amount; } Public void Lostscore(intNum) {score-= num; } Public void SetScore(intnum) {score = num; } Public int Gethealth()    {returnHealth } Public void Addhealth(intNUM) {health + = num; } Public void Reducehealth(intNUM) {health-= num; } Public void Sethealth(intnum) {health = num; } Public BOOL getisfinished()    {returnisfinished; } Public void setisfinished(BOOLAval) {isfinished = Aval; }}

Basic player Management:

 Public classBaseplayermanager:monobehaviour { Public BOOLDidinit; PublicBaseplayermanager DataManager; Public Virtual void Awake() {Didinit =false;    Init (); } Public Virtual void Init() {DataManager = gameobject.getcomponent<baseusermanager> ();if(DataManager = =NULL) DataManager = gameobject.addcomponent<baseusermanager> (); Didinit =true; } Public Virtual void gamefinished() {datamanager.setisfinished (true); } Public Virtual void Gamestart() {datamanager.setisfinished (false); }}

Basic Input Control

 Public classBaseinputcontroller:monobehaviour { Public BOOLup; Public BOOLDown; Public BOOLLeft; Public BOOLright; Public BOOLFire1; Public BOOLSLOT1; Public BOOLSlot2; Public BOOLSLOT3; Public BOOLSLOT4; Public BOOLSLOT5; Public BOOLSLOT6; Public BOOLSLOT7; Public BOOLSlot8; Public BOOLSlot9; Public floatVert Public floatHorz; Public BOOLShouldreapawn; PublicVector3 TEMPVEC3;PrivateVector3 Zerovector =NewVector3 (0,0,0); Public Virtual void Checkinput() {Horz = Input.getaxis ("Horizontal"); Vert = Input.getaxis ("Vertical"); } Public Virtual float Gethorizontal()    {returnHorz; } Public Virtual float getvertical()    {returnVert } Public Virtual BOOL Getfire()    {returnFire1; } Public BOOL Getrespawn()    {returnShouldreapawn; } Public VirtualVector3Getmovementdirectionvector() {TEMPVec3 = Zerovector;if(Left | |        right) {tempvec3.x = Horz; }if(Up | |        Down) {tempvec3.y = vert; }returnTEMPVEC3; }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Basic Game Framework Introduction

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.