Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let Application = PIXI.Application,
  2.     Container = PIXI.Container,
  3.     loader = PIXI.loader,
  4.     resources = PIXI.loader.resources,
  5.     Sprite = PIXI.Sprite,
  6.     TextureCache = PIXI.utils.TextureCache,
  7.     Rectangle = PIXI.Rectangle,
  8.     Graphics = PIXI.Graphics,
  9.     Text = PIXI.Text,
  10.     TextStyle = PIXI.TextStyle;
  11.  
  12. //Create a Pixi Application
  13. let app = new Application({
  14.     width: 512,
  15.     height: 512,
  16.     antialias: true,
  17.     transparent: false,
  18.     resolution: 1
  19.     });
  20.  
  21.  
  22. //Add the canvas that Pixi automaticall created for you to the HTML Document
  23. document.body.appendChild(app.view);
  24. app.renderer.backgroundColor = 0x45b5f6;
  25. //app.renderer.view.style.position = "absolute";
  26. //app.renderer.view.style.display = "block";
  27. //app.renderer.autoResize = true;
  28. //app.renderer.resize(window.innerWidth, window.innerHeight);
  29.  
  30. //load the sprite as a texture
  31. loader
  32.     .add([
  33.     "images/treasureHunter.json"
  34.     ])
  35.     .load(setup);
  36.  
  37. //define keyboard listener
  38. function keyboard(keyCode) {
  39.     let key = {};
  40.     key.code = keyCode;
  41.     key.isDown = false;
  42.     key.isUp = true;
  43.     key.press = undefined;
  44.     key.release = undefined;
  45.    
  46.     //downhandler
  47.     key.downHandler = event => {
  48.         if (event.keyCode === key.code) {
  49.             if (key.isUp && key.press) key.press();
  50.             key.isDown = true;
  51.             key.isUp = false;
  52.         }
  53.         event.preventDefault();
  54.     };
  55.    
  56.     //uphandler
  57.     key.upHandler = event => {
  58.         if (event.keyCode === key.code) {
  59.             if (key.isDown && key.release) key.release();
  60.             key.isDown = false;
  61.             key.isUp = true;
  62.         }
  63.         event.preventDefault();
  64.     };
  65.    
  66.     //attach event listeners
  67.     window.addEventListener(
  68.     "keydown", key.downHandler.bind(key), false
  69.     );
  70.    
  71.     window.addEventListener(
  72.     "keyup", key.upHandler.bind(key), false
  73.     );
  74.     return key;
  75. }
  76.  
  77. let dungeon, explorer, treasure, door, id, state;
  78.  
  79. function setup() {
  80.     //Treasurehunter
  81.     id = resources["images/treasureHunter.json"].textures;
  82.    
  83.     //create the gamescene container
  84.     gameScene = new Container();
  85.     app.stage.addChild(gameScene);
  86.    
  87.     //create the gameOverScene for win screen
  88.     gameOverScene = new Container();
  89.     app.stage.addChild(gameOverScene);
  90.    
  91.     gameOverScene.visible = false;
  92.    
  93.     gameWinBg = new Graphics();
  94.     gameWinBg.beginFill(0x000000);
  95.     gameWinBg.drawRect(0, 0, 512, 512);
  96.     gameWinBg.endFill();
  97.     gameOverScene.addChild(gameWinBg);
  98.    
  99.     //winning screnne style
  100.     let winStyle = new TextStyle({
  101.         fontFamily: "Montserrat",
  102.         fontSize: 64,
  103.         fill: "white",
  104.     });
  105.    
  106.     message = new Text("The End!", winStyle);
  107.     message.anchor.set(0.5, 0.5);
  108.     message.position.set(512/2, 512/2);
  109.     gameOverScene.addChild(message);
  110.    
  111.     explorer = new Sprite(id["explorer.png"]);
  112.     dungeon = new Sprite(id["dungeon.png"]);
  113.     treasure = new Sprite(id["treasure.png"]);
  114.     door = new Sprite(id["door.png"]);
  115.     gameScene.addChild(dungeon, explorer, treasure, door);
  116.    
  117.     //Create the health bar
  118.     healthBar = new PIXI.DisplayObjectContainer();
  119.     healthBar.position.set(app.stage.width - 170, 4);
  120.     gameScene.addChild(healthBar);
  121.    
  122.     //black part of healthbar
  123.     let innerBar = new Graphics();
  124.     innerBar.beginFill(0x000000);
  125.     innerBar.drawRect(0, 0, 128, 8);
  126.     innerBar.endFill();
  127.     healthBar.addChild(innerBar);
  128.    
  129.     //create the red healthbar
  130.     let outerBar = new Graphics();
  131.     outerBar.beginFill(0xFF3300);
  132.     outerBar.drawRect(0, 0, 128, 8);
  133.     outerBar.endFill();
  134.     healthBar.addChild(outerBar);
  135.    
  136.     healthBar.outer = outerBar;
  137.    
  138.     explorer.position.set(68, app.stage.height / 2 - explorer.height / 2);
  139.     treasure.position.set(app.stage.width - treasure.width - 48, app.stage.height / 2 - treasure.height / 2);
  140.     door.position.set(32,0);
  141.    
  142.     //add movement
  143.     explorer.vx = 0;
  144.     explorer.vy = 0;
  145.    
  146.     let numberOfBlobs = 6,
  147.         spacing = 48,
  148.         xOffset = 150,
  149.         speed = 2,
  150.         direction = 1;
  151.    
  152.     //array to store the blobs
  153.     blobs = [];
  154.    
  155.     //make as many blobs as the number of 'numberOfBlobs'
  156.     for (let i = 0; i < numberOfBlobs; i++) {
  157.        
  158.         //make a blob
  159.         let blob = new Sprite(id["blob.png"]);
  160.        
  161.         //space the blobs
  162.         let x = spacing * i + xOffset;
  163.        
  164.         //random y position
  165.         let y = randomInt(0, app.stage.height - blob.height);
  166.        
  167.         blob.x = x;
  168.         blob.y = y;
  169.        
  170.         blob.vy = speed * direction;
  171.        
  172.         direction *= -1;
  173.        
  174.         blobs.push(blob);
  175.        
  176.         gameScene.addChild(blob);
  177.     }
  178.    
  179.    
  180.    
  181.     state = play;
  182.     app.ticker.add(delta => gameLoop(delta));
  183.  
  184. //    //draw rounded rectangle
  185. //    let roundBox = new Graphics();
  186. //    roundBox.lineStyle(4, 0x1133ff, 1);
  187. //    roundBox.beginFill(0x66ccff);
  188. //    roundBox.drawRoundedRect(0,0,350,120, 5);
  189. //    roundBox.endFill();
  190. //    roundBox.position.set(app.stage.width / 2, app.stage.height / 2);
  191. //    roundBox.pivot.set(175, 60);
  192. //    app.stage.addChild(roundBox);
  193. //    console.log(roundBox.drawRoundedRect.width);
  194. //    
  195. //    //text style
  196. //    let style = new TextStyle({
  197. //        fontFamily: "VT323",
  198. //        fontSize: 32,
  199. //        fill: "white",
  200. //        stroke: "#ff3300",
  201. //        strokeThickness: 4,
  202. //        dropShadow: true,
  203. //        dropShadowColor: "#000000",
  204. //        dropShadowBlur: 4,
  205. //        dropShadowAngle: Math.PI / 6,
  206. //        dropShadowDistance: 6,
  207. //        align: "center",
  208. //    });
  209. //    
  210. //    //message
  211. //    let message = new Text("You're dead!", style);
  212. //    message.position.set(app.stage.width/2, app.stage.height/2);
  213. //    message.anchor.set(0.5, 0.5);
  214. //    app.stage.addChild(message);
  215. //    message.text = "Hocus Pocus!";
  216.    
  217.    
  218. //end setup
  219. }
  220.  
  221. function gameLoop(delta) {
  222.     state(delta);
  223. }
  224.  
  225. function play(delta) {
  226.    
  227.     //boundaries of movement
  228.     function contain(sprite, container) {
  229.         let collision = undefined;
  230.        
  231.         //left
  232.         if (sprite.x < container.x) {
  233.             sprite.x = container.x;
  234.             collision = "left";
  235.         }
  236.        
  237.         //top
  238.         if (sprite.y < container.y) {
  239.             sprite.y = container.y;
  240.             collision = "top";
  241.         }
  242.        
  243.         //right
  244.         if (sprite.x + sprite.width > container.width) {
  245.             sprite.x = container.width - sprite.width;
  246.             collision = "right";
  247.         }
  248.        
  249.         //bottom
  250.         if (sprite.y + sprite.height > container.height) {
  251.             sprite.y = container.height - sprite.height;
  252.             collision = "bottom";
  253.         }
  254.        
  255.         //return collision value
  256.         return collision;
  257.     }
  258.    
  259.     contain(explorer, {x: 28, y: 10, width:488, height: 480});
  260.    
  261.     explorer.x += explorer.vx;
  262.     explorer.y += explorer.vy;
  263.    
  264.     blobs.forEach(function(blob){
  265.         //move the blob
  266.         blob.y += blob.vy;
  267.        
  268.         let blobHitWall = contain(blob, {x: 28, y:10, width: 488, height:480});
  269.        
  270.         if (blobHitWall === "top" || blobHitWall === "bottom") {
  271.             blob.vy *= -1;
  272.         }
  273.        
  274.         let explorerHit = false;
  275.        
  276.         if(hitTestRectangle(explorer, blob)){
  277.             explorerHit = true;
  278.         }
  279.        
  280.         if(explorerHit) {
  281.             explorer.alpha = 0.5;
  282.             healthBar.outer.width -= 1;
  283.            
  284.         } else {
  285.             explorer.alpha = 1;
  286.         }
  287.        
  288.         if (hitTestRectangle(explorer, treasure)) {
  289.             treasure.x = explorer.x + 8;
  290.             treasure.y = explorer.y + 8;
  291.         }
  292.        
  293.         if (hitTestRectangle(treasure, door)) {
  294.             state = end;
  295.             message.text = "You won!";
  296.         }
  297.        
  298.         if (healthBar.outer.width < 0) {
  299.             state = end;
  300.             message.text = "You lost!";
  301.         }
  302.        
  303.     });
  304. }
  305. //play end
  306.  
  307. function end() {
  308.     gameScene.visible = false;
  309.     gameOverScene.visible = true;
  310. }
  311.  
  312. //keyboard
  313. let left = keyboard(37),
  314.         up = keyboard(38),
  315.         right = keyboard(39),
  316.         down = keyboard(40);
  317.    
  318.     //left
  319.     left.press = () => {
  320.         explorer.vx = -5;
  321.         explorer.vy = 0;
  322.     };
  323.    
  324.     left.release = () => {
  325.         if (!right.isDown && explorer.vy === 0) {
  326.             explorer.vx = 0;
  327.         }
  328.     };
  329.    
  330.     //up
  331.     up.press = () => {
  332.         explorer.vy = -5;
  333.         explorer.vx = 0;
  334.     };
  335.    
  336.     up.release = () => {
  337.         if (!down.isDown && explorer.vx === 0) {
  338.             explorer.vy = 0;
  339.         }
  340.     };
  341.    
  342.     //right
  343.     right.press = () => {
  344.         explorer.vy = 0;
  345.         explorer.vx = 5;
  346.     };
  347.    
  348.     right.release = () => {
  349.         if (!left.isDown && explorer.vy === 0) {
  350.             explorer.vx = 0;
  351.         }
  352.     };
  353.    
  354.     //down
  355.     down.press = () => {
  356.         explorer.vy = 5;
  357.         explorer.vx = 0;
  358.     };
  359.    
  360.     down.release = () => {
  361.         if (!up.isDown && explorer.vx === 0) {
  362.             explorer.vy = 0;
  363.         }
  364.     };
  365. //keyboard end
  366.  
  367. //hit rectangle
  368.     function hitTestRectangle(r1, r2) {
  369.        
  370.         let hit, combinedHalfWidths, combinedHalfHeights, vx, vy;
  371.        
  372.         hit = false;
  373.        
  374.         r1.centerX = r1.x + r1.width / 2;
  375.         r1.centerY = r1.y + r1.height / 2;
  376.         r2.centerX = r2.x + r2.width / 2;
  377.         r2.centerY = r2.y + r2.height / 2;
  378.        
  379.         r1.halfWidth = r1.width / 2;
  380.         r1.halfHeight = r1.height / 2;
  381.         r2.halfWidth = r2.width / 2;
  382.         r2.halfHeight = r2.height / 2;
  383.        
  384.         vx = r1.centerX - r2.centerX;
  385.         vy = r1.centerY - r2.centerY;
  386.        
  387.         combinedHalfWidths = r1.halfWidth + r2.halfWidth;
  388.         combinedHalfHeights = r1.halfHeight + r2.halfHeight;
  389.        
  390.         if (Math.abs(vx) < combinedHalfWidths) {
  391.             if (Math.abs(vy) < combinedHalfHeights) {
  392.                 hit = true;
  393.             } else {
  394.                 hit = false;
  395.             }
  396.            
  397.         } else {
  398.             hit = false;
  399.            
  400.         }
  401.        
  402.         return hit;
  403.        
  404.     }
  405.     //hit rectangle end
  406.  
  407. function randomInt(min, max) {
  408.     return Math.floor(Math.random() * (max - min + 1)) + min;
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement