I have a game with 2 players. They have almost same beginning data and of course it changes in the course of the game.
I am guessing I need some king of constructor object that I will replicate and then modify, also I need it to be JSON for easy ajax sending.
Is there a design pattern that can help me? I prefer not to use database since I need the data only for a single game instance.
This is the structure I use now for one player:
player = {
"active" : true,
"room" : openRoom,
"id" : playerID,
"name": username,
"hp" : 5,
"units" : {
1 : {
"id" : 1,
"hp" : 3,
"row" : 1,
"square" : 1
},
2 : {
"id" : 2,
"hp" : 4,
"row" : 2,
"square" : 1
},
3 : {
"id" : 3,
"hp" : 5,
"row" : 3,
"square" : 1
}
}
};
There are several design patterns regarding object instantiation and initialization. However, you don’t seem to need any inheritance or “property sharing” techniques, so a very simple function that returns a new object should fit well as a constructor. You even can use object literals in coding it.
An example of two constructor functions, each with some parameters: