Game.re.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import AsyncLoader from 'Assets/Library/AsyncLoader';
  2. import * as RE from 'rogue-engine';
  3. import * as THREE from 'three'
  4. import CannonBody from '../../rogue_packages/rogue-cannon/Components/CannonBody.re'
  5. export default class Game extends RE.Component {
  6. loader = new AsyncLoader()
  7. @RE.Prop("Prefab") asteroid
  8. // @RE.Prop("Prefab") player
  9. start() {
  10. console.log("calling start", new Date().getTime())
  11. const expectedComponents = [
  12. "top-left", "top-middle", "top-right", "left-side", "right-side", "bottom-left", "bottom-middle", "bottom-right",
  13. ]
  14. this.loader.loadStaticText("space_hud.html").then(html => {
  15. // RE.Runtime.uiContainer.innerHTML = html
  16. expectedComponents.forEach(componentId => {
  17. let component = document.getElementById(componentId)
  18. let setupFunctionName = this.formatComponentName(componentId)
  19. if(this[setupFunctionName]) {
  20. //this[setupFunctionName](component)
  21. }
  22. })
  23. }, error => RE.Debug.logError(error))
  24. RE.Runtime.rogueDOMContainer.addEventListener('contextmenu', event => { event.preventDefault() })
  25. window.addEventListener('resize', this.onResize.bind(this))
  26. let field = RE.App.currentScene.getObjectByName("asteroidField")
  27. this.loader.loadStaticText("Data/level1.json").then(levelstring => {
  28. this.asteroids = []
  29. let objectData = JSON.parse(levelstring)
  30. for(let i = 0; i < objectData.length; i++) {
  31. let obj = objectData[i]
  32. if(obj[0] == "asteroid") {
  33. let asteroid = this.asteroid.instantiate(field)
  34. asteroid.position.copy(new THREE.Vector3(obj[1] ,1, obj[2]))
  35. asteroid.rotation.set(Math.PI * Math.random(), Math.PI * Math.random(), Math.PI * Math.random())
  36. // let size = (obj[1] % 3) + 1
  37. // asteroid.scale.set(size, size, size)
  38. // let body = RE.getComponent(CannonBody, asteroid).body
  39. // console.log(body)
  40. // setTimeout(() => {
  41. // body.shapes[0].radius = size / 2
  42. // body.mass = size
  43. // body.shapes[0].computeBoundingSphereRadius();
  44. // body.computeAABB();
  45. // body.updateMassProperties();
  46. // },0)
  47. // setTimeout(() => {
  48. // body.mass = size 10
  49. // let bodyShape = body.shapes[0]
  50. // body.removeShape(bodyShape)
  51. // bodyShape.updateBoundingSphereRadius(size / 2)
  52. // body.addShape(bodyShape)
  53. // bodyShape.updateConvexPolyhedronRepresentation();
  54. // bodyShape.computeBoundingSphereRadius();
  55. // body.computeAABB();
  56. // body.updateMassProperties();
  57. // }, 0)
  58. // body.body.shapes[0].boundingSphereRadius = size / 2
  59. // body.body.mass = size
  60. }
  61. }
  62. }, error => RE.Debug.logError(error))
  63. //random asteroids
  64. // let field = RE.App.currentScene.getObjectByName("asteroidField")
  65. // this.asteroids = []
  66. // for(let i = 0; i < 300; i++) {
  67. // let asteroid = this.asteroid.instantiate(field)
  68. // asteroid.position.copy(new THREE.Vector3(Math.floor(500 * Math.random() - 250) ,1 , Math.floor(500 * Math.random() - 250)))
  69. // }
  70. // let field = RE.App.currentScene.getObjectByName("asteroidField")
  71. // let objectData = levelData
  72. // for(let i = 0; i < objectData.length; i++) {
  73. // let obj = objectData[i]
  74. // if(obj[0] == "asteroid") {
  75. // let asteroid = this.asteroid.instantiate(field)
  76. // asteroid.position.copy(new THREE.Vector3(obj[1] ,1 , obj[2]))
  77. // }
  78. // }
  79. // this.player.instantiate()
  80. }
  81. onResize(event) {
  82. }
  83. formatComponentName(componentId) {
  84. return `setup` + componentId.split('-').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join('')
  85. }
  86. setupTopMiddle(element) {
  87. this.armorInfo = document.createElement('div')
  88. this.armorInfo.className = "header-info"
  89. this.armorInfo.innerHTML = "Armor: 50"
  90. element.appendChild(this.armorInfo)
  91. this.shieldsInfo = document.createElement('div')
  92. this.shieldsInfo.className = "header-info"
  93. this.shieldsInfo.innerHTML = "Shields: 100"
  94. element.appendChild(this.shieldsInfo)
  95. this.moneyInfo = document.createElement('div')
  96. this.moneyInfo.className = "header-info"
  97. this.moneyInfo.innerHTML = "₩: 140,000"
  98. element.appendChild(this.moneyInfo)
  99. }
  100. setupBottomLeft(element) {
  101. let info = document.createElement('div')
  102. info.innerHTML = "Mission: Retrieve the lost crystal alloy from the nearby asteroid field"
  103. element.appendChild(info)
  104. }
  105. setupBottomRight(element) {
  106. let minimap = document.createElement('canvas')
  107. minimap.width = Math.max(element.offsetWidth, 160)
  108. minimap.height = Math.max(element.offsetHeight, 160)
  109. let ctx = minimap.getContext('2d')
  110. ctx.strokeStyle = `rgba(40, 240, 40, 0.8)`
  111. ctx.beginPath()
  112. ctx.arc(minimap.width / 2, minimap.height / 2, Math.min(minimap.width / 2, minimap.height / 2), 0, 2 * Math.PI)
  113. ctx.stroke()
  114. ctx.beginPath()
  115. ctx.arc(minimap.width / 2, minimap.height / 2, Math.min(3 *minimap.width / 8, 3 * minimap.height / 8), 0, 2 * Math.PI)
  116. ctx.stroke()
  117. ctx.beginPath()
  118. ctx.arc(minimap.width / 2, minimap.height / 2, Math.min(1 *minimap.width / 4, 1 * minimap.height / 4), 0, 2 * Math.PI)
  119. ctx.stroke()
  120. ctx.beginPath()
  121. ctx.arc(minimap.width / 2, minimap.height / 2, Math.min(1 * minimap.width / 8, 1 * minimap.height / 8), 0, 2 * Math.PI)
  122. ctx.stroke()
  123. ctx.beginPath()
  124. ctx.moveTo(minimap.width / 2, minimap.height / 2)
  125. ctx.lineTo(2 * Math.min(minimap.width / 2, minimap.height / 2), minimap.height / 2)
  126. ctx.stroke()
  127. element.appendChild(minimap)
  128. }
  129. }
  130. RE.registerComponent(Game);