How to Organize Code in a Resource Management Phaser Typescript Project? -


i'm making type of resource-management type game , need sort of "collection manager" in between states , objects. i'm using typescript.

let's example game raising cats. in 1 state, player can skip forward 1 year. when happens want go through collection of cats , select pairs become parents of new cats. in state player can feed 10 cats (changing hunger values). in 2 different states, need update collection of cats.

i had code inside each state , collection of cats passed around through localstorage (or phaser cache). code has become quite messy.

so want create abstraction layer between states , collection of objects. responsible updating cats, creating new ones, , returning subsets of cats based on passed criteria. static layer don't have pass between states. love able call functions like:

catmanager.generatenewcats(10); catmanager.getcats({minage:2, maxage:5}); catmanager.incrementages(); 

how go organizing this?

here 1 potential solution:

/** creates guid (uuid v4) */ function createid(): string {     return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {         var r = math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);         return v.tostring(16);     }); }   class cat {     id: string;     constructor() {         this.id = createid();     } } class manager {     cats: {}     getnew(howmany: number): cat[] {         const result: cat[] = [];         (let index = 0; index < howmany; index++) {             const cat = new cat();             this.cats[cat.id] = cat;         }         return result;     }  }  export const manager = new manager(); 

you can identify cat id. can extend manager buffer places local storage if want.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -