main.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //Phaser is a "Game Engine" that is primarily used to create 2d games on the web browser and is one of the mose used
  2. //In my opinion, the documentation is just bad and feels all over the place
  3. //When I'm looking to create a physics object and land on the doc of "Phaser.Physics.Arcade.Factory" to create a physics objects
  4. //and try to use this in my code, it wont work since Im supposed to use "this.physics.add" but it doesnt get explicitly explained in that doc
  5. //Basically, it doesnt tell you at all, how to use the code they documentated
  6. //The tutorial on the website also doesnt even explain this properly and like also other tutorials on youtube, it is always like "this code does that"
  7. //but I dont find one that explains how phaser actually works in the background and how to use their documentations properly
  8. //If i compare this to Unity, the documentation here is by far worse because its not "straight-forward" but spaghetti
  9. //Even Unreal Engine is easier to understand despite being more complex by far.
  10. //a phaser scene is similar to scenes in unity and levels in unreal engine
  11. //Everything outside this scene can be used as an "Game instance" since those things persist outside of the different scenes
  12. class MyGame extends Phaser.Scene {
  13. //this function is used to load assets into your game
  14. constructor(){
  15. super("scene-game");
  16. this.cursor;
  17. this.playerSpeed = 300;
  18. this.playerStart = {x: 0, y: 0};
  19. this.playerJumpVelocity = -500;
  20. this.goalX = 5000;
  21. this.platforms;
  22. this.background;
  23. this.camera;
  24. this.playerText;
  25. this.spikes;
  26. this.moveLeft = false;
  27. this.moveRight = false;
  28. this.jumpPressed = false;
  29. this.test = Phaser.Physics.Arcade.World;
  30. this.buttonY = 400;
  31. this.leftBtnX = -400;
  32. this.leftBtn;
  33. this.rightBtnX = -280;
  34. this.rightBtn;
  35. this.jumpBtnX = 700;
  36. this.jumpBtn;
  37. }
  38. preload() {
  39. this.load.setBaseURL("./images");
  40. this.load.image("blackSquare", "blackSquare.png");
  41. this.load.image("redSquare", "redSquare.png");
  42. //this.load.image("backgroundSky", "backgroundSky.jpg"); //https://www.vecteezy.com/vector-art/6045799-blue-sky-with-clouds-seamless-pattern
  43. }
  44. create() {
  45. this.physics.world.setBounds(0, 0, 10000, 600); //https://newdocs.phaser.io/docs/3.55.2/Phaser.Physics.Arcade.World#setBounds
  46. //this.background = this.add.tileSprite(0, 0, this.scale.width, this.scale.height, "backgroundSky");
  47. //#region Player settings
  48. //#region Input
  49. this.input.addPointer(2);
  50. // Track button states
  51. // LEFT BUTTON
  52. this.leftBtn = this.add.rectangle(this.leftBtnX, this.buttonY, 100, 100, 0x0000ff, 1)
  53. .setInteractive();
  54. this.leftBtnPlayerDistanceX = this.playerStart.x - this.leftBtnX;
  55. this.leftBtn.on("pointerdown", () => {
  56. this.moveLeft = true;
  57. });
  58. this.leftBtn.on("pointerup", () => {
  59. this.moveLeft = false;
  60. });
  61. this.leftBtn.on("pointerout", () => {
  62. this.moveLeft = false;
  63. });
  64. // RIGHT BUTTON
  65. this.rightBtn = this.add.rectangle(this.rightBtnX, this.buttonY, 100, 100, 0x00ff00, 0.5)
  66. .setInteractive();
  67. this.rightBtnPlayerDistanceX = this.playerStart.x - this.rightBtnX;
  68. this.rightBtn.on("pointerdown", () => {
  69. this.moveRight = true;
  70. });
  71. this.rightBtn.on("pointerup", () => {
  72. this.moveRight = false;
  73. });
  74. this.rightBtn.on("pointerout", () => {
  75. this.moveRight = false;
  76. });
  77. // JUMP BUTTON
  78. this.jumpBtn = this.add.rectangle(this.jumpBtnX, this.buttonY, 100, 100, 0xff0000, 0.5)
  79. .setInteractive();
  80. this.jumpBtnPlayerDistanceX = this.playerStart.x - this.jumpBtnX;
  81. this.jumpBtn.on("pointerdown", () => {
  82. this.jumpPressed = true;
  83. });
  84. this.jumpBtn.on("pointerup", () => {
  85. this.jumpPressed = false;
  86. });
  87. this.jumpBtn.on("pointerout", () => {
  88. this.jumpPressed = false;
  89. });
  90. //#endregion
  91. this.player = this.physics.add.image(this.playerStart.x, this.playerStart.y,"redSquare").setOrigin(0,0); //https://newdocs.phaser.io/docs/3.55.2/Phaser.Physics.Arcade.Factory#image
  92. this.player.setBounce(0); //https://newdocs.phaser.io/docs/3.55.2/Phaser.Physics.Arcade.Body#setBounce
  93. this.player.setCollideWorldBounds(false); //https://newdocs.phaser.io/docs/3.55.2/Phaser.Physics.Arcade.Body#setCollideWorldBounds
  94. //#endregion
  95. //#region Platform settings
  96. this.platforms = this.physics.add.staticGroup();
  97. // Add a rectangle to the static group
  98. var rect = [];
  99. rect.push(this.add.rectangle(300, 300, 1000, 50, 0xff0000)); // pos x, pos y, width, height, color hex
  100. rect.push(this.add.rectangle(1300, 600, 100, 50, 0xff0000));
  101. rect.push(this.add.rectangle(1500, 400, 100, 50, 0xff0000));
  102. rect.push(this.add.rectangle(2000, 200, 100, 50, 0xff0000));
  103. rect.push(this.add.rectangle(2600, 300, 100, 50, 0xff0000));
  104. rect.push(this.add.rectangle(3000, 200, 100, 50, 0xff0000));
  105. rect.push(this.add.rectangle(3820, 550, 100, 50, 0xff0000));
  106. rect.push(this.add.rectangle(4350, 590, 500, 50, 0xff0000));
  107. for (let i = 0; i < rect.length; i++){
  108. this.physics.add.existing(rect[i], true); //https://newdocs.phaser.io/docs/3.55.2/Phaser.Physics.Arcade.Factory#existing
  109. this.platforms.add(rect[i]);
  110. }
  111. this.physics.add.collider(this.player, this.platforms);
  112. //#endregion
  113. //#region spikes
  114. this.spikes = this.physics.add.staticGroup(); //https://newdocs.phaser.io/docs/3.55.2/Phaser.Physics.Arcade.Factory#staticGroup
  115. var spikes = [];
  116. spikes.push(this.add.rectangle(3100, 190, 50, 50, 0x0000ff));
  117. for (let i = 0; i < spikes.length; i++){
  118. this.physics.add.existing(spikes[i], true); // 'true' makes it a static body
  119. this.spikes.add(spikes[i]);
  120. }
  121. this.physics.add.collider(this.player, this.spikes, this.onPlayerSpikeCollision, null, this); //https://newdocs.phaser.io/docs/3.55.2/Phaser.Physics.Arcade.Factory#collider
  122. //#endregion
  123. //#region Level End
  124. //#endregion
  125. //#region Camera
  126. //(follow Target, round pixel to int, lerpX, lerpY, offsetX, offsetY) //https://newdocs.phaser.io/docs/3.60.0/focus/Phaser.Cameras.Scene2D.Camera-startFollow
  127. this.cameras.main.startFollow(this.player, false, 1, 0, 0, -100);
  128. this.cameras.main.backgroundColor= "black";
  129. this.cameras.main.setViewport(-config.width / 2, -config.height/2, config.width*2, config.height*2);
  130. this.cameras.main.zoom = 0.5;
  131. //#endregion
  132. //#region HUD
  133. //this.playerText = this.add.text(10, 10, 'Position: (0, 0)', { fontSize: '16px', fill: '#00FF00' });
  134. //#endregion
  135. this.cursor = this.input.keyboard.createCursorKeys();
  136. }
  137. update(){
  138. let onGround = this.player.body.blocked.down;
  139. //#region Input Manager
  140. const {left, right, space} = this.cursor;
  141. if (left.isDown || this.moveLeft){
  142. console.log("left press");
  143. this.player.setVelocityX(-this.playerSpeed);
  144. } else if (right.isDown || this.moveRight){
  145. console.log("right press");
  146. this.player.setVelocityX(this.playerSpeed);
  147. } else {
  148. this.player.setVelocityX(0);
  149. }
  150. if ((space.isDown || this.jumpPressed) && onGround){
  151. this.player.setVelocityY(this.playerJumpVelocity);
  152. }
  153. //#endregion
  154. //#region Update HUD
  155. this.leftBtn.setPosition(this.player.x - this.leftBtnPlayerDistanceX, this.buttonY);
  156. this.rightBtn.setPosition(this.player.x - this.rightBtnPlayerDistanceX, this.buttonY);
  157. this.jumpBtn.setPosition(this.player.x - this.jumpBtnPlayerDistanceX, this.buttonY);
  158. //#endregion
  159. if (this.player.y > this.physics.world.bounds.height){
  160. this.scene.restart();
  161. alert("You lost.");
  162. } else if (this.player.x > this.goalX){
  163. this.scene.restart()
  164. alert("You won!");
  165. }
  166. }
  167. onPlayerSpikeCollision(player, spikes){
  168. this.scene.restart()
  169. alert("You lost.");
  170. }
  171. }
  172. const config = {
  173. type: Phaser.AUTO,
  174. width: window.innerWidth - 10,
  175. height: window.innerHeight- 10,
  176. backgroundColor: "#ffffff", //sets the background color of the scene
  177. scene: MyGame,
  178. physics: {
  179. default: 'arcade',
  180. arcade: {
  181. gravity: { y: 500 }
  182. }
  183. }
  184. };
  185. console.log(config);
  186. const game = new Phaser.Game(config);