Advertisement
XArnonX

Sammelobjekte

Feb 16th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Sammelobjekte = (function () {
  2.  
  3. 'use strict';
  4.  
  5.  
  6. function Class(game,map) {
  7.  
  8.  
  9.     this.className = 'Objects';
  10. }
  11.  
  12.  // private methods
  13.   function preload() {
  14.     this.load.image('Objekt1',          'assets/images/Objekt1.png');
  15.     this.load.image('Objekt2',          'assets/images/Objekt2.png');
  16.     this.load.image('Objekt3',          'assets/images/Objekt3.png');
  17.     this.load.image('Objekt4',          'assets/images/Objekt4.png');
  18.     this.load.image('Objekt5',          'assets/images/Objekt5.png');
  19.     this.load.image('Objekt6',          'assets/images/Objekt6.png');
  20.     this.load.image('Objekt7',          'assets/images/Objekt7.png');
  21.     this.load.image('Objekt8',          'assets/images/Objekt8.png');
  22.   }
  23.  
  24.   function init(tile) {
  25.  
  26. /////////////////////////////////////
  27. this.objects = this.add.group();
  28.  
  29.  
  30. /////////////////////////////////////
  31.  
  32.     // set position vars
  33.     this.initialTile = tile;
  34.  
  35.     // add sprite
  36.     this.sprite = this.gameReference.add.sprite(
  37.       this.getTileX(1),
  38.       this.getTileY(1),
  39.       //Ursprung
  40.       //this.getTileX(this.initialTile.x),
  41.       //this.getTileY(this.initialTile.y),
  42.       this.spriteName
  43.     );
  44.  
  45.     // set anchor
  46.     this.sprite.anchor.setTo(0.5, 0.5);
  47.  
  48.  
  49. ////////////////////////////////////////////////////////////////
  50.   ErschaffeSammelobjekte: function() {
  51.     //Erschaffe die Sammelobjekte
  52.     this.Sammelobjekte = this.game.add.group();
  53.     this.Sammelobjekte.enableBody = true;
  54.     var Sammelobjekt;    
  55.     result = this.findObjectsByType('Sammelobjekt', this.map, 'Spielobjekte');
  56.     result.forEach(function(element){
  57.       this.createFromTiledObject(element, this.Sammelobjekte);
  58.     }, this);
  59.   },
  60.  
  61.   //find objects in a Tiled layer that containt a property called "type" equal to a certain value
  62.   findObjectsByType: function(type, map, layer) {
  63.     var result = new Array();
  64.     map.objects[layer].forEach(function(element){
  65.       if(element.properties.type === type) {
  66.         element.y -= map.tileHeight;
  67.         result.push(element);
  68.       }      
  69.     });
  70.     return result;
  71.   },
  72.   //create a sprite from an object
  73.   createFromTiledObject: function(element, group) {
  74.     var sprite = group.create(element.x, element.y, element.properties.sprite);
  75.  
  76.       //copy all properties to the sprite
  77.       Object.keys(element.properties).forEach(function(key){
  78.         sprite[key] = element.properties[key];
  79.       });
  80.   },
  81.  
  82.  
  83. ///////////////////////////////////////////////////////////////
  84.  
  85.   // public api
  86.   Class.prototype = {
  87.     preload: preload,
  88.     init: init,
  89.     update: update
  90.   }
  91.   // add the abstract's api
  92.   _.extend(Class.prototype, AbstractSprite.prototype);
  93.  
  94.   return Class;
  95.  
  96.  
  97.  
  98.  
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement