Thing.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. const Thing = {};
  2. // const T = Things = require('./Things');
  3. const _types = {};
  4. const Behavior = require('./Behavior');
  5. const Game = require('../../lib/Geoffrey/Game');
  6. Thing.create = function createThing (opts, game) {
  7. if (typeof game === 'undefined') {
  8. throw new Error('game scope is missing, cannot create Thing!')
  9. }
  10. // first, determine what the name of the thing will be
  11. // if a Thing has a type, Geoffrey will automatically give the Thing a name with an auto-incremented ID
  12. let name;
  13. if (opts.type) {
  14. if (typeof _types[opts.type] === 'undefined') {
  15. // check _types, if doesn't exist add new key and set to 0
  16. _types[opts.type] = 0;
  17. } else{
  18. // if key exists, increment the value
  19. _types[opts.type]++;
  20. }
  21. name = opts.type + '-' + _types[opts.type];
  22. }
  23. if (opts.name) {
  24. name = opts.name;
  25. }
  26. // console.log('creating thing with name: ', name, opts);
  27. // console.log('ooo', opts, game)
  28. let thing;
  29. // TODO: allow other types of things to be created, besides physics / matter things
  30. if (opts.gameobject === 'group') {
  31. thing = game.add.group();
  32. }
  33. else if (opts.gameobject === 'text') {
  34. thing = game.add.text(opts.x, opts.y, opts.text, opts.style);
  35. } else if (opts.matter === false) {
  36. thing = game.add.sprite(opts.x, opts.y, opts.texture, null);
  37. } else {
  38. thing = game.matter.add.sprite(opts.x, opts.y, opts.texture, null, { isStatic: opts.isStatic });
  39. }
  40. thing.setDepth(10);
  41. thing.behaviors = thing.behaviors || {};
  42. thing.name = name;
  43. thing.inputs = opts.inputs || {};
  44. if (typeof opts.sync !== 'undefined') {
  45. thing.sync = opts.sync;
  46. }
  47. if (typeof opts.height !== 'undefined') {
  48. thing.height = opts.height;
  49. //thing.displayHeight = opts.displayHeight;
  50. }
  51. if (typeof opts.width !== 'undefined') {
  52. thing.width = opts.width;
  53. // thing.displayWidth = opts.displayWidth;
  54. }
  55. // Namespace added for Geoffrey, easier this way to reference anything Geoffrey is doing vs Phaser.io API
  56. thing.G = {
  57. name: name,
  58. texture: opts.texture
  59. };
  60. if (opts.owner) {
  61. thing.G.owner = opts.owner;
  62. }
  63. thing.G.destroy = function () {
  64. var name = thing.name;
  65. let T = game.Things;
  66. // console.log("DESTROY", name, T[name])
  67. if (typeof T[name] !== "object") {
  68. // if it doesn't exist in Things, it shouldn't exist at all
  69. thing.destroy();
  70. return;
  71. }
  72. // first detach / remove all behaviors
  73. var bs = T[name].behaviors;
  74. if (bs) {
  75. Object.keys(bs).forEach(function (b) {
  76. if (typeof bs[b] === "object") {
  77. if (typeof bs[b].remove === "function") {
  78. bs[b].remove(T[name]);
  79. }
  80. Behavior.detach(b, T[name], {});
  81. }
  82. });
  83. }
  84. // then actually destroy the thing ( phaser.io sprite level destroy )
  85. thing.destroy();
  86. if (thing.attachments) {
  87. thing.attachments.getChildren().forEach(function(a){
  88. a.destroy();
  89. });
  90. }
  91. // delete references to the thing in Things memory
  92. delete T[name];
  93. // delete actual thing itself ( javascript level destroy )
  94. delete thing;
  95. }
  96. game.Things = game.Things || {};
  97. game.Things[thing.name] = thing;
  98. return thing;
  99. };
  100. Thing.inflate = function inflateThing (thingy) {
  101. // TODO: must check if Thing already exists, if so, then we want to apply the values and not create duplicate
  102. // console.log('Things[thingy.name]', thingy.name, Things[thingy.name])
  103. if (Things[thingy.name]) {
  104. //Things[thingy.name].x = thingy.x;
  105. //Things[thingy.name].y = thingy.y;
  106. //Things[thingy.name].body.velocity = thingy.velocity;
  107. //Things[thingy.name].body.angle = thingy.angle;
  108. // Things[thingy.name].rotation = thingy.rotation;
  109. Things[thingy.name].health = thingy.health;
  110. } else {
  111. // takes a serialized thing type structure ( thingy ),
  112. // and reinflates it back into an actual Thing using Thing.create
  113. let thing = Thing.create({
  114. name: thingy.name,
  115. owner: thingy.owner,
  116. texture: thingy.texture
  117. });
  118. for (let b in thingy.behaviors) {
  119. Behavior.attach(b, thing, thingy.behaviors[b].opts)
  120. }
  121. }
  122. };
  123. module.exports = Thing;