123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- include("mouseinput.js");
- include("camera.js");
- include("background.js");
- include("headsupdisplay.js");
- include("curtain.js");
- include("slotreel.js");
- function SlotMachine() {
- this.game = null;
- this.system = null;
- this.canvas = null;
- this.mouse = null;
- this.camera = null;
- this.background = null;
- this.reels = [];
- this.spinButton = null;
- this.waitingToCalculate = false;
- this.prize = 20;
- this.prizeTextPosition = 1024;
- this.prizeTextFontSize = 72;
- this.curtain = null;
- this.isStarted = false;
- this.init = function(systemObject, canvas) {
- this.game = this;
- this.canvas = canvas;
- this.system = systemObject;
- system.assets.buildSprite("slotreel", "slotreel", {x:0, y:0}, {width: 256, height: 2048});
- this.mouse = new MouseInput();
- this.mouse.attach(canvas, this);
- canvas.style.cursor = "default";
- this.camera = new Camera();
- this.camera.init(canvas);
- this.background = new Background(this.camera);
- this.background.init();
- this.reels = [];
- this.reels[0] = new SlotReel();
- this.reels[1] = new SlotReel();
- this.reels[2] = new SlotReel();
- for(var reelIndex in this.reels) {
- var reel = this.reels[reelIndex];
- reel.init({x: (256 * reelIndex) + 32 + (16 * reelIndex), y: 32}, parseInt(reelIndex));
- }
- this.spinButton = new Button(canvas);
- this.spinButton.addClickEvent(this.spinReels);
- this.prize = 20;
- this.ui = new HeadsUpDisplay(this.game, this.camera);
- this.ui.init(canvas);
- var stage = this;
- this.curtain = new Curtain();
- this.curtain.init(systemObject, canvas);
- this.curtain.open(700, function() {
- stage.isStarted = true;
- });
- this.handleResize(canvas);
- };
- this.end = function(callback) {
- this.mouse.detatch();
- this.closeCurtain(callback);
- }
- this.closeCurtain = function(callback) {
- system.theater.stage.curtain.close(700, function() {
- callback();
- });
- }
- this.updateDelta = function(delta, canvas){
- this.camera.update(canvas, this.player, delta);
- this.ui.update(delta);
- this.update(canvas);
- this.curtain.updateDelta(delta);
- };
- this.update = function(canvas) {
- this.background.update(canvas);
- if(system.keyboard.isPressed(Keys.F1)) {
- system.theater.changeStage("mainmenu");
- }
- if(system.keyboard.isPressed(Keys.F2)) {
- system.theater.changeStage("end");
- }
- if(system.keyboard.isPressed(Keys.Space) || system.keyboard.isPressed(Keys.Enter)) {
- this.spinReels();
- }
- for(var reelIndex in this.reels) {
- var reel = this.reels[reelIndex];
- reel.update();
- }
- var spinResults = [];
- for(var reelIndex in this.reels) {
- var reel = this.reels[reelIndex];
- if(!reel.isSpinning) {
- spinResults.push(reel.spinTarget);
- }
- }
- if(this.waitingToCalculate && spinResults.length == 3) {
- if(spinResults[0] == spinResults[1]) {
- if(spinResults[0] == spinResults[2]) {
- this.prize += 50;
- } else {
- this.prize += 20;
- }
- } else if(spinResults[0] == spinResults[2]) {
- this.prize += 20;
- } else if(spinResults[1] == spinResults[2]) {
- this.prize += 20;
- }
- this.waitingToCalculate = false;
- if(this.prize > 100) {
- system.theater.changeStage("end");
- }
- }
- };
- this.draw = function(context) {
- context.save();
- context.translate(this.camera.position.x, this.camera.position.y);
- this.background.draw(context);
- for(var reelIndex in this.reels) {
- var reel = this.reels[reelIndex];
- reel.draw(context);
- }
- this.spinButton.draw(context);
- context.font = this.prizeTextFontSize + "px sans-serif";
- context.textAlign = "right";
- context.textBaseline = "middle";
- context.lineWidth = 2;
- context.fillStyle = "black";
- context.strokeStyle = "black";
- context.fillText("$" + this.prize, this.prizeTextPosition, 64);
- context.restore();
- this.ui.draw(context);
- this.curtain.draw(context);
- };
- this.handleResize = function(canvas) {
- this.prizeTextPosition = 1024;
- this.prizeTextFontSize = 72;
- this.camera.handleResize(canvas);
- var buttonOptions = {position: {x: 1024, y: 512}, font:{color:"#DDDDDD", stroke: "#333333", size: 72, family: "sans-serif"}};
- if(this.canvas.width < 768) {
- buttonOptions.font.size = 36;
- buttonOptions.position.x *= 0.5;
- buttonOptions.position.y *= 0.5;
- this.prizeTextPosition *= 0.5;
- this.prizeTextFontSize = 36;
- }
- this.spinButton.init("Spin", buttonOptions);
- for(var reelIndex in this.reels) {
- this.reels[reelIndex].handleResize(canvas);
- }
- this.ui.handleResize(canvas);
- }
- this.mouseMove = function(canvas, x, y) {
- this.ui.mouseMove(canvas, x, y);
- this.spinButton.mouseMove(x, y);
- };
- this.mouseDown = function(canvas, button, x, y) {
- this.ui.mouseDown(canvas, button, x, y);
- this.spinButton.mouseDown(button, x, y);
- };
- this.mouseUp = function(canvas, button, x, y) {
- this.ui.mouseUp(canvas, button, x, y);
- };
- this.touchStart = function(canvas, x, y) {
- this.ui.touchStart(canvas, x, y);
- this.spinButton.touchStart(x, y);
- };
- this.touchMove = function(canvas, x, y) {
- this.ui.touchMove(canvas, x, y);
- this.spinButton.touchMove(x, y);
- };
- this.touchEnd = function(canvas, x, y) {
- this.ui.touchEnd(canvas, x, y);
- };
- this.contextMenu = function(canvas, event) {
- };
- this.spinReels = function() {
- system.theater.stage.prize--;
- var reels = system.theater.stage.reels;
- for(var reelIndex in reels) {
- var reel = reels[reelIndex];
- if(reel.isSpinning) {
- return;
- }
- }
- for(var reelIndex in reels) {
- var reel = reels[reelIndex];
- reel.startSpin();
- }
- system.theater.stage.waitingToCalculate = true;
- }
- };
|