Tuesday 28 January 2014

Endless Runner(Part 2)


Ok time to get your hands dirty with code. In game we will have small class to represent cell properties.We will be doing most of the code in one script(Using JS). Lastly I am not saying it is the best approach which I will share, this is what I know there could be better other solutions too.

Create a new JS script name it GameController . First of all we will declare few enumeration type variables for representing directions and type of cell.


enum direction { N, E, W, S, NS, EW}
enum cellType {run,jump,jumpObstacle,duck,left,right}


now create an inner class in GameController name it "CellProperties".  It will hold the information about the cell that will be displayed on screen. The member variables in class will be position,dir,type,model,neighbours,coins,spikes and visited bool.

class CellProperties

{

var position:Vector3;
var dir:direction;
var type:cellType;
var model:GameObject;

var neighbourN:CellProperties;
    var neighbourE:CellProperties;
    var neighbourW:CellProperties;
    var neighbourS:CellProperties;
    var visited=false;
    var coins = new ArrayList();
    var spikes = new ArrayList();
    
    function setValues( cPosition:Vector3,  cDir:direction, cType:cellType)
    {
   
    this.position  = cPosition;
this.dir = cDir;
this.type = cType;
this.neighbourN = null;
this.neighbourE = null;
this.neighbourW = null;
this.neighbourS = null;
    }

};

This is the structure of cell class. In next part I will setup the scene and make the prefebs for the cells. 
Thank you.

Here you can find the source code : https://dl.dropboxusercontent.com/u/79608708/CellClass.js

No comments:

Post a Comment