Home Reference Source Repository

js/GameplayKit/GKScene.js

  1. 'use strict'
  2.  
  3. import NSObject from '../ObjectiveC/NSObject'
  4. //import GKSceneRootNodeType from './GKSceneRootNodeType'
  5. //import GKEntity from './GKEntity'
  6. //import GKGraph from './GKGraph'
  7.  
  8. /**
  9. * A container for associating GameplayKit objects with a SpriteKit scene.
  10. * @access public
  11. * @extends {NSObject}
  12. * @see https://developer.apple.com/documentation/gameplaykit/gkscene
  13. */
  14. export default class GKScene extends NSObject {
  15.  
  16. /**
  17. * constructor
  18. * @access public
  19. * @constructor
  20. */
  21. constructor() {
  22. super()
  23.  
  24. // Accessing the SpriteKit Scene
  25.  
  26. /**
  27. * The SpriteKit scene managed by this GKScene object.
  28. * @type {?GKSceneRootNodeType}
  29. * @see https://developer.apple.com/documentation/gameplaykit/gkscene/1640947-rootnode
  30. */
  31. this.rootNode = null
  32.  
  33.  
  34. // Managing Entities and Components
  35.  
  36. this._entities = []
  37.  
  38. // Managing Pathfinding Graphs
  39.  
  40. this._graphs = {}
  41. }
  42.  
  43. // Loading a Scene File
  44.  
  45. /**
  46. * Loads the specified SpriteKit scene file, creating a GKScene object containing the SpriteKit scene and associated GameplayKit objects.
  47. * @access public
  48. * @param {string} filename - The name of a scene file in your app’s main bundle.
  49. * @returns {void}
  50. * @desc Use this initializer to load SpriteKit scenes (.sks files) created in the Xcode SpriteKit scene editor that contain associated GameplayKit entities, components, and pathfinding graphs.
  51. * @see https://developer.apple.com/documentation/gameplaykit/gkscene/1640935-init
  52. */
  53. static sceneWithFileNamed(filename) {
  54. const scene = new GKScene()
  55. // TODO: implement
  56. return scene
  57. }
  58.  
  59. // Managing Entities and Components
  60.  
  61. /**
  62. * Adds a GameplayKit entity to the list of entities managed by the scene.
  63. * @access public
  64. * @param {GKEntity} entity - The entity to be added to the scene.
  65. * @returns {void}
  66. * @see https://developer.apple.com/documentation/gameplaykit/gkscene/1640954-addentity
  67. */
  68. addEntity(entity) {
  69. if(this._entities.indexOf(entity) < 0){
  70. this._entities.push(entity)
  71. }
  72. }
  73.  
  74. /**
  75. * Removes a GameplayKit entity from the list of entities managed by the scene.
  76. * @access public
  77. * @param {GKEntity} entity - The entity to be removed from the scene.
  78. * @returns {void}
  79. * @see https://developer.apple.com/documentation/gameplaykit/gkscene/1640686-removeentity
  80. */
  81. removeEntity(entity) {
  82. const index = this._entities.indexOf(entity)
  83. this._entities.splice(index, 1)
  84. }
  85.  
  86. /**
  87. * The list of GameplayKit entities managed by the scene.
  88. * @type {GKEntity[]}
  89. * @desc When you add entities (and their components) to a scene in the Xcode SpriteKit scene editor, Xcode automatically adds them to this array.
  90. * @see https://developer.apple.com/documentation/gameplaykit/gkscene/1640795-entities
  91. */
  92. get entities() {
  93. return this._entities.slice()
  94. }
  95.  
  96. // Managing Pathfinding Graphs
  97.  
  98. /**
  99. * Removes a pathfinding graph from the list of graphs managed by the scene.
  100. * @access public
  101. * @param {string} name -
  102. * @returns {void}
  103. * @see https://developer.apple.com/documentation/gameplaykit/gkscene/1640663-removegraph
  104. */
  105. removeGraph(name) {
  106. if(this._graphs[name]){
  107. delete this._graphs[name]
  108. }
  109. }
  110.  
  111. /**
  112. * The list of pathfinding graph objects managed by the scene.
  113. * @type {Map<string, GKGraph>}
  114. * @desc When you define pathfinding graphs in the Xcode SpriteKit scene editor, Xcode automatically adds them to this array.
  115. * @see https://developer.apple.com/documentation/gameplaykit/gkscene/1640940-graphs
  116. */
  117. get graphs() {
  118. return this._graphs
  119. }
  120.  
  121. // Instance Methods
  122.  
  123. /**
  124. *
  125. * @access public
  126. * @param {GKGraph} graph -
  127. * @param {string} name -
  128. * @returns {void}
  129. * @see https://developer.apple.com/documentation/gameplaykit/gkscene/2143063-addgraph
  130. */
  131. addGraph(graph, name) {
  132. this._graphs[name] = graph
  133. }
  134. }