Guest User

TheMuffinCoder

a guest
Jul 3rd, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2. /**
  3.  * The Last Pastry
  4.  * @author TheMuffinCoder (a.k.a Sebastian)
  5.  */
  6. module PastryCore {
  7.    
  8.     export class Engine {
  9.  
  10.         static log(message: string): void {
  11.  
  12.             console.log("PASTRY ENGINE: " + message);
  13.  
  14.         }
  15.  
  16.         static calculateAngleSimple(pointOne: Position, pointTwo: Position): number {
  17.  
  18.             var deltaX: number = pointTwo.x - pointOne.x;
  19.             var deltaY: number = pointTwo.y - pointOne.y;
  20.  
  21.             return (Math.atan2(deltaY, deltaX) * 180 / Math.PI);
  22.  
  23.         }
  24.  
  25.         static calculateAngle(pointOne: Position, pointTwoX: number, pointTwoY: number): number {
  26.  
  27.             var deltaX: number = pointTwoX - pointOne.x;
  28.             var deltaY: number = pointTwoY - pointOne.y;
  29.  
  30.             return (Math.atan2(deltaY, deltaX) * 180 / Math.PI);
  31.  
  32.         }
  33.     }
  34.  
  35.     export class Dimension {
  36.  
  37.         constructor(public width: number = 0, public height: number = 0) { }
  38.  
  39.         setDimensions(width: number, height: number): void {
  40.  
  41.             this.width = width;
  42.             this.height = height;
  43.  
  44.         }
  45.  
  46.     }
  47.  
  48.     export class Position {
  49.  
  50.         constructor(public x: number = 0, public y: number = 0) { }
  51.  
  52.         updateY(delta: number): void {
  53.  
  54.             this.y += delta;
  55.  
  56.         }
  57.  
  58.         updateX(delta: number): void {
  59.  
  60.             this.x += delta;
  61.  
  62.         }
  63.  
  64.         setX(x: number): void {
  65.  
  66.             this.x = x;
  67.  
  68.         }
  69.  
  70.         setY(y: number): void {
  71.  
  72.             this.y = y;
  73.  
  74.         }
  75.  
  76.     }
  77.  
  78.     export class Bounds {
  79.  
  80.         public min: Position = new Position();
  81.         public max: Position = new Position();
  82.  
  83.         public center: Position = new Position();
  84.         public tileLocation: Position = new Position();
  85.  
  86.         public update(): void {
  87.  
  88.             this.center.x = this.min.x + 8;
  89.  
  90.         }
  91.              
  92.     }
  93.  
  94.     export enum GameStates { INGAME, MAIN, PAUSED, LOADING}
  95.  
  96.     export class GameManager {
  97.  
  98.         constructor(public currentState: GameStates = GameStates.INGAME) {
  99.  
  100.         }
  101.  
  102.     }
  103.  
  104.     export class TimeManager {
  105.  
  106.         static DELAY_SHORT: number = 25;
  107.         static DELAY_MEDIUM: number = 50;
  108.         static DELAY_LONG: number = 100;
  109.         static lapse: number = 0.0;
  110.         static fps: number = 0.0;
  111.  
  112.         private start: number = 0.0;
  113.         private before: number = 0.0;
  114.  
  115.         public fps: number = 0.0;
  116.  
  117.         constructor() {
  118.  
  119.             window.requestAnimationFrame = (
  120.                
  121.                 window.requestAnimationFrame ||
  122.                 window.webkitRequestAnimationFrame ||
  123.                 window.mozRequestAnimationFrame ||
  124.                 window.oRequestAnimationFrame ||
  125.                 window.msRequestAnimationFrame || function (callback, element) {
  126.  
  127.                     window.setTimeout(PASTRY.loop, 60);
  128.                 }
  129.             );
  130.  
  131.             this.before = Date.now();
  132.  
  133.         }
  134.  
  135.         update(): void {
  136.  
  137.             this.start = Date.now();
  138.             TimeManager.lapse = (this.start - this.before) / 1000;
  139.             this.before = this.start;
  140.  
  141.             TimeManager.fps = (1 / TimeManager.lapse) | 0;
  142.  
  143.         }
  144.  
  145.        
  146.  
  147.     }
  148.  
  149.     export class EventSet {
  150.  
  151.         available: boolean = false;
  152.  
  153.         moveLeft: boolean = false;
  154.         moveRight: boolean = false;
  155.         moveUp: boolean = false;
  156.         moveDown: boolean = false;
  157.  
  158.         action: boolean = false;
  159.         actionDirection: number = 0;
  160.  
  161.         deactivateAllMovement(): void {
  162.  
  163.             this.moveLeft = this.moveDown = this.moveRight = this.moveUp = false;
  164.  
  165.         }
  166.  
  167.         deactivateAll(): void {
  168.  
  169.             this.moveLeft = this.moveDown = this.moveRight = this.moveUp = this.action = false;
  170.  
  171.         }
  172.     }
  173.  
  174.  
  175.     export enum KEYBOARD {
  176.        
  177.         ENTER   = 13,
  178.         SHIFT   = 16,
  179.         CTRL    = 17,
  180.         ESAPE   = 27,
  181.         SPACE   = 32,
  182.         LEFT    = 37,
  183.         UP      = 38,
  184.         RIGHT   = 39,
  185.         DOWN    = 40,
  186.  
  187.         A   = 65,
  188.         D   = 68,
  189.         E   = 69,
  190.         Q   = 81,
  191.         R   = 82,
  192.         S   = 83,
  193.         T   = 84,
  194.         W   = 87,
  195.         Y   = 89,
  196.  
  197.     }
  198.  
  199.     export enum GAMEPAD_XBOX {
  200.  
  201.         BUTTON_A = 0,
  202.         BUTTON_B = 1,
  203.         BUTTON_X = 2,
  204.         BUTTON_Y = 3,
  205.  
  206.         BUTTON_LEFT_TRIGGER = 4,
  207.         BUTTON_RIGHT_TRIGGER = 5,
  208.  
  209.         BUTTON_LEFT_BUMPER = 6,
  210.         BUTTON_RIGHT_BUMPER = 7,
  211.  
  212.         BUTTON_VIEW = 8,
  213.         BUTTON_MENU = 9,
  214.  
  215.         BUTTON_LEFT_STICK_PRESS = 10,
  216.  
  217.         BUTTON_RIGHT_STICK_PRESS = 11,
  218.  
  219.         BUTTON_DPAD_UP = 12,
  220.         BUTTON_DPAD_DOWN = 13,
  221.         BUTTON_DPAD_LEFT = 14,
  222.         BUTTON_DPAD_RIGHT = 15,
  223.  
  224.         STICK_LEFT_HORIZONTAL = 0,
  225.         STICK_LEFT_VERTICAL = 1,
  226.  
  227.         STICK_RIGHT_HORIZONTAL = 2,
  228.         STICK_RIGHT_VERTICAL = 3,
  229.  
  230.     }
  231.  
  232.     export class InputManager {
  233.  
  234.         static keyBoardEvents: EventSet = new EventSet();
  235.  
  236.         static touchEvents: EventSet = new EventSet();
  237.  
  238.         static touchCenter: Position = new Position();
  239.         static touchBounds: Position = new Position();
  240.         static touchIcon: any;
  241.  
  242.         private GAMEPAD: any;
  243.        
  244.  
  245.         GAMEPAD_ONE_INDEX: number = 0;
  246.         GAMEPAD_TWO_INDEX: number = 1;
  247.         GAMEPAD_THREE_INDEX: number = 2;
  248.         GAMEPAD_FOUR_INDEX: number = 3;
  249.         GamepadSupported: boolean = false;
  250.  
  251.         static GAMEPADS: Array<EventSet> = [new EventSet(), new EventSet(), new EventSet(), new EventSet()];
  252.  
  253.         updateEvents(): void {
  254.         }
  255.  
  256.         readKeyboard(e): void {
  257.         }
  258.  
  259.         readKeyboardButtonPressed(e): void {
  260.  
  261.             switch (e.keyCode) {
  262.  
  263.                 case KEYBOARD.W:
  264.                 case KEYBOARD.UP:
  265.  
  266.                     InputManager.keyBoardEvents.moveUp = true;
  267.                     break;
  268.  
  269.                 case KEYBOARD.S:
  270.                 case KEYBOARD.DOWN:
  271.  
  272.                     InputManager.keyBoardEvents.moveDown = true;
  273.                     break;
  274.  
  275.                 case KEYBOARD.A:
  276.                 case KEYBOARD.LEFT:
  277.  
  278.                     InputManager.keyBoardEvents.moveLeft = true;
  279.                     break;
  280.  
  281.                 case KEYBOARD.D:
  282.                 case KEYBOARD.RIGHT:
  283.  
  284.                     InputManager.keyBoardEvents.moveRight = true;
  285.                     break;
  286.  
  287.                 case KEYBOARD.SPACE:
  288.                 case KEYBOARD.ENTER:
  289.                     InputManager.keyBoardEvents.action = true;
  290.                     break;
  291.  
  292.                 default:
  293.                     console.log(e.keyCode);
  294.             };
  295.  
  296.         }
  297.  
  298.         readKeyboardButtonLifted(e): void {
  299.  
  300.             switch (e.keyCode) {
  301.  
  302.                 case KEYBOARD.W:
  303.                 case KEYBOARD.UP:
  304.  
  305.                     InputManager.keyBoardEvents.moveUp = false;
  306.                     break;
  307.  
  308.                 case KEYBOARD.S:
  309.                 case KEYBOARD.DOWN:
  310.  
  311.                     InputManager.keyBoardEvents.moveDown = false;
  312.                     break;
  313.  
  314.                 case KEYBOARD.A:
  315.                 case KEYBOARD.LEFT:
  316.  
  317.                     InputManager.keyBoardEvents.moveLeft = false;
  318.                     break;
  319.  
  320.                 case KEYBOARD.D:
  321.                 case KEYBOARD.RIGHT:
  322.  
  323.                     InputManager.keyBoardEvents.moveRight = false;
  324.                     break;
  325.  
  326.                 case KEYBOARD.SPACE:
  327.                 case KEYBOARD.ENTER:
  328.  
  329.                     InputManager.keyBoardEvents.action = false;
  330.                     break;
  331.  
  332.                 default:
  333.                     console.log(e.keyCode);
  334.             }
  335.         }
  336.  
  337.         static checkTouchEvent(touchAngle: number): void {
  338.  
  339.             if (touchAngle < 0) {//Top
  340.  
  341.                 if (touchAngle < -157.5) {//Only left
  342.  
  343.                     InputManager.touchEvents.moveLeft = true;
  344.                     InputManager.touchEvents.moveRight = false;
  345.                     InputManager.touchEvents.moveDown = false;
  346.                     InputManager.touchEvents.moveUp = false;
  347.  
  348.                 } else if (touchAngle < -112.5) {//Left Up
  349.  
  350.                     InputManager.touchEvents.moveLeft = true;
  351.                     InputManager.touchEvents.moveRight = false;
  352.                     InputManager.touchEvents.moveDown = false;
  353.                     InputManager.touchEvents.moveUp = true;
  354.  
  355.                 } else if (touchAngle < -67.5) {//Up only
  356.  
  357.                     InputManager.touchEvents.moveLeft = false;
  358.                     InputManager.touchEvents.moveRight = false;
  359.                     InputManager.touchEvents.moveDown = false;
  360.                     InputManager.touchEvents.moveUp = true;
  361.  
  362.                 } else if (touchAngle < -22.5) {//Right up
  363.  
  364.                     InputManager.touchEvents.moveLeft = false;
  365.                     InputManager.touchEvents.moveRight = true;
  366.                     InputManager.touchEvents.moveDown = false;
  367.                     InputManager.touchEvents.moveUp = true;
  368.  
  369.                 } else {//Right
  370.  
  371.                     InputManager.touchEvents.moveLeft = false;
  372.                     InputManager.touchEvents.moveRight = true;
  373.                     InputManager.touchEvents.moveDown = false;
  374.                     InputManager.touchEvents.moveUp = false;
  375.  
  376.                 }
  377.  
  378.             } else if (touchAngle > 0) {//Bottom
  379.  
  380.                 if (touchAngle > 157.5) {//Only left
  381.  
  382.                     InputManager.touchEvents.moveLeft = true;
  383.                     InputManager.touchEvents.moveRight = false;
  384.                     InputManager.touchEvents.moveDown = false;
  385.                     InputManager.touchEvents.moveUp = false;
  386.  
  387.                 } else if (touchAngle > 112.5) {//Left Down
  388.  
  389.                     InputManager.touchEvents.moveLeft = true;
  390.                     InputManager.touchEvents.moveRight = false;
  391.                     InputManager.touchEvents.moveDown = true;
  392.                     InputManager.touchEvents.moveUp = false;
  393.  
  394.                 } else if (touchAngle > 67.5) {//Down only
  395.  
  396.                     InputManager.touchEvents.moveLeft = false;
  397.                     InputManager.touchEvents.moveRight = false;
  398.                     InputManager.touchEvents.moveDown = true;
  399.                     InputManager.touchEvents.moveUp = false;
  400.  
  401.                 } else if (touchAngle > 22.5) {//Right Down
  402.  
  403.                     InputManager.touchEvents.moveLeft = false;
  404.                     InputManager.touchEvents.moveRight = true;
  405.                     InputManager.touchEvents.moveDown = true;
  406.                     InputManager.touchEvents.moveUp = false;
  407.  
  408.                 } else {//Right
  409.  
  410.                     InputManager.touchEvents.moveLeft = false;
  411.                     InputManager.touchEvents.moveRight = true;
  412.                     InputManager.touchEvents.moveDown = false;
  413.                     InputManager.touchEvents.moveUp = false;
  414.  
  415.                 }
  416.  
  417.             }
  418.  
  419.         }
  420.  
  421.         readTouchStart(e): void {
  422.  
  423.             e.preventDefault();
  424.  
  425.             if (e.targetTouches[0].pageX < 20 && e.targetTouches[0].pageY < 20) {
  426.  
  427.                 ScreenManager.toggleFullScreen();
  428.  
  429.             }
  430.  
  431.             if (e.targetTouches[0].pageX <= InputManager.touchBounds.x && e.targetTouches[0].pageY >= InputManager.touchBounds.y) {
  432.  
  433.                 InputManager.checkTouchEvent(Engine.calculateAngle(InputManager.touchCenter, e.targetTouches[0].pageX, e.targetTouches[0].pageY) | 0);
  434.  
  435.             } else {
  436.  
  437.                 InputManager.touchEvents.deactivateAllMovement();
  438.                
  439.             }
  440.         }
  441.  
  442.         readTouchMove(e): void {
  443.            
  444.             e.preventDefault();        
  445.            
  446.             if (e.targetTouches[0].pageX <= InputManager.touchBounds.x && e.targetTouches[0].pageY >= InputManager.touchBounds.y) {
  447.  
  448.                 InputManager.checkTouchEvent(Engine.calculateAngle(InputManager.touchCenter, e.targetTouches[0].pageX, e.targetTouches[0].pageY) | 0);
  449.  
  450.             } else {
  451.  
  452.                 InputManager.touchEvents.deactivateAllMovement();
  453.  
  454.             }
  455.         }
  456.  
  457.         readTouchEnd(e): void {
  458.  
  459.             e.preventDefault();
  460.             InputManager.touchEvents.deactivateAllMovement();
  461.         }
  462.  
  463.         updateTouchCenter(): void{
  464.         }
  465.  
  466.         onResize(): void {
  467.  
  468.             if(InputManager.touchEvents.available) {
  469.  
  470.                 if(window.innerWidth > window.innerHeight) {//Landscape
  471.  
  472.                     InputManager.touchBounds.x = window.innerWidth/3;
  473.                     InputManager.touchBounds.y = window.innerHeight - InputManager.touchBounds.x;
  474.  
  475.                     InputManager.touchCenter.x = InputManager.touchBounds.x / 2;
  476.                     InputManager.touchCenter.y = InputManager.touchBounds.y + InputManager.touchCenter.x;
  477.  
  478.                     InputManager.touchIcon.style.top = InputManager.touchBounds.y + "px";
  479.                     InputManager.touchIcon.style.width = InputManager.touchBounds.x + "px";
  480.                     InputManager.touchIcon.style.height = "auto";
  481.  
  482.                 } else {//Portrait
  483.  
  484.                     InputManager.touchBounds.y = window.innerHeight - window.innerHeight/3;
  485.                     InputManager.touchBounds.x = window.innerHeight - InputManager.touchBounds.y;
  486.  
  487.                     InputManager.touchCenter.x = InputManager.touchBounds.x / 2;
  488.                     InputManager.touchCenter.y = InputManager.touchBounds.y + (window.innerHeight / 6);
  489.  
  490.                     InputManager.touchIcon.style.top = InputManager.touchBounds.y + "px";
  491.                     InputManager.touchIcon.style.width = InputManager.touchBounds.x + "px";
  492.                     InputManager.touchIcon.style.height = "auto";
  493.  
  494.                 }
  495.  
  496.             }
  497.            
  498.            
  499.         }
  500.  
  501.         readTouchScreen(e): void {
  502.  
  503.         }
  504.  
  505.         readMousePointer(e): void {
  506.         }
  507.  
  508.         readAccelerometer(e): void {
  509.             /*
  510.              * Documentation : http://www.w3.org/TR/orientation-event/
  511.              * X represents
  512.              *   the tile forward or backwards
  513.              *   negative values means more to the left (-8 is suffic)
  514.              *   positive values means more to the right (+8 is suffic)
  515.              * Y represents
  516.              *   the tilt left to right
  517.              *   increase values means more forward
  518.              *   how the screen is laying
  519.              *   upwards will have value of >9
  520.              *   downwards will have value of <9
  521.              */
  522.             /*
  523.             PASTRY.logPrint(
  524.                 " x : " + event.accelerationIncludingGravity.x +
  525.                 "<br/> y : " + event.accelerationIncludingGravity.y +
  526.                 "<br/> z : " + event.accelerationIncludingGravity.z
  527.             );
  528.             */
  529.  
  530.  
  531.         }
  532.  
  533.         updateGamePad (): void {
  534.  
  535.             this.updateGamePadPlayer(this.GAMEPAD_ONE_INDEX);
  536.             this.updateGamePadPlayer(this.GAMEPAD_TWO_INDEX);
  537.             this.updateGamePadPlayer(this.GAMEPAD_THREE_INDEX);
  538.             this.updateGamePadPlayer(this.GAMEPAD_FOUR_INDEX);
  539.         }
  540.  
  541.         updateGamePadPlayer(GAMEPAD_PLAYER_INDEX): void {
  542.  
  543.             this.GAMEPAD = navigator.getGamepads()[GAMEPAD_PLAYER_INDEX];
  544.             if (this.GAMEPAD) {
  545.  
  546.                 var gamePadEventSet:EventSet = InputManager.GAMEPADS[GAMEPAD_PLAYER_INDEX];
  547.  
  548.                 for (var i = 0; i < this.GAMEPAD.buttons.length; i++) {
  549.  
  550.                     switch (i) {
  551.  
  552.                         case GAMEPAD_XBOX.BUTTON_A:
  553.  
  554.                             gamePadEventSet.action = this.GAMEPAD.buttons[i].pressed;
  555.                             break;
  556.                     };
  557.  
  558.                 };
  559.  
  560.                 for (var i = 0; i < this.GAMEPAD.axes.length; i++) {
  561.  
  562.                     switch (i) {
  563.  
  564.                         case GAMEPAD_XBOX.STICK_LEFT_HORIZONTAL:
  565.  
  566.                             gamePadEventSet.moveLeft = this.GAMEPAD.axes[i] < -0.3;
  567.                             gamePadEventSet.moveRight = this.GAMEPAD.axes[i] > 0.3;
  568.                             break;
  569.  
  570.                         case GAMEPAD_XBOX.STICK_LEFT_VERTICAL:
  571.  
  572.                             gamePadEventSet.moveDown = this.GAMEPAD.axes[i] > 0.3;
  573.                             gamePadEventSet.moveUp = this.GAMEPAD.axes[i] < -0.3;
  574.                             break;
  575.                     }
  576.                 }
  577.             }
  578.         }
  579.  
  580.         update(): void {
  581.  
  582.             if (this.GamepadSupported) {
  583.  
  584.                 this.updateGamePad();
  585.  
  586.             }
  587.            
  588.  
  589.         }
  590.  
  591.         addListeners(): void {
  592.  
  593.             window.addEventListener("touchstart", this.readTouchStart, false);
  594.             window.addEventListener("touchmove", this.readTouchStart, false);
  595.             window.addEventListener("touchend", this.readTouchEnd, false);
  596.  
  597.             window.addEventListener("keydown", this.readKeyboardButtonPressed, false);
  598.             window.addEventListener("keyup", this.readKeyboardButtonLifted, false);
  599.  
  600.         }
  601.  
  602.         checkGamePadSupport(): void {
  603.  
  604.             this.GamepadSupported = "getGamepads" in navigator;
  605.  
  606.         }
  607.  
  608.         setupTouchEvents(): void {
  609.  
  610.             InputManager.touchIcon = document.getElementById("controller");
  611.             InputManager.touchEvents.available = "ontouchend" in document;
  612.  
  613.             if (InputManager.touchEvents.available) {
  614.  
  615.                 InputManager.touchIcon.style.display = "inline";
  616.  
  617.             }
  618.  
  619.         }
  620.  
  621.         init(): void {
  622.  
  623.             this.checkGamePadSupport();
  624.             this.addListeners();
  625.             this.setupTouchEvents();
  626.             this.onResize();
  627.         }
  628.  
  629.     }
  630.  
  631.     export class SpriteManager {
  632.  
  633.         static textureLength: number = 16;
  634.         static centerOffset: number = SpriteManager.textureLength / -2;
  635.  
  636.         static indexCounter: number = 0;
  637.  
  638.         static getIndexPos(): number {
  639.  
  640.             var pos = SpriteManager.indexCounter * SpriteManager.textureLength;
  641.  
  642.             SpriteManager.indexCounter++;
  643.  
  644.             return pos;
  645.  
  646.         }
  647.  
  648.     }
  649.  
  650.     export enum SpriteIndexs {
  651.  
  652.         none = -1,
  653.         muffinFrontIdle     = SpriteManager.getIndexPos(),
  654.         muffinFrontBlink1   = SpriteManager.getIndexPos(),
  655.         muffinFrontBlink2   = SpriteManager.getIndexPos(),
  656.  
  657.         muffinLeftIdle      = SpriteManager.getIndexPos(),
  658.         muffinLeftBlink1    = SpriteManager.getIndexPos(),
  659.         muffinLeftBlink2    = SpriteManager.getIndexPos(),
  660.  
  661.         muffinRightIdle     = SpriteManager.getIndexPos(),
  662.         muffinRightBlink1   = SpriteManager.getIndexPos(),
  663.         muffinRightBlink2   = SpriteManager.getIndexPos(),
  664.  
  665.         muffinBackIdle      = SpriteManager.getIndexPos(),
  666.  
  667.  
  668.         toasterPastryVanilla    = SpriteManager.getIndexPos(),
  669.         toasterPastryChocolate  = SpriteManager.getIndexPos(),
  670.  
  671.         toaster = SpriteManager.getIndexPos(),
  672.  
  673.         tileDirtLight = SpriteManager.getIndexPos(),
  674.         tileDirtDark = SpriteManager.getIndexPos(),
  675.  
  676.         tileGrass = SpriteManager.getIndexPos(),
  677.  
  678.         tileChest = SpriteManager.getIndexPos(),
  679.  
  680.     }
  681.  
  682.     export class CharacterAnimation {
  683.  
  684.         constructor( public idleLeft: SpriteIndexs, public idleRight: SpriteIndexs, public idleFront: SpriteIndexs, public idleBack: SpriteIndexs) {
  685.  
  686.  
  687.  
  688.         }
  689.  
  690.     }
  691.  
  692.     export enum CharacterType {
  693.  
  694.         muffin = 0,
  695.         toaster,
  696.  
  697.     }
  698.  
  699.     export class AnimationManager {
  700.  
  701.         static spriteSets: Array<CharacterAnimation> = [
  702.  
  703.             /*muffin */ new CharacterAnimation(SpriteIndexs.muffinLeftIdle, SpriteIndexs.muffinRightIdle, SpriteIndexs.muffinFrontIdle, SpriteIndexs.muffinBackIdle),
  704.             /*toaster*/
  705.  
  706.         ];
  707.  
  708.     }
  709.  
  710.     export class StatsType {
  711.  
  712.         static normal: Array<number> = [100, 100, 100, 100, 55];
  713.         static tank: Array<number> = [300, 80, 200, 100, 100];
  714.         static melee: Array<number> = [90, 300, 100, 100, 100];
  715.     }
  716.  
  717.     export class Stats {
  718.  
  719.         health: number;
  720.         attack: number;
  721.         armor: number;
  722.         money: number;
  723.         movementSpeed: number;
  724.  
  725.         constructor(params: Array<number>) {
  726.  
  727.             this.health = params[0];
  728.             this.attack = params[1];
  729.             this.armor = params[2];
  730.             this.money = params[3];
  731.             this.movementSpeed = params[4];
  732.  
  733.         }
  734.  
  735.     }
  736.  
  737.     export class Events {
  738.  
  739.         activate: boolean = false;
  740.         previous: boolean = false;
  741.         repeated: boolean = false;
  742.  
  743.         toggleActivation(condition: boolean): void {
  744.  
  745.             this.previous = this.activate;
  746.             this.activate = condition;
  747.             this.repeated = this.previous === this.activate;
  748.  
  749.         }
  750.  
  751.         turnActivated(): void {
  752.  
  753.             this.previous = this.activate;
  754.             this.activate = true;
  755.             this.repeated = this.previous === this.activate;
  756.  
  757.         }
  758.  
  759.         deActivate(): void {
  760.  
  761.             this.previous = this.activate;
  762.             this.activate = false;
  763.             this.repeated = this.previous === this.activate;
  764.  
  765.         }
  766.  
  767.         activateRepeated(): void {
  768.  
  769.             this.repeated = true;
  770.  
  771.         }
  772.  
  773.         deActivateRepeated(): void {
  774.  
  775.             this.repeated = false;
  776.  
  777.         }
  778.     }
  779.  
  780.     export class Player {
  781.  
  782.         stats: Stats;
  783.         animation: CharacterAnimation;
  784.         currentAnimationFrame: SpriteIndexs;
  785.         bounds: Bounds = new Bounds();
  786.         velocity: Position = new Position();
  787.  
  788.         eventMoveLeft = new Events();
  789.         eventMoveRight = new Events();
  790.         eventMoveUp = new Events();
  791.         eventMoveDown = new Events();
  792.         eventAction = new Events();
  793.  
  794.         private tileRegion: Bounds = new Bounds();
  795.  
  796.         constructor(public name: string, x: number, y: number, public spriteIdentity, public statsType: Array<number>, public animationID: CharacterType) {
  797.  
  798.             this.bounds.min = new Position(x, y);
  799.             this.bounds.max = new Position(x + SpriteManager.textureLength, y + SpriteManager.textureLength);
  800.             this.stats = new Stats(statsType);
  801.             this.animation = AnimationManager.spriteSets[animationID];
  802.             this.currentAnimationFrame = this.animation.idleFront;
  803.         }
  804.  
  805.         private setX(x): void {
  806.  
  807.             this.bounds.min.setX(x);
  808.             this.updateMaxX();
  809.  
  810.         }
  811.  
  812.         private setY(y): void {
  813.  
  814.             this.bounds.min.setY(y);
  815.             this.updateMaxY();
  816.  
  817.         }
  818.  
  819.         private updateMaxY(): void {
  820.  
  821.             this.bounds.max.setY(this.bounds.min.y + SpriteManager.textureLength);
  822.  
  823.         }
  824.  
  825.         private updateMaxX(): void {
  826.  
  827.             this.bounds.max.setX(this.bounds.min.x + SpriteManager.textureLength);
  828.  
  829.         }
  830.  
  831.         private setAnimation(frame: SpriteIndexs): void {
  832.  
  833.             this.currentAnimationFrame = frame;
  834.  
  835.         }
  836.  
  837.         private updateX(): void {
  838.  
  839.             this.bounds.min.updateX(TimeManager.lapse * this.velocity.x);
  840.             this.updateMaxX();
  841.  
  842.         }
  843.  
  844.         private updateY(): void {
  845.  
  846.             this.bounds.min.updateY(TimeManager.lapse * this.velocity.y);
  847.             this.updateMaxY();
  848.  
  849.         }
  850.  
  851.         private updateVelocityX(direction: number): void {
  852.  
  853.             this.velocity.setX(direction * this.stats.movementSpeed);
  854.             this.updateX();
  855.  
  856.         }
  857.  
  858.         private updateVelocityY(direction: number): void {
  859.            
  860.             this.velocity.setY(direction * this.stats.movementSpeed);
  861.             this.updateY();
  862.  
  863.         }
  864.  
  865.         private updateCollisionRegion(): void {
  866.  
  867.             this.tileRegion.min.x = ((this.bounds.min.x / SpriteManager.textureLength) | 0) - 1;
  868.             this.tileRegion.min.y = ((this.bounds.min.y / SpriteManager.textureLength) | 0) - 1;
  869.             this.tileRegion.max.x = ((this.bounds.max.x / SpriteManager.textureLength) | 0)+ 1;
  870.             this.tileRegion.max.y = ((this.bounds.max.y / SpriteManager.textureLength) | 0) + 1;
  871.  
  872.         }
  873.  
  874.         private checkAABBcollision(tileX: number, tileY: number): boolean {
  875.  
  876.             return (
  877.                 (this.bounds.min.x < tileX + SpriteManager.textureLength) &&
  878.                 (this.bounds.min.y < tileY + SpriteManager.textureLength) &&
  879.                 (this.bounds.max.x > tileX) &&
  880.                 (this.bounds.max.y > tileY)
  881.             );
  882.  
  883.         }
  884.  
  885.         updatePosition(world: World): void {
  886.  
  887.             if (this.eventMoveLeft.activate !== this.eventMoveRight.activate) {
  888.  
  889.                 if (this.eventMoveLeft.activate) {
  890.  
  891.                     this.updateVelocityX(-1);
  892.                     this.setAnimation(this.animation.idleLeft)
  893.  
  894.                 } else if (this.eventMoveRight.activate) {
  895.  
  896.                     this.updateVelocityX(1);
  897.                     this.setAnimation(this.animation.idleRight)
  898.  
  899.                 }
  900.  
  901.  
  902.                 this.updateCollisionRegion();
  903.  
  904.                 for (var x: number = this.tileRegion.min.x; x < this.tileRegion.max.x; x++) {
  905.  
  906.                     for (var y: number = this.tileRegion.min.y; y < this.tileRegion.max.y; y++) {
  907.  
  908.                         if (world.validPos(x, y) && world.solidPos(x, y, WorldLayer.backGround)){
  909.  
  910.                             var tileCoordX: number = x * SpriteManager.textureLength;
  911.                             var tileCoordY: number = y * SpriteManager.textureLength;
  912.  
  913.                             if(this.checkAABBcollision(tileCoordX, tileCoordY)) {
  914.  
  915.                                 if(this.velocity.x > 0) {
  916.  
  917.                                     this.setX(tileCoordX - SpriteManager.textureLength);
  918.  
  919.                                 } else {
  920.  
  921.                                     this.setX(tileCoordX + SpriteManager.textureLength);
  922.  
  923.                                 }
  924.  
  925.                             }
  926.  
  927.  
  928.                         }
  929.  
  930.                     }
  931.  
  932.                 }
  933.  
  934.             } else {
  935.  
  936.                 this.updateVelocityX(0);
  937.                 this.setX(this.bounds.min.x | 0);
  938.  
  939.             }
  940.  
  941.             if (this.eventMoveUp.activate !== this.eventMoveDown.activate) {
  942.  
  943.                 if (this.eventMoveUp.activate) {
  944.  
  945.                     this.updateVelocityY(-1);                  
  946.                     this.setAnimation(this.animation.idleBack)
  947.  
  948.                 } else if (this.eventMoveDown.activate) {
  949.  
  950.                     this.updateVelocityY(1);
  951.                     this.setAnimation(this.animation.idleFront)
  952.  
  953.                 }
  954.  
  955.                 this.updateCollisionRegion();
  956.  
  957.                 for (var x: number = this.tileRegion.min.x; x < this.tileRegion.max.x; x++) {
  958.  
  959.                     for (var y: number = this.tileRegion.min.y; y < this.tileRegion.max.y; y++) {
  960.  
  961.                         if (world.validPos(x, y) && world.solidPos(x, y, WorldLayer.backGround)) {
  962.  
  963.                             var tileCoordX: number = x * SpriteManager.textureLength;
  964.                             var tileCoordY: number = y * SpriteManager.textureLength;
  965.  
  966.                             if (this.checkAABBcollision(tileCoordX, tileCoordY)) {
  967.  
  968.                                 if (this.velocity.y > 0) {
  969.  
  970.                                     this.setY(tileCoordY - SpriteManager.textureLength);
  971.  
  972.                                 } else {
  973.  
  974.                                     this.setY(tileCoordY + SpriteManager.textureLength);
  975.  
  976.                                 }
  977.  
  978.                             }
  979.  
  980.  
  981.                         }
  982.  
  983.                     }
  984.  
  985.                 }
  986.  
  987.             } else {
  988.  
  989.                 this.updateVelocityY(0);
  990.                 this.setY(this.bounds.min.y | 0);
  991.  
  992.             }
  993.  
  994.             if (this.bounds.min.x < 0) {
  995.  
  996.                 this.updateVelocityX(0);
  997.                 this.setX(0);
  998.  
  999.             }
  1000.  
  1001.             if (this.bounds.min.y < 0) {
  1002.  
  1003.                 this.updateVelocityY(0);
  1004.                 this.setY(0);
  1005.  
  1006.             }
  1007.  
  1008.         }
  1009.  
  1010.     }
  1011.  
  1012.     export enum PlayerIndex {
  1013.         one = 0,
  1014.         two = 1,
  1015.         three  = 2,
  1016.         four = 5,
  1017.     }
  1018.  
  1019.     export class EntityManager {
  1020.  
  1021.         playerOne: Player = new Player("FluffyMuffin", 10, 10, null, StatsType.normal, CharacterType.muffin);
  1022.         playerTwo: Player;
  1023.         playerThree: Player;
  1024.         playerFour: Player;
  1025.  
  1026.         private world: World;
  1027.        
  1028.         private updatePlayer(currentPlayer: Player, index: PlayerIndex): void {
  1029.        
  1030.             switch (index) {
  1031.  
  1032.                 case PlayerIndex.one:
  1033.  
  1034.                     currentPlayer.eventMoveLeft.toggleActivation(
  1035.                         InputManager.keyBoardEvents.moveLeft || InputManager.GAMEPADS[index].moveLeft || InputManager.touchEvents.moveLeft);
  1036.  
  1037.                     currentPlayer.eventMoveRight.toggleActivation(
  1038.                         InputManager.keyBoardEvents.moveRight || InputManager.GAMEPADS[index].moveRight || InputManager.touchEvents.moveRight);
  1039.  
  1040.                     currentPlayer.eventMoveUp.toggleActivation(
  1041.                         InputManager.keyBoardEvents.moveUp || InputManager.GAMEPADS[index].moveUp || InputManager.touchEvents.moveUp);
  1042.  
  1043.                     currentPlayer.eventMoveDown.toggleActivation(
  1044.                         InputManager.keyBoardEvents.moveDown || InputManager.GAMEPADS[index].moveDown || InputManager.touchEvents.moveDown);
  1045.  
  1046.                     currentPlayer.eventAction.toggleActivation(
  1047.                         InputManager.keyBoardEvents.action || InputManager.GAMEPADS[index].action || InputManager.touchEvents.action);
  1048.  
  1049.                     break;
  1050.  
  1051.                 default:
  1052.                     currentPlayer.eventMoveLeft.toggleActivation(InputManager.GAMEPADS[index].moveLeft);
  1053.  
  1054.                     currentPlayer.eventMoveRight.toggleActivation(InputManager.GAMEPADS[index].moveRight);
  1055.  
  1056.                     currentPlayer.eventMoveUp.toggleActivation(InputManager.GAMEPADS[index].moveUp);
  1057.  
  1058.                     currentPlayer.eventMoveDown.toggleActivation(InputManager.GAMEPADS[index].moveDown);
  1059.  
  1060.                     currentPlayer.eventAction.toggleActivation(InputManager.GAMEPADS[index].action);
  1061.  
  1062.             }
  1063.  
  1064.             currentPlayer.updatePosition(this.world);
  1065.         }
  1066.  
  1067.         private updatePlayerEntities(): void {
  1068.  
  1069.             this.updatePlayer(this.playerOne, PlayerIndex.one);
  1070.  
  1071.         }
  1072.  
  1073.         update(world: World): void {
  1074.  
  1075.             this.world = world;
  1076.  
  1077.             this.updatePlayerEntities();
  1078.  
  1079.         }
  1080.     }
  1081.  
  1082.     export class TextureManager {
  1083.  
  1084.         private fileImageData: any;
  1085.         private fileImageDimensions: Dimension = new Dimension();
  1086.         private fileImageScaledDimensions: Dimension = new Dimension();
  1087.  
  1088.         public textureLength: number = SpriteManager.textureLength;
  1089.         public textureScale: number = 1;
  1090.  
  1091.         public spriteSheetCanvas: any;
  1092.         private spriteSheetContext: any;
  1093.  
  1094.  
  1095.         constructor(public filePath: string = "resources/sprites/spritesheet.png") {
  1096.  
  1097.         }
  1098.  
  1099.         setupCanvas(): void {
  1100.  
  1101.             this.spriteSheetCanvas = document.createElement('canvas');
  1102.             this.spriteSheetContext = this.spriteSheetCanvas.getContext('2d');
  1103.  
  1104.         }
  1105.  
  1106.         resizeCanvas(width: number, height: number): void {
  1107.  
  1108.             this.spriteSheetCanvas.width = width;
  1109.             this.spriteSheetCanvas.height = height;
  1110.  
  1111.         }
  1112.  
  1113.         generateUnScaledImgData(fileImage): void {
  1114.             this.fileImageDimensions.setDimensions(fileImage.width, fileImage.height);
  1115.  
  1116.             this.resizeCanvas(this.fileImageDimensions.width, this.fileImageDimensions.height);
  1117.             this.spriteSheetContext.drawImage(fileImage, 0, 0);
  1118.             this.fileImageData = this.spriteSheetContext.getImageData(0, 0, this.fileImageDimensions.width, this.fileImageDimensions.height).data;
  1119.  
  1120.         }
  1121.  
  1122.         setScale(scaleMutiplier: number): void {
  1123.             this.textureScale = (scaleMutiplier | 0);
  1124.             this.textureLength = this.textureScale * SpriteManager.textureLength;
  1125.         }
  1126.  
  1127.         upScaleSpriteSheet(scaleMutiplier: number): void {
  1128.             /// <summary>
  1129.             /// Scales the sprite sheet every time
  1130.             /// </summary>
  1131.             /// <param name="scaleMutiplier"></param>
  1132.  
  1133.             this.setScale(scaleMutiplier);
  1134.  
  1135.             this.fileImageScaledDimensions.setDimensions(this.fileImageDimensions.width * this.textureScale, this.fileImageDimensions.height * this.textureScale)
  1136.  
  1137.             this.resizeCanvas(this.fileImageScaledDimensions.width, this.fileImageScaledDimensions.height);
  1138.  
  1139.             var canvasUpscalePixels = this.spriteSheetContext.getImageData(0, 0, this.fileImageScaledDimensions.width, this.fileImageScaledDimensions.height);
  1140.  
  1141.             var iscale = 1.0 / this.textureScale;
  1142.             var indexScaled = 0;
  1143.  
  1144.             // (T << shifter == T * spriteSheet.width * 4)
  1145.             var shifter = (Math.log(this.fileImageDimensions.width) * 1.442695040888964 + 2) | 0;
  1146.             var y = 0;
  1147.             while (y < this.fileImageScaledDimensions.height) {
  1148.  
  1149.                 var a = (y * iscale) << shifter;
  1150.  
  1151.                 for (var c = this.textureScale; c--;) {
  1152.  
  1153.                     for (var x = 0; x < this.fileImageScaledDimensions.width;) {
  1154.  
  1155.                         var index = ((x * iscale) << 2) + a;
  1156.                         var R = this.fileImageData[index++];
  1157.                         var G = this.fileImageData[index++];
  1158.                         var B = this.fileImageData[index++];
  1159.                         var A = this.fileImageData[index];
  1160.                         for (var z = this.textureScale; z--;) {
  1161.                             canvasUpscalePixels.data[indexScaled++] = R;
  1162.                             canvasUpscalePixels.data[indexScaled++] = G;
  1163.                             canvasUpscalePixels.data[indexScaled++] = B;
  1164.                             canvasUpscalePixels.data[indexScaled++] = A;
  1165.                         }
  1166.                         x += this.textureScale;
  1167.                     }
  1168.                     y++;
  1169.                 }
  1170.             }
  1171.             this.spriteSheetContext.putImageData(canvasUpscalePixels, 0, 0);
  1172.  
  1173.         }
  1174.  
  1175.         load(): void {
  1176.  
  1177.             var fileImage = new Image();
  1178.             fileImage.src = this.filePath;
  1179.  
  1180.             fileImage.onload = () => {
  1181.  
  1182.                 this.generateUnScaledImgData(fileImage);
  1183.                 PASTRY.resourcesLoaded();
  1184.  
  1185.             }
  1186.         }
  1187.  
  1188.         init(): void {
  1189.  
  1190.             this.setupCanvas();
  1191.             this.load();
  1192.  
  1193.         }
  1194.  
  1195.     }
  1196.  
  1197.     export enum WorldLayer {
  1198.  
  1199.         backGround = 0,
  1200.         foreGround = 1,
  1201.  
  1202.     }
  1203.  
  1204.     export class World {
  1205.  
  1206.         public map: Array<SpriteIndexs>[][] = [];
  1207.         public dimensions: Dimension;
  1208.  
  1209.         constructor(width: number, height: number) {
  1210.  
  1211.             this.dimensions = new Dimension(width, height);
  1212.  
  1213.             for (var x = 0; x < width; x++) {
  1214.  
  1215.                 this.map[x] = [];
  1216.  
  1217.                 for (var y = 0; y < height; y++) {
  1218.  
  1219.                     this.map[x][y] = [];
  1220.  
  1221.                 }
  1222.  
  1223.             }
  1224.  
  1225.         }
  1226.  
  1227.         public setPos(x: number, y: number, z: WorldLayer, type: SpriteIndexs) {
  1228.            
  1229.             if (x >= 0 && x < this.dimensions.width && y >= 0 && y < this.dimensions.height) {
  1230.  
  1231.                 this.map[x][y][z] = type;
  1232.  
  1233.             }
  1234.  
  1235.         }
  1236.  
  1237.         public solidPos(x: number, y: number, z: WorldLayer): boolean {
  1238.  
  1239.             switch(this.map[x][y][z]) {
  1240.            
  1241.                 case SpriteIndexs.tileDirtLight:
  1242.                 case SpriteIndexs.tileDirtDark:
  1243.  
  1244.                     return true;
  1245.  
  1246.                     break;
  1247.  
  1248.                 default:
  1249.                     return false;
  1250.             }
  1251.  
  1252.         }
  1253.  
  1254.         public emptyPos(x: number, y: number, z: WorldLayer): boolean {
  1255.  
  1256.             return this.map[x][y][z] === SpriteIndexs.none;
  1257.  
  1258.         }
  1259.  
  1260.         public validPos(x: number, y: number): boolean {
  1261.  
  1262.             return x >= 0 && x < this.dimensions.width && y >= 0 && y < this.dimensions.height;
  1263.  
  1264.         }
  1265.  
  1266.         public getPos(x: number, y: number, z: WorldLayer): SpriteIndexs {
  1267.  
  1268.             return this.map[x][y][z];
  1269.  
  1270.         }
  1271.     }
  1272.  
  1273.     export class WorldGenerator {
  1274.  
  1275.         private seed: number;
  1276.         private max: number = Math.pow(2, 32);
  1277.  
  1278.         private birthLimit: number = 4;
  1279.         private deathLimit: number = 3;
  1280.         private stepCount: number = 4;
  1281.         private initalChance: number = 0.45;
  1282.  
  1283.         public setSeed(seed: number = Math.round(Math.random() * this.max)): void {
  1284.  
  1285.             this.seed = seed;
  1286.  
  1287.         }
  1288.  
  1289.         public getSeed(): number {
  1290.  
  1291.             return this.seed;
  1292.  
  1293.         }
  1294.  
  1295.         public random(): number {
  1296.  
  1297.             this.seed += (this.seed * this.seed) | 5;
  1298.  
  1299.             return (this.seed >>> 32) / this.max;
  1300.  
  1301.         }
  1302.  
  1303.         public randomRanged(min: number, max: number): number {
  1304.  
  1305.             return ((this.random() * (max - min + 1))|0) + min;
  1306.        
  1307.         }
  1308.  
  1309.         private initalizeWorld(world: World) {
  1310.  
  1311.             world.map = [];
  1312.             for (var x: number = 0; x < world.dimensions.width; x++) {
  1313.  
  1314.                 world.map[x] = [];
  1315.  
  1316.                 for (var y: number = 0; y < world.dimensions.height; y++) {
  1317.  
  1318.                     world.map[x][y] = [SpriteIndexs.none, SpriteIndexs.none];
  1319.  
  1320.                 }
  1321.  
  1322.             }
  1323.  
  1324.         }
  1325.  
  1326.         private generateSimple(world: World) {
  1327.  
  1328.             for (var x = 0; x < world.dimensions.width; x++) {
  1329.  
  1330.                 for (var y = 0; y < world.dimensions.height; y++) {
  1331.  
  1332.                     world.setPos(x, y, WorldLayer.backGround, SpriteIndexs.tileGrass);
  1333.  
  1334.                 }
  1335.  
  1336.             }
  1337.  
  1338.         }
  1339.  
  1340.         private generateCaveSystem(world: World): void {
  1341.  
  1342.             var map: boolean[][] = [];
  1343.  
  1344.             map = this.sampleMapSetup(map, world.dimensions);
  1345.        
  1346.             for (var i: number = 0; i < this.stepCount; i++) {
  1347.  
  1348.                 map = this.sampleMapCycle(map);
  1349.  
  1350.             }
  1351.  
  1352.             this.placeTreasure(map, world);
  1353.             this.applySampleMap(map, world);
  1354.         }
  1355.  
  1356.         private placeTreasure(map: boolean[][], world: World) {
  1357.  
  1358.             var rarity: number = 5;
  1359.  
  1360.             for (var x: number = 0; x < world.dimensions.width; x++) {
  1361.  
  1362.                 for (var y: number = 0; y < world.dimensions.height; y++) {
  1363.  
  1364.                     if (!map[x][y]) {
  1365.  
  1366.                         var count: number = this.countSurroundingSolids(map, x, y);
  1367.  
  1368.                         if(count >= rarity) {
  1369.  
  1370.                             world.setPos(x, y, WorldLayer.foreGround, SpriteIndexs.tileChest);
  1371.  
  1372.                         }
  1373.  
  1374.                     }
  1375.  
  1376.                 }
  1377.  
  1378.             }
  1379.  
  1380.         }
  1381.  
  1382.         private applySampleMap(map: boolean[][], world: World): void {
  1383.  
  1384.             //Place map
  1385.             for (var x: number = 0; x < world.dimensions.width; x++) {
  1386.  
  1387.                 for (var y: number = 0; y < world.dimensions.height; y++) {
  1388.  
  1389.                     if (map[x][y]) {
  1390.  
  1391.                         world.setPos(x, y, WorldLayer.backGround, SpriteIndexs.tileDirtLight);
  1392.  
  1393.                     } else {
  1394.  
  1395.                         world.setPos(x, y, WorldLayer.backGround, SpriteIndexs.tileGrass);
  1396.  
  1397.                     }
  1398.  
  1399.                 }
  1400.  
  1401.             }
  1402.  
  1403.         }
  1404.  
  1405.         private countSurroundingSolids(map: boolean[][], x: number, y: number): number {
  1406.  
  1407.             var count: number = 0;
  1408.  
  1409.             for (var ix: number = -1; ix < 2; ix++) {
  1410.  
  1411.                 for (var iy: number = -1; iy < 2; iy++) {
  1412.  
  1413.                     var tileX: number = x + ix;
  1414.                     var tileY: number = y + iy;
  1415.  
  1416.                     if (!(ix == 0 && iy == 0)) {
  1417.  
  1418.                         if (tileX < 0 || tileY < 0 || tileX >= map.length || tileY >= map[0].length) {
  1419.  
  1420.                             count++;
  1421.  
  1422.                         } else if (map[tileX][tileY]) {
  1423.  
  1424.                             count++;
  1425.  
  1426.                         }
  1427.  
  1428.                     }
  1429.  
  1430.                 }
  1431.  
  1432.             }
  1433.  
  1434.             return count;
  1435.  
  1436.         }
  1437.  
  1438.         private sampleMapCycle(map: boolean[][]): boolean[][] {
  1439.  
  1440.             var newMap: boolean[][] = [];
  1441.            
  1442.             for (var x: number = 0, w: number = map.length, h: number = map[0].length; x < w; x++) {
  1443.  
  1444.                 newMap[x] = [];
  1445.  
  1446.                 for (var y:number = 0; y < h; y++) {
  1447.  
  1448.                     var count: number = this.countSurroundingSolids(map, x, y);
  1449.  
  1450.                     newMap[x][y] = (map[x][y]) ? !(count < this.deathLimit) : (count > this.birthLimit);
  1451.  
  1452.                 }
  1453.  
  1454.             }
  1455.  
  1456.             return newMap;
  1457.  
  1458.         }
  1459.  
  1460.         private sampleMapSetup(map: boolean[][], dimensions: Dimension): boolean[][]{
  1461.  
  1462.             for (var x = 0; x < dimensions.width; x++) {
  1463.  
  1464.                 map[x] = [];
  1465.  
  1466.                 for (var y = 0; y < dimensions.height; y++) {
  1467.  
  1468.                     map[x][y] = this.random() < this.initalChance;
  1469.  
  1470.                 }
  1471.  
  1472.             }
  1473.  
  1474.             return map;
  1475.  
  1476.         }
  1477.  
  1478.         public generateWorld(seed: number, width: number, height: number): World {
  1479.  
  1480.             var world: World = new World(width, height);
  1481.  
  1482.             this.setSeed(seed);
  1483.  
  1484.             this.initalizeWorld(world);
  1485.             this.generateCaveSystem(world);
  1486.            
  1487.  
  1488.             return world;
  1489.  
  1490.         }
  1491.  
  1492.     }
  1493.  
  1494.     export class WorldManager {
  1495.  
  1496.         public currentWorld: World;
  1497.         private generator: WorldGenerator = new WorldGenerator();
  1498.  
  1499.         public setWorld(seed: number, width: number, height: number): void {
  1500.  
  1501.             this.currentWorld = this.generator.generateWorld(seed, width, height);
  1502.  
  1503.         }
  1504.  
  1505.     }
  1506.  
  1507.     export class ScreenManager {
  1508.  
  1509.         windowDimensions: PastryCore.Dimension = new PastryCore.Dimension(window.innerWidth, window.innerHeight);
  1510.         static defaultDimension: PastryCore.Dimension = new PastryCore.Dimension(240, 160);
  1511.         currentDimension: PastryCore.Dimension = new PastryCore.Dimension(240, 160);
  1512.  
  1513.         scale: number = 1;
  1514.  
  1515.         fullscreen: boolean = false;
  1516.         fullScreenCapable: boolean = false;
  1517.  
  1518.         constructor() {
  1519.  
  1520.  
  1521.  
  1522.         }
  1523.  
  1524.         static toggleFullScreen(): void {
  1525.  
  1526.             if ((document.fullScreenElement && document.fullScreenElement !== null) || document.mozFullScreen || document.webkitIsFullScreen) {
  1527.  
  1528.                 if (document.exitFullscreen) {
  1529.  
  1530.                     document.exitFullscreen();
  1531.  
  1532.                 } else if (document.msExitFullscreen) {
  1533.  
  1534.                     document.msExitFullscreen();
  1535.  
  1536.                 } else if (document.mozCancelFullScreen) {
  1537.  
  1538.                     document.mozCancelFullScreen();
  1539.  
  1540.                 } else if (document.webkitExitFullscreen) {
  1541.  
  1542.                     document.webkitExitFullscreen();
  1543.  
  1544.                 }
  1545.  
  1546.             } else {
  1547.  
  1548.                 if (document.documentElement.requestFullscreen) {
  1549.  
  1550.                     document.documentElement.requestFullscreen();
  1551.  
  1552.                 } else if (document.documentElement.msRequestFullscreen) {
  1553.  
  1554.                     document.documentElement.msRequestFullscreen();
  1555.  
  1556.                 } else if (document.documentElement.mozRequestFullScreen) {
  1557.  
  1558.                     document.documentElement.mozRequestFullScreen();
  1559.  
  1560.                 } else if (document.documentElement.webkitRequestFullscreen) {
  1561.  
  1562.                     document.documentElement.webkitRequestFullscreen();
  1563.  
  1564.                 }
  1565.  
  1566.             }
  1567.  
  1568.         }
  1569.  
  1570.         updateWindowDimensions(): void {
  1571.  
  1572.             this.windowDimensions.setDimensions(window.innerWidth, window.innerHeight);
  1573.  
  1574.         }
  1575.  
  1576.         updateDimensions(): void {
  1577.  
  1578.             this.currentDimension.setDimensions(ScreenManager.defaultDimension.width * this.scale, ScreenManager.defaultDimension.height * this.scale);
  1579.  
  1580.         }
  1581.  
  1582.         updateScale(): boolean {
  1583.  
  1584.             var displayScaleX: number = (this.windowDimensions.width / ScreenManager.defaultDimension.width) | 0;
  1585.             var displayScaleY: number = (this.windowDimensions.height / ScreenManager.defaultDimension.height) | 0;
  1586.  
  1587.             var before: number = this.scale;
  1588.             this.scale = (displayScaleX <= displayScaleY) ? displayScaleX : displayScaleY;
  1589.  
  1590.             if (this.scale < 1) {
  1591.  
  1592.                 this.scale = 1;
  1593.  
  1594.             }
  1595.             console.log(this.scale);
  1596.             return before !== this.scale;
  1597.  
  1598.         }
  1599.  
  1600.     }
  1601.  
  1602.     export class Camera {
  1603.  
  1604.         public tileIndex: Position = new Position();
  1605.         public tilePos: Position = new Position();
  1606.         public worldOffset: Position = new Position();
  1607.  
  1608.         public screenTileCapacity: Dimension = new Dimension();
  1609.         public spriteCentered: Position = new Position();
  1610.  
  1611.         public focusEntity: boolean = true;
  1612.  
  1613.  
  1614.         constructor() {
  1615.  
  1616.             this.screenTileCapacity.width = ((ScreenManager.defaultDimension.width / SpriteManager.textureLength) | 0) + 2;//11
  1617.             this.screenTileCapacity.height = ((ScreenManager.defaultDimension.height / SpriteManager.textureLength) | 0) + 2;//16
  1618.  
  1619.             this.spriteCentered.x = ((ScreenManager.defaultDimension.width/2)|0) - (SpriteManager.textureLength / 2);
  1620.             this.spriteCentered.y = ((ScreenManager.defaultDimension.height/2)|0) - (SpriteManager.textureLength / 2);
  1621.  
  1622.         }
  1623.  
  1624.  
  1625.  
  1626.         update(entityFocus: Position): void {
  1627.  
  1628.             this.worldOffset.x = entityFocus.x - this.spriteCentered.x;
  1629.             this.worldOffset.y = entityFocus.y - this.spriteCentered.y;
  1630.  
  1631.             this.tileIndex.x = ((this.worldOffset.x / SpriteManager.textureLength) | 0) - 1;
  1632.             this.tileIndex.y = ((this.worldOffset.y / SpriteManager.textureLength) | 0) - 1;
  1633.  
  1634.             this.tilePos.x = this.tileIndex.x * SpriteManager.textureLength - this.worldOffset.x;
  1635.             this.tilePos.y = this.tileIndex.y * SpriteManager.textureLength - this.worldOffset.y;
  1636.  
  1637.         }
  1638.     }
  1639.  
  1640.     export class Renderer {
  1641.  
  1642.         private textureManager: TextureManager = new TextureManager();
  1643.         private screenManager: ScreenManager = new ScreenManager();
  1644.         private camera: Camera = new Camera();
  1645.  
  1646.         private canvas: any;
  1647.         private context: any;
  1648.         private bufferCanvas: any;
  1649.         private bufferContext: any;
  1650.         private canvasId: string = "appCanvas";
  1651.  
  1652.         private updateCanvasDimensions(): void {
  1653.  
  1654.             this.canvas.width = this.bufferCanvas.width = this.screenManager.currentDimension.width;
  1655.             this.canvas.height = this.bufferCanvas.height = this.screenManager.currentDimension.height;
  1656.  
  1657.             document.getElementById(this.canvasId).setAttribute('width', this.canvas.width);
  1658.             document.getElementById(this.canvasId).setAttribute('height', this.canvas.height);
  1659.  
  1660.         }
  1661.  
  1662.         private centerCanvas(): void {
  1663.  
  1664.             this.canvas.style.left = (((this.screenManager.windowDimensions.width - this.screenManager.currentDimension.width) / 2) | 0) + "px";
  1665.             this.canvas.style.top = (((this.screenManager.windowDimensions.height - this.screenManager.currentDimension.height) / 2) | 0) + "px";
  1666.        
  1667.  
  1668.         }
  1669.  
  1670.         resize():void {
  1671.  
  1672.             this.screenManager.updateWindowDimensions();
  1673.  
  1674.             if (this.screenManager.updateScale()) {
  1675.  
  1676.                 this.textureManager.upScaleSpriteSheet(this.screenManager.scale);
  1677.  
  1678.             }
  1679.  
  1680.             this.screenManager.updateDimensions();
  1681.             this.updateCanvasDimensions();
  1682.             this.centerCanvas();
  1683.         }
  1684.  
  1685.         private addListeners(): void {
  1686.  
  1687.         }
  1688.  
  1689.         private initalizeBufferedContext (): void {
  1690.  
  1691.             this.bufferContext.font = '10pt Calibri';
  1692.             this.bufferContext.textAlign = 'center';
  1693.             this.bufferContext.fillStyle = 'white';
  1694.  
  1695.         }
  1696.  
  1697.         private createCanvas (id): any {
  1698.  
  1699.             var canvas = document.getElementById(this.canvasId);
  1700.  
  1701.             if (canvas === null) {
  1702.  
  1703.                 canvas = document.createElement('canvas');
  1704.                 canvas.id = this.canvasId;
  1705.                 canvas.className = "canvasCrispRendering";
  1706.                 document.body.appendChild(this.canvas);
  1707.  
  1708.             };
  1709.             return canvas;
  1710.  
  1711.         }
  1712.  
  1713.         private setupCanvas(): void {
  1714.  
  1715.             this.canvas = this.createCanvas(this.canvasId);
  1716.             this.context = this.canvas.getContext("2d");
  1717.  
  1718.             this.bufferCanvas = document.createElement('canvas');
  1719.             this.bufferContext = this.bufferCanvas.getContext('2d');
  1720.  
  1721.             this.initalizeBufferedContext();
  1722.  
  1723.         }
  1724.  
  1725.         init(): void {
  1726.  
  1727.             this.addListeners();
  1728.             this.setupCanvas();
  1729.             this.textureManager.init();
  1730.  
  1731.         }
  1732.  
  1733.         refreshBuffer(): void {
  1734.  
  1735.             this.bufferContext.clearRect(0, 0, this.bufferCanvas.width, this.bufferCanvas.height);
  1736.  
  1737.         }
  1738.  
  1739.         render(): void {
  1740.            
  1741.             this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  1742.             this.context.drawImage(this.bufferCanvas, 0, 0);
  1743.  
  1744.         }
  1745.  
  1746.         drawInGame(entityManager: EntityManager, world: World): void {
  1747.  
  1748.             this.camera.update(entityManager.playerOne.bounds.min);
  1749.  
  1750.             this.drawTileMap(world);
  1751.             this.drawPlayerEntity(entityManager.playerOne, true);
  1752.  
  1753.             this.drawHUD();
  1754.  
  1755.             this.drawMiniMap(5, 100, world);
  1756.  
  1757.             //this.drawText("MinPos: " + entityManager.playerOne.bounds.min.x + " : " + entityManager.playerOne.bounds.min.y, 10, 20, false);
  1758.             //this.drawText("MaxPos: " + entityManager.playerOne.bounds.max.x + " : " + entityManager.playerOne.bounds.max.y, 10, 30, false);
  1759.  
  1760.         }
  1761.  
  1762.  
  1763.         drawMiniMap(px: number, py: number, world: World): void {
  1764.  
  1765.             var counterX: number = 0;
  1766.             var counterY: number = 0;
  1767.             var scale: number = 1;
  1768.  
  1769.             for (var x: number = this.camera.tileIndex.x - 5, counterX = 0, xEnd: number = 10 + x + this.camera.screenTileCapacity.width, yEnd: number = 5 + this.camera.tileIndex.y + this.camera.screenTileCapacity.height; x < xEnd; x++, counterX++) {
  1770.  
  1771.                 for (var y: number = this.camera.tileIndex.y - 5, counterY = 0; y < yEnd; y++, counterY++) {
  1772.  
  1773.                     if (world.validPos(x, y)) {
  1774.  
  1775.                         switch (world.getPos(x, y, WorldLayer.backGround)) {
  1776.  
  1777.                             case SpriteIndexs.tileDirtLight:
  1778.  
  1779.                                 this.bufferContext.fillRect(px + counterX * scale, py + counterY * scale, scale, scale);
  1780.  
  1781.                                 break;
  1782.                         }
  1783.                     }
  1784.  
  1785.                 }
  1786.  
  1787.             }
  1788.  
  1789.         }
  1790.  
  1791.         drawHUD(): void {
  1792.  
  1793.             this.drawText("FPS: " + TimeManager.fps, 10, 10, false);
  1794.  
  1795.         }
  1796.  
  1797.         private drawTileMap(world: World): void {
  1798.  
  1799.             for (var x:number = this.camera.tileIndex.x, xCounter: number = 0, xEnd: number = x + this.camera.screenTileCapacity.width; x < xEnd; x++, xCounter++) {
  1800.  
  1801.                 for (var y: number = this.camera.tileIndex.y, yCounter: number = 0, yEnd: number = y + this.camera.screenTileCapacity.height; y < yEnd; y++, yCounter++) {
  1802.  
  1803.                     if (world.validPos(x, y)) {
  1804.  
  1805.                         this.drawSprite(world.getPos(x, y, PastryCore.WorldLayer.backGround), this.camera.tilePos.x + xCounter * PastryCore.SpriteManager.textureLength, this.camera.tilePos.y + yCounter * PastryCore.SpriteManager.textureLength);
  1806.  
  1807.                         if(world.getPos(x, y, PastryCore.WorldLayer.foreGround) !== SpriteIndexs.none) {
  1808.  
  1809.                             this.drawSprite(world.getPos(x, y, PastryCore.WorldLayer.foreGround), this.camera.tilePos.x + xCounter * PastryCore.SpriteManager.textureLength, this.camera.tilePos.y + yCounter * PastryCore.SpriteManager.textureLength);
  1810.  
  1811.                         }
  1812.  
  1813.                     }
  1814.                    
  1815.                 }
  1816.  
  1817.             }
  1818.  
  1819.         }
  1820.  
  1821.         private drawPlayerEntity(currentPlayer: Player, screenCentered: boolean) {
  1822.  
  1823.             if (screenCentered) {
  1824.  
  1825.                 this.drawText(currentPlayer.name, this.camera.spriteCentered.x + 8, this.camera.spriteCentered.y - 9, true);
  1826.                 this.drawSprite(currentPlayer.currentAnimationFrame, this.camera.spriteCentered.x, this.camera.spriteCentered.y);
  1827.  
  1828.             } else {
  1829.  
  1830.                 this.drawText(currentPlayer.name, currentPlayer.bounds.min.x + 8, currentPlayer.bounds.min.y - 9, true);
  1831.                 this.drawSprite(currentPlayer.currentAnimationFrame, currentPlayer.bounds.min.x, currentPlayer.bounds.min.y);
  1832.  
  1833.             }
  1834.  
  1835.         }
  1836.  
  1837.         private drawSprite(spriteIdentifier: SpriteIndexs, x: number, y: number): void {
  1838.  
  1839.             this.bufferContext.drawImage(
  1840.  
  1841.                 this.textureManager.spriteSheetCanvas,
  1842.  
  1843.                 spriteIdentifier * this.textureManager.textureScale, 0,
  1844.  
  1845.                 this.textureManager.textureLength, this.textureManager.textureLength,
  1846.  
  1847.                 x * this.textureManager.textureScale,
  1848.                 y * this.textureManager.textureScale,
  1849.  
  1850.                 this.textureManager.textureLength, this.textureManager.textureLength
  1851.  
  1852.             );
  1853.  
  1854.  
  1855.         }
  1856.  
  1857.         private drawText(text: string, x: number, y: number, centered: boolean): void {
  1858.  
  1859.             this.bufferContext.font = (4 * this.textureManager.textureScale) + 'pt Calibri';
  1860.  
  1861.             if (centered) {
  1862.                 this.bufferContext.textAlign = 'center';
  1863.             } else {
  1864.                 this.bufferContext.textAlign = 'left';
  1865.             }
  1866.  
  1867.  
  1868.             this.bufferContext.fillStyle = 'white';
  1869.             this.bufferContext.fillText(text, x * this.textureManager.textureScale, y * this.textureManager.textureScale);
  1870.  
  1871.  
  1872.         }
  1873.  
  1874.     }
  1875.  
  1876. }
  1877.  
  1878. interface PastryFrameWork {
  1879.  
  1880.     VERSION: number;
  1881.  
  1882.     engine: PastryCore.Engine;
  1883.     timeManager: PastryCore.TimeManager;
  1884.     inputManager: PastryCore.InputManager;
  1885.     gameManager: PastryCore.GameManager;
  1886.     entityManager: PastryCore.EntityManager;
  1887.     renderer: PastryCore.Renderer;
  1888.     worldManager: PastryCore.WorldManager;
  1889.  
  1890.     main(): void;
  1891.     loop(): void;
  1892.     resourcesLoaded(): void;
  1893.  
  1894. }
  1895.  
  1896. var PASTRY: PastryFrameWork = {
  1897.  
  1898.     VERSION: 1,
  1899.  
  1900.     engine:         new PastryCore.Engine(),
  1901.     timeManager:    new PastryCore.TimeManager(),
  1902.     inputManager:   new PastryCore.InputManager(),
  1903.     gameManager:    new PastryCore.GameManager(),
  1904.     renderer:       new PastryCore.Renderer(),
  1905.     entityManager:  new PastryCore.EntityManager(),
  1906.     worldManager:   new PastryCore.WorldManager(),
  1907.  
  1908.     main: function (): void {
  1909.  
  1910.         PASTRY.renderer.init();
  1911.  
  1912.     },
  1913.  
  1914.     resourcesLoaded: function (): void {
  1915.  
  1916.         PastryCore.Engine.log("Resources loaded!");
  1917.  
  1918.         PASTRY.inputManager.init();
  1919.  
  1920.         window.onresize = () => {
  1921.  
  1922.             PASTRY.inputManager.onResize();
  1923.             PASTRY.renderer.resize();
  1924.  
  1925.         };
  1926.  
  1927.         PASTRY.renderer.resize();
  1928.  
  1929.         PASTRY.worldManager.setWorld(245 ,1000, 1000);
  1930.  
  1931.         PASTRY.loop();
  1932.  
  1933.     },
  1934.  
  1935.     loop: function (): void {
  1936.  
  1937.         PASTRY.timeManager.update();
  1938.         PASTRY.inputManager.update();
  1939.         PASTRY.renderer.refreshBuffer();
  1940.  
  1941.         switch(PASTRY.gameManager.currentState) {
  1942.            
  1943.             case PastryCore.GameStates.INGAME:
  1944.  
  1945.                 PASTRY.entityManager.update(PASTRY.worldManager.currentWorld);
  1946.  
  1947.                 PASTRY.renderer.drawInGame(PASTRY.entityManager, PASTRY.worldManager.currentWorld);
  1948.                 break;
  1949.  
  1950.             case PastryCore.GameStates.LOADING:
  1951.  
  1952.  
  1953.  
  1954.                 break;
  1955.  
  1956.             case PastryCore.GameStates.MAIN:
  1957.  
  1958.  
  1959.  
  1960.                 break;
  1961.  
  1962.             case PastryCore.GameStates.PAUSED:
  1963.  
  1964.  
  1965.            
  1966.                 break;
  1967.         }
  1968.        
  1969.         PASTRY.renderer.render();
  1970.  
  1971.         requestAnimationFrame(PASTRY.loop);
  1972.     }
  1973. };
  1974.  
  1975. PASTRY.main();
Advertisement
Add Comment
Please, Sign In to add comment