Skip to content

Instantly share code, notes, and snippets.

@particle4dev
Last active August 29, 2015 14:21
Show Gist options
  • Save particle4dev/27f7e63f465ad155485b to your computer and use it in GitHub Desktop.
Save particle4dev/27f7e63f465ad155485b to your computer and use it in GitHub Desktop.
html-game
"use strict";
var Cloud = function(_level, _spritesheet) {
Phaser.Sprite.call(this, _level.game, 0, 0, _spritesheet), _level.game.add.existing(this), this.level = _level, this.anchor.setTo(.5, .5), this.anglePos = 0, this.angleRad = 0, this.ySky = 0, this.planet = _level.planet, this.direction = 1, this.speed = .001
};
Cloud.prototype = Object.create(Phaser.Sprite.prototype), Cloud.prototype.constructor = Cloud, Cloud.prototype.update = function() {
var planetScale = this.planet.scale.x;
this.scale.setTo(planetScale, planetScale), this.anglePos += this.direction * this.speed * this.level.game.time.elapsed, this.angleRad = this.anglePos * (3.14159 / 180);
var realRadius = .5 * this.planet.width + .5 * this.height - 15 * planetScale;
realRadius += this.ySky * planetScale, this.x = this.planet.x + realRadius * Math.cos(this.angleRad), this.y = this.planet.y + realRadius * Math.sin(this.angleRad), this.angle = this.anglePos + 90
};
"use strict";
function createClouds(_this) {
_this.clouds = _this.level.game.add.group(), createCloud(_this, _this.level, 0, "cloud1", 1, 120), createCloud(_this, _this.level, 270, "cloud1", -.1, 79), createCloud(_this, _this.level, 90, "cloud2", 0, 300), createCloud(_this, _this.level, 190, "cloud2", -1, 200), createCloud(_this, _this.level, 300, "cloud3", 1, 30), createCloud(_this, _this.level, 120, "cloud3", -1, 10)
}
function createCloud(_this, _level, _angle, _name, _direction, _baseYSky) {
var cloud = new Cloud(_level, _name), r = _baseYSky + 250 * Math.random();
cloud.ySky = r, cloud.direction = _direction, cloud.anglePos = _angle, cloud.speed = .002 * Math.random(), _this.clouds.add(cloud)
}
var CloudManager = function(_level) {
this.level = _level, this.planet = _level.planet, createClouds(this)
};
CloudManager.preload = function(_game) {
_g_gameame.load.image("cloud1", "assets/images/cloud_1.png"), _game.load.image("cloud2", "assets/images/cloud_2.png"), _game.load.image("cloud3", "assets/images/cloud_3.png")
};
"use strict";
function DOMishParser() {
}
function DOMishAttributes() {
}
function makeDOMishObject(data) {
return new DOMishObject(data)
}
function DOMishObject(data) {
this.attributes = this.convertContent(data), this.length = Object.keys(this.attributes).length
}
DOMishParser.prototype.parseFromString = function(data) {
return new DOMishObject(JSON.parse(data))
}, DOMishAttributes.prototype.getNamedItem = function(name) {
return {nodeValue: this[name] || null}
}, DOMishObject.prototype.documentElement = document, DOMishObject.prototype.convertContent = function(obj) {
var prop, attributes = new DOMishAttributes;
for (prop in obj)
attributes[prop] = null !== obj[prop] && "object" == typeof obj[prop] ? Array.isArray(obj[prop]) ? obj[prop].map(makeDOMishObject) : new DOMishObject(obj[prop]) : obj[prop];
return attributes
}, DOMishObject.prototype.getElementsByTagName = function(name) {
var array = this.attributes[name] ? Array.isArray(this.attributes[name]) ? this.attributes[name] : [this.attributes[name]] : [];
return array
}, DOMishObject.prototype.getAttribute = function(name) {
return this.attributes[name] || null
};
Dust = function(_level) {
Phaser.Sprite.call(this, _level.game, 0, 0, "dust"), _level.game.add.existing(this), this.blowAnim = this.animations.add("blow", [0, 1, 2, 3, 4]), this.isBlowing = !1, this.anchor.setTo(.5, .5), this.level = _level, this.anglePos = 0, this.planet = _level.planet
}, Dust.prototype = Object.create(Phaser.Sprite.prototype), Dust.prototype.constructor = Dust, Dust.prototype.blow = function(_item) {
this.animations.play("blow", 5, !1), this.visible = !0, this.isBlowing = !0, this.anglePos = _item.anglePos, this.angle = _item.angle
}, Dust.prototype.update = function() {
if (this.isBlowing) {
var planetScale = this.planet.scale.x;
this.scale.setTo(planetScale, planetScale), this.angleRad = this.anglePos * (3.14159 / 180);
var realRadius = .5 * this.planet.width + .5 * this.height - 35 * planetScale;
this.x = this.planet.x + realRadius * Math.cos(this.angleRad), this.y = this.planet.y + realRadius * Math.sin(this.angleRad), this.blowAnim.isPlaying || (this.isBlowing = !1, this.visible = !1)
}
};
"use strict";
var Item = function(_level, _spritesheet, _layer, _player, _instance) {
var obj = this;
null != _instance && (obj = _instance), Phaser.Sprite.call(obj, _level.game, -1e3, 0, _spritesheet), _level.game.add.existing(obj), obj.level = _level, obj.player = _player, obj.frame = 0, obj.anchor.setTo(.5, .5), obj.onGround = !0, obj.anglePos = 0, obj.angleRad = 0, obj.bubbleInfo = null, obj.planet = _level.planet, obj.offsetY = 15, obj.layer = _layer, null == obj.layer && (obj.layer = 0)
};
Item.prototype = Object.create(Phaser.Sprite.prototype), Item.prototype.constructor = Item, Item.prototype.update = function() {
var planetScale = this.planet.scale.x;
this.scale.setTo(planetScale, planetScale), this.angleRad = this.anglePos * (3.14159 / 180);
var realRadius = .5 * this.planet.width + .5 * this.height - this.offsetY * planetScale;
this.x = realRadius * Math.cos(this.angleRad), this.y = realRadius * Math.sin(this.angleRad), this.angle = this.anglePos + 90, this.alpha = this.layer > this.level.player.layer + 1 ? .5 : 1
};
LabelButton = function(game, x, y, key, label, fontSize, callback, callbackContext, overFrame, outFrame, downFrame, upFrame) {
Phaser.Button.call(this, game, x, y, key, callback, callbackContext, overFrame, outFrame, downFrame, upFrame), this.label = this.game.add.bitmapText(0, 0, "hungryrex_font1", "label", fontSize), this.addChild(this.label), this.setLabel(label)
}, LabelButton.prototype = Object.create(Phaser.Button.prototype), LabelButton.prototype.constructor = LabelButton, LabelButton.prototype.setLabel = function(label) {
this.label.text = label;
var height = this.label.fontSize, width = .5 * height * this.label.text.length;
this.label.x = Math.floor(this.width * (.5 - this.anchor.x) - .5 * width), this.label.y = Math.floor(this.height * (.5 - this.anchor.y) - .5 * height)
};
function createItems(_this, _jsonArray, _name, _layer, _offsetY) {
if (null == _jsonArray)
return void console.warn("no " + _name + " found in this level");
for (var i = 0; i < _jsonArray.length; i++) {
var item = new Item(_this.level, _name, _layer, _this.player);
item.name = _name + i, item.anglePos = _jsonArray[i].angle, item.offsetY = _offsetY, _this.level.items.add(item), null == _this.types[_layer] && (_this.types[_layer] = 0), _this.types[_layer]++
}
}
LevelLoader = function(_level, _player) {
this.level = _level, this.player = _player, this.types = new Array
}, LevelLoader.prototype.loadLevel = function(_name) {
this.json = JSON.parse(this.level.game.cache.getText(_name)), createItems(this, this.json.mountains, "mountain", 3, 30), createItems(this, this.json.houses, "house", 2, 15), createItems(this, this.json.cars, "car", 1, 15), createItems(this, this.json.bushes, "bush", 0, 15), this.createTriggers(this.json.triggers)
}, LevelLoader.prototype.createTriggers = function(_jsonArray) {
if (null == _jsonArray)
return void console.warn("no triggers found in this level");
for (var i = 0; i < _jsonArray.length; i++) {
var trigger = new Trigger(this.level, this.player);
trigger.anglePos = _jsonArray[i].angle, trigger.callback = _jsonArray[i].callback, trigger.offsetY = 100, this.level.triggers.add(trigger)
}
};
"use strict";
var WIDTH = window.innerWidth, HEIGHT = window.innerHeight, STATES = new Object, CURRENT_LEVEL = 1, PLAYER, OVERRIDE_XML = !0;
1 == OVERRIDE_XML && (window.DOMParser = DOMishParser), window.onload = function() {
var game = new Phaser.Game(WIDTH, HEIGHT, Phaser.AUTO, "hungryrex");
STATES.boot = new Boot(game), STATES.loader = new Loader(game), STATES.landing = new Landing(game), STATES.level = new Level(game), STATES.selection = new SelectionState(game), game.state.start("Boot")
};
var musicPath = "assets/audio/music/";
fxPath = "assets/audio/fx/", MusicManager = function(_game) {
this.musicMain, this.bitesFX = new Array, this.isPlaying = !1, this.game = _game, this.musicMain = this.game.add.audio("main_theme"), this.taiko = this.game.add.audio("taiko");
for (var i = 0; 4 > i; i++)
this.bitesFX.push(this.game.add.audio("bite" + i));
this.bump = this.game.add.audio("bump"), this.level = 0
}, MusicManager.preload = function(_game) {
_game.load.audio("main_theme", musicPath + "sweet_rampage.ogg"), _game.load.audio("taiko", musicPath + "taiko.ogg");
for (var i = 0; 4 > i; i++)
_game.load.audio("bite" + i, fxPath + "bite" + (i + 1) + ".wav");
_game.load.audio("bump", fxPath + "bump.ogg")
}, MusicManager.prototype.play = function() {
this.isPlaying || (this.musicMain.play("", 0, 1, !0, !0), this.taiko.play("", .1, 0, !0, !0)), this.isPlaying = !0
}, MusicManager.prototype.stop = function() {
this.isPlaying = !1, this.musicMain.stop(), this.taiko.stop()
}, MusicManager.prototype.pause = function() {
this.isPlaying = !1, this.musicMain.pause("", 2)
}, MusicManager.prototype.playBite = function(_level) {
_level >= this.bitesFX.length || this.bitesFX[_level].play("", 0, .45)
}, MusicManager.prototype.playBump = function() {
this.bump.play()
}, MusicManager.prototype.nextLevel = function() {
switch (this.level++, this.level) {
case 1:
this.musicMain.volume = .9, this.taiko.volume = .15;
break;
case 2:
this.musicMain.volume = .85, this.taiko.volume = .3;
break;
case 3:
this.musicMain.volume = .75, this.taiko.volume = .6
}
};
"use strict";
var Player = function(_level) {
Phaser.Sprite.call(this, _level.game, 0, 0, "player_sheet"), _level.game.add.existing(this), this.level = _level, this.pause = !1, this.frame = 0, this.anchor.setTo(.5, .5), this.onGround = !0, this.isRolling = !1, this.isWinning = !1, this.wait = 0, this.anglePos = 270, this.rollingAngle = 0, this.angleRad = 0, this.angleSpeed = .02, this.isJumping = !1, this.currentJumpHeight = 0, this.jumpMaxHeight = 80, this.jumpPower = .175, this.upForce = 0, this.realRadius = 0, this.gravityForce = 3e-4, _level.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR).onDown.add(this.onJumpPressed, this), _level.game.input.keyboard.addKey(Phaser.Keyboard.P).onDown.add(this.onPause, this), _level.game.input.onDown.add(this.justPressed, this), _level.game.input.onUp.add(this.justReleased, this), this.layer = 0, this.walkAnim = this.animations.add("walk", [0, 1, 2, 3, 2, 1]), this.biteAnim = this.animations.add("bite", [4, 0]), this.animations.play("walk", 5, !0), this.food = 0, this.isBiting = !1, this.pointerTimestamp = 0, this.deltaPointer = new Phaser.Point, this.touchRexPos = new Phaser.Point
};
Player.prototype = Object.create(Phaser.Sprite.prototype), Player.prototype.constructor = Player, Player.GESTURE_MIN_DURATION = 25, Player.GESTURE_MAX_DURATION = 300, Player.GESTURE_DELTA_ANGLE_MAX = 25, Player.prototype.move = function() {
var planetScale = this.level.planet.scale.x;
this.pause || (this.anglePos += this.angleSpeed * (2 - planetScale) * this.game.time.elapsed), this.anglePos < 0 && (this.anglePos += 360), this.anglePos = this.anglePos % 360, this.angleRad = this.anglePos * (3.14159 / 180), this.realRadius = .5 * (this.level.planet.width + this.height) + this.currentJumpHeight - 15, this.x = this.level.planet.x + this.realRadius * Math.cos(this.angleRad), this.y = this.level.planet.y + this.realRadius * Math.sin(this.angleRad), this.angle = this.anglePos + 90, this.isRolling === !0 && (this.angleSpeed >= 0 ? (this.angleSpeed = 0, this.wait += .001 * this.game.time.elapsed, this.wait >= .2 && this.game.state.start("Selection")) : (this.rollingAngle -= .32 * this.game.time.elapsed % 360, this.angleSpeed += 15e-6 * this.game.time.elapsed), this.angle += this.rollingAngle), this.isWinning === !0 && (this.onGround === !0 && this.onJumpPressed(), this.wait += .001 * this.game.time.elapsed, this.wait >= 1 && null == this.btnPlay && (this.btnPlay = this.game.add.button(0, 0, "play", function() {
this.game.state.start("Selection")
}, this, 1, 0, 1), this.btnPlay.anchor.setTo(.5, .5), this.btnPlay.fixedToCamera = !0, this.btnPlay.cameraOffset.x = .5 * this.game.camera.width, this.btnPlay.cameraOffset.y = .75 * this.game.camera.height))
}, Player.prototype.stop = function() {
}, Player.prototype.update = function() {
this.move(), this.isJumping === !0 && this.pause === !1 ? this.doJumpUpdate() : this.onGround || this.pause !== !1 || this.applyGravity(), this.isBiting && this.doBitingUpdate()
}, Player.prototype.applyGravity = function() {
this.upForce += this.gravityForce * this.game.time.elapsed, this.currentJumpHeight -= this.upForce * this.game.time.elapsed, this.currentJumpHeight < 0 && (this.currentJumpHeight = 0, this.onGround = !0)
}, Player.prototype.onJumpPressed = function() {
this.onGround === !0 && this.isRolling === !1 && (this.isJumping = !0, this.onGround = !1, this.upForce = 0)
}, Player.prototype.doJumpUpdate = function() {
this.currentJumpHeight += this.jumpPower * this.game.time.elapsed, this.currentJumpHeight >= this.jumpMaxHeight && (this.isJumping = !1)
}, Player.prototype.bite = function() {
this.isBiting = !0, this.animations.play("bite", 8, !1)
}, Player.prototype.doBitingUpdate = function() {
this.biteAnim.isPlaying || (this.animations.play("walk", 5, !0), this.isBiting = !1)
}, Player.prototype.onPause = function() {
this.pause = !this.pause
}, Player.prototype.justPressed = function(_pointer) {
0 == this.pointerTimestamp && (this.pointerX = _pointer.x, this.pointerY = _pointer.y, this.pointerTimestamp = (new Date).getTime())
}, Player.prototype.justReleased = function(_pointer) {
var duration = (new Date).getTime() - this.pointerTimestamp;
if (duration > Player.GESTURE_MIN_DURATION && duration < Player.GESTURE_MAX_DURATION) {
var oldPos = new Phaser.Point(this.pointerX, this.pointerY), newPos = new Phaser.Point(_pointer.x, _pointer.y);
if (1 == this.tapped(oldPos, newPos))
this.level.checkItemsToEat();
else {
var deltaY = _pointer.y - this.pointerY, deltaX = _pointer.x - this.pointerX;
if (this.deltaPointer.setTo(deltaX, deltaY), this.deltaPointer.getMagnitude() > 50) {
this.deltaPointer.normalize(), this.touchRexPos.setTo(this.x - this.level.planet.x, this.y - this.level.planet.y).normalize();
var anglePos = Math.acos(this.touchRexPos.x);
this.touchRexPos.y < 0 && (anglePos += Math.PI), anglePos = 180 * anglePos / Math.PI;
var angle = Math.acos(this.deltaPointer.x);
this.deltaPointer.y < 0 && (angle += Math.PI), angle = 180 * angle / Math.PI;
var deltaAngle = Math.abs(anglePos - angle), dot = this.deltaPointer.dot(this.touchRexPos);
deltaAngle < Player.GESTURE_DELTA_ANGLE_MAX && dot >= 0 && this.onJumpPressed()
}
}
}
this.pointerTimestamp = 0
}, Player.prototype.tapped = function(_oldPos, _newPos) {
var tapped = !1;
return _oldPos.distance(_newPos) < 40 && (tapped = !0), tapped
};
"use strict";
var Popup = function(_level, _backgroundImg) {
this.level = _level, this.planet = this.level.planet, this.x = this.planet.x, this.y = this.planet.y;
var backgroundImg = "tutorial_miam";
_backgroundImg && (backgroundImg = _backgroundImg), this.background = new Phaser.Sprite(this.level.game, 0, 0, backgroundImg, 0), this.level.game.add.existing(this.background), this.background.anchor.setTo(.5, .5), this.background.fixedToCamera = !0, this.background.cameraOffset.x = .5 * this.level.game.camera.width, this.background.cameraOffset.y = .5 * this.level.game.camera.height, this.btnClose = new Phaser.Button(this.level.game, 300, 200, "croix", this.close, this, 0, 0, 0), this.level.game.add.existing(this.btnClose), this.btnClose.anchor.setTo(.5, .5), this.btnClose.fixedToCamera = !0, this.btnClose.cameraOffset.x = this.background.cameraOffset.x + .5 * this.background.width, this.btnClose.cameraOffset.y = this.background.cameraOffset.y - .5 * this.background.height
};
Popup.prototype.close = function() {
this.background.destroy(), this.btnClose.destroy(), this.level.player.pause = !1
};
"use strict";
var Boot = function(_game) {
Phaser.State.call(this, _game), _game.state.add("Boot", this, !1)
};
Boot.prototype = Object.create(Phaser.State.prototype), Boot.prototype.constructor = Boot, Boot.prototype.preload = function() {
this.game.stage.backgroundColor = "#66543d", this.game.load.spritesheet("loading", "assets/images/loading.png", 128, 128)
}, Boot.prototype.create = function() {
this.game.state.start("Loader")
}, Boot.prototype.update = function() {
}, Boot.prototype.render = function() {
};
"use strict";
var Landing = function(_game) {
Phaser.State.call(this, _game), _game.state.add("Landing", this, !1)
};
Landing.prototype = Object.create(Phaser.State.prototype), Landing.prototype.constructor = Landing, Landing.prototype.preload = function() {
}, Landing.prototype.create = function() {
this.game.stage.backgroundColor = "#66543d", this.game.world.setBounds(0, 0, window.innerWidth, window.innerHeight), this.background = this.game.add.sprite(0, 0, "landing"), this.background.anchor.setTo(.5, .5), this.background.x = .5 * window.innerWidth, this.background.y = .5 * window.innerHeight, this.play = this.game.add.button(0, 0, "play", function() {
this.game.state.start("Selection")
}, this, 1, 0, 1), this.play.anchor.setTo(.5, .5), this.play.x = this.background.x, this.play.y = this.background.y + 20, this.rex = this.game.add.sprite(0, 0, "player_sheet"), this.rex.anchor.setTo(.5, .5), this.rex.x = this.background.x + 5, this.rex.y = this.background.y + 132, this.rex.animations.add("walk", [0, 1, 2, 3, 2, 1], 7, !0), this.rex.animations.play("walk", 7, !0)
}, Landing.prototype.update = function() {
this.game.camera.focusOnXY(this.background.x, this.background.y)
}, Landing.prototype.render = function() {
};
"use strict";
function onMute() {
this.game.sound.mute = !this.game.sound.mute, this.game.sound.mute ? this.muteButton.setFrames(1, 1, 0) : this.muteButton.setFrames(0, 0, 1)
}
function getFreeDust(_this) {
for (var i = 0; i < _this.dustArray.length; i++)
if (_this.dustArray[i].isBlowing === !1)
return _this.dustArray[i];
return null
}
function createBubbles(_this) {
_this.bubbles = new Array;
for (var i = 0; 15 > i; i++)
_this.bubbles.push(new BubbleInfo(_this))
}
function respawnBubbles(_this) {
deactivateBubbles(_this);
for (var i = 0; i < _this.items.length; i++) {
var bubble, item = _this.items.getAt(i);
item.layer == _this.player.layer ? (bubble = getFreeBubble(_this), bubble.activate(item, 1)) : item.layer == _this.player.layer + 1 && (bubble = getFreeBubble(_this), bubble.activate(item, 0))
}
}
function deactivateBubbles(_this) {
for (var i = 0; i < _this.bubbles.length; i++)
_this.bubbles[i].deactivate();
return null
}
function getFreeBubble(_this) {
for (var i = 0; i < _this.bubbles.length; i++)
if (_this.bubbles[i].active === !1)
return _this.bubbles[i];
return null
}
var Level = function(_game) {
Phaser.State.call(this, _game), _game.state.add("Level", this, !1), this.planetSprite, this.muteButton, this.dustArray, this.bubbles
};
Level.prototype = Object.create(Phaser.State.prototype), Level.prototype.constructor = Level, Level.DISTANCE_EAT_MAX = 150, Level.prototype.preload = function() {
this.game.load.text("level", "assets/data/json/level" + CURRENT_LEVEL + ".json")
}, Level.prototype.create = function() {
_gaq.push(["_trackEvent", "Game", "Launch", "Level", CURRENT_LEVEL]), this.game.lastLevelPlayed = CURRENT_LEVEL, this.game.lastLevelFinished = !1, this.game.stage.backgroundColor = "#accedd", this.scale = 1, this.scaleTo = 1, this.planet = this.game.add.sprite(.5 * (WIDTH - 1e3 * this.scale), .5 * (HEIGHT - 1e3 * this.scale), "planet"), this.planet.anchor.setTo(.5, .5), this.planet.x = 1e3, this.planet.y = 1e3, this.items = this.game.add.group(), this.items.x = this.planet.x, this.items.y = this.planet.y, this.triggers = this.game.add.group(), this.triggers.x = this.planet.x, this.triggers.y = this.planet.y, this.dustArray = new Array;
for (var i = 0; 5 > i; i++)
this.dustArray.push(new Dust(this));
PLAYER = new Player(this), this.player = PLAYER, this.cursors = this.game.input.keyboard.createCursorKeys(), this.game.world.setBounds(0, 0, 2e3, 2e3), this.musicManager = new MusicManager(this.game), this.musicManager.play(), this.levelLoader = new LevelLoader(this, this.player), this.levelLoader.loadLevel("level"), this.cloudManager = new CloudManager(this), createBubbles(this), respawnBubbles(this), this.foodgaugeBack = this.game.add.sprite(0, 0, "foodgauge"), this.foodgaugeBack.frame = 0, this.foodgaugeBack.anchor.setTo(.5, .5), this.foodgaugeBack.fixedToCamera = !0, this.foodgaugeBack.cameraOffset.setTo(36, .5 * HEIGHT), this.foodgaugeFront = this.game.add.sprite(0, 0, "foodgauge"), this.foodgaugeFront.frame = 1, this.foodgaugeFront.cropEnabled = !0, this.foodgaugeFrontRect = new Phaser.Rectangle(28, 0, 28, 10), this.foodgaugeFront.crop(this.foodgaugeFrontRect), this.foodgaugeFront.anchor.setTo(.5, .5), this.foodgaugeFront.fixedToCamera = !0, this.foodgaugeFront.cameraOffset.setTo(36, .5 * HEIGHT), this.muteButton = this.game.add.button(WIDTH, 0, "mute_sheet", onMute, this, 0, 0, 1), this.game.sound.mute && this.muteButton.setFrames(1, 1, 0), this.muteButton.fixedToCamera = !0, this.muteButton.anchor.setTo(.5, .5), this.muteButton.cameraOffset.setTo(36, .5 * HEIGHT + 132 + 26 + 10)
}, Level.prototype.update = function() {
this.planet.scale.setTo(this.scale, this.scale);
var gaugeHeight = 265 * (this.player.food / this.levelLoader.types[this.player.layer]);
if (this.foodgaugeFront.cameraOffset.setTo(36, .5 * (HEIGHT + 265 - gaugeHeight)), this.foodgaugeFrontRect.setTo(28, 265, 28, -gaugeHeight), this.foodgaugeFront.crop(this.foodgaugeFrontRect), this.player.isRolling === !1) {
for (var nbItems = this.items.total, i = nbItems - 1; i >= 0; i--) {
var item = this.items.getAt(i), pAngle = this.player.anglePos, offsetAngle = 5 * (2 - this.planet.scale.x), lowAngle = item.anglePos - offsetAngle, highAngle = item.anglePos + offsetAngle;
if (pAngle > lowAngle && highAngle > pAngle) {
var height = this.player.realRadius, offset = .5 * this.planet.width + 62;
offset > height && item.layer == this.player.layer + 1 && (this.player.isRolling = !0, this.player.angleSpeed *= -1, this.musicManager.playBump(), this.musicManager.stop())
}
}
for (var nbTriggers = this.triggers.total, i = nbTriggers - 1; i >= 0; i--) {
var trigger = this.triggers.getAt(i), pAngle = this.player.anglePos, offsetAngle = 5 * (2 - this.planet.scale.x), lowAngle = trigger.anglePos - offsetAngle, highAngle = trigger.anglePos + offsetAngle;
if (pAngle > lowAngle && highAngle > pAngle) {
var height = this.player.realRadius, offset = .5 * this.planet.width + 62;
offset > height && (trigger.doCallback(), trigger.destroy())
}
}
}
if (this.scale > this.scaleTo)
this.scale -= .001 * this.game.time.elapsed * .5, this.game.camera.focusOnXY(this.player.x, this.player.y);
else if (this.scale = this.scaleTo, this.scale <= .25) {
if (null == this.originX && (this.originX = this.player.x, this.originY = this.player.y, this.elapsedTime = 0), this.elapsedTime < 1) {
var dx = this.originX + (this.planet.x - this.originX) * this.elapsedTime, dy = this.originY + (this.planet.y - this.originY) * this.elapsedTime;
this.game.camera.focusOnXY(dx, dy), this.elapsedTime += .001 * this.game.time.elapsed
}
} else
this.player.isWinning === !1 ? this.game.camera.focusOnXY(this.player.x, this.player.y) : this.moveCameraToPlanet();
if (0 == this.items.total && this.player.isWinning === !1 && (this.musicManager.stop(), this.player.isWinning = !0, this.player.angleSpeed = 0, this.player.loadTexture("player_sheet_fire"), this.player.fire = this.player.animations.add("fire", [0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1]), this.player.animations.play("fire", 15, !0), this.game.lastLevelFinished = !0, this.game.saves && (this.game.saves.levels[CURRENT_LEVEL] = 1, localStorage))) {
var strSaves = JSON.stringify(this.game.saves);
strSaves && localStorage.setItem(SelectionState.SAVES_KEY, strSaves)
}
}, Level.prototype.render = function() {
}, Level.prototype.makePlayerEat = function(_item) {
this.musicManager.playBite(this.player.layer), this.player.bite();
var dust = getFreeDust(this);
null != dust && dust.blow(_item), _item.bubbleInfo && _item.bubbleInfo.deactivate(), this.player.food++, this.player.food == this.levelLoader.types[_item.layer] && (this.musicManager.nextLevel(), this.player.food = 0, this.player.layer++, 1 == this.scale ? this.scaleTo = .75 : .75 == this.scale ? this.scaleTo = .5 : .5 == this.scale && (this.scaleTo = .25), respawnBubbles(this)), this.items.remove(_item), _item.destroy()
}, Level.prototype.checkItemsToEat = function() {
for (var playerPos = new Phaser.Point(this.player.x, this.player.y), itemPos = new Phaser.Point, i = 0, found = !1, nbItems = this.items.children.length; nbItems > i && 0 == found; ) {
var item = this.items.children[i];
playerPos.setTo(this.player.x, this.player.y), itemPos.x = this.items.x + item.x, itemPos.y = this.items.y + item.y;
var distance = playerPos.distance(itemPos);
if (distance < Level.DISTANCE_EAT_MAX) {
itemPos.add(-this.planet.x, -this.planet.y), itemPos.normalize(), playerPos.add(-this.planet.x, -this.planet.y), playerPos.normalize();
var cross = playerPos.cross(itemPos);
cross >= 0 && item.layer <= this.player.layer && (this.makePlayerEat(item), found = !0)
}
i++
}
}, Level.prototype.moveCameraToPlanet = function() {
if (null == this.focusCamToPlanet && (this.focusCamToPlanet = new Phaser.Point, this.focusCamToPlanet.x = this.player.x, this.focusCamToPlanet.y = this.player.y, this.deltaCamToPlanet = new Phaser.Point, this.speedCameToPlanet = 200), this.deltaCamToPlanet.x = this.planet.x - this.focusCamToPlanet.x, this.deltaCamToPlanet.y = this.planet.y - this.focusCamToPlanet.y, this.deltaCamToPlanet.getMagnitude() > .01 * this.speedCameToPlanet) {
this.deltaCamToPlanet.normalize();
var dt = .001 * this.game.time.elapsed;
this.focusCamToPlanet.x += this.deltaCamToPlanet.x * this.speedCameToPlanet * dt, this.focusCamToPlanet.y += this.deltaCamToPlanet.y * this.speedCameToPlanet * dt, this.game.camera.focusOnXY(this.focusCamToPlanet.x, this.focusCamToPlanet.y)
}
};
"use strict";
var Loader = function(_game) {
Phaser.State.call(this, _game), _game.state.add("Loader", this, !1)
};
Loader.prototype = Object.create(Phaser.State.prototype), Loader.prototype.constructor = Loader, Loader.prototype.preload = function() {
this.game.stage.backgroundColor = "#66543d", this.game.world.setBounds(0, 0, window.innerWidth, window.innerHeight), this.loading = this.game.add.sprite(0, 0, "loading"), this.loading.anchor.setTo(.5, .5), this.loading.x = .5 * window.innerWidth, this.loading.y = .5 * window.innerHeight, this.loading.animations.add("load", [0, 1, 2, 3, 4, 5, 6], 10, !0), this.loading.animations.play("load", 10, !0), this.game.load.spritesheet("landing", "assets/images/landing.png", 640, 360), this.game.load.spritesheet("play", "assets/images/play.png", 322, 86), this.game.load.spritesheet("tutorial_jump", "assets/images/tuto_jump.png", 360, 300), this.game.load.spritesheet("tutorial_miam", "assets/images/tuto_miam.png", 360, 300), this.game.load.spritesheet("croix", "assets/images/croix.png", 72, 72), this.game.load.json("levels", "assets/data/json/levels.json"), this.game.load.spritesheet("levels_sheet", "assets/images/levels.png", 90, 90, 6), this.game.load.spritesheet("button_level", "assets/images/button_level.png", 90, 90, 2), this.game.load.image("logo", "assets/images/logo.png"), this.game.load.image("planet", "assets/images/planet.png"), this.game.load.image("bush", "assets/images/bush.png"), this.game.load.image("car", "assets/images/car.png"), this.game.load.image("house", "assets/images/house.png"), this.game.load.image("mountain", "assets/images/mountain.png"), this.game.load.spritesheet("jump_miam", "assets/images/jump_miam.png", 75, 75, 2), this.game.load.spritesheet("dust", "assets/images/dust.png", 204, 68, 6), this.game.load.spritesheet("player_sheet", "assets/images/rex.png", 62, 72, 5), this.game.load.spritesheet("player_sheet_fire", "assets/images/rex_fire.png", 119, 72, 8), this.game.load.spritesheet("foodgauge", "assets/images/foodgauge.png", 28, 265, 2), this.game.load.spritesheet("mute_sheet", "assets/images/mute.png", 52, 52, 2), MusicManager.preload(this.game), CloudManager.preload(this.game), this.game.load.bitmapFont("hungryrex_font1", "assets/fonts/hungryrex_font1.png", "assets/fonts/hungryrex_font1.json")
}, Loader.prototype.create = function() {
var goLanding = !0;
if (window.location.href.indexOf("localhost") >= 0) {
var split = window.location.href.split("level=");
split.length > 1 && (goLanding = !1, CURRENT_LEVEL = parseInt(split[1]), this.game.state.start("Level"))
}
1 == goLanding && this.game.state.start("Landing")
}, Loader.prototype.update = function() {
}, Loader.prototype.render = function() {
};
"use strict";
var SelectionState = function(_game) {
Phaser.State.call(this, _game), _game.state.add("Selection", this, !1), this.playButtons = new Array
};
SelectionState.prototype = Object.create(Phaser.State.prototype), SelectionState.prototype.constructor = SelectionState, SelectionState.SAVES_KEY = "hungryrex_saves", SelectionState.prototype.preload = function() {
}, SelectionState.prototype.create = function() {
if (null == this.game.saves && (this.game.saves = {levels: [1]}), localStorage) {
var strSaves = localStorage.getItem(SelectionState.SAVES_KEY);
"string" == typeof strSaves && (this.game.saves = JSON.parse(strSaves))
}
this.game.stage.backgroundColor = "#66543d", this.game.world.setBounds(0, 0, window.innerWidth, window.innerHeight), this.levelsJSON = this.game.cache.getJSON("levels"), this.background = this.game.add.sprite(0, 0, "landing"), this.background.anchor.setTo(.5, .5), this.background.x = .5 * window.innerWidth, this.background.y = .5 * window.innerHeight, this.prevButton = new LabelButton(this.game, 300, 200, "button_level", "<", 60, this.onClickPrev, this, 1, 0, 1), this.game.add.existing(this.prevButton), this.prevButton.anchor.setTo(.5, .5), this.prevButton.x = this.background.x - 320 + .75 * this.prevButton.width, this.prevButton.y = this.background.y + 20, this.prevButton.setLabel("<"), this.prevButton.visible = !1, this.playButtons = new Array;
for (var nbLevels = this.levelsJSON.levels.count, delta = .3, i = 0; nbLevels > i; i++) {
var button = null;
button = 1 == this.game.saves.levels[i] ? new LabelButton(this.game, 300, 200, "button_level", i + 1, 60, this.onClickLevel, this, 1, 0, 1) : new LabelButton(this.game, 300, 200, "button_level", i + 1, 60, null, null, 1, 1, 1), this.game.add.existing(button), button.name = i + 1, button.anchor.setTo(.5, .5), button.x = this.background.x - 320 + 640 * delta, button.y = this.background.y + 20, button.setLabel(i + 1), i > 2 && (button.visible = !1), this.playButtons.push(button), delta += .2, delta > .7 && (delta = .3)
}
this.nextButton = new LabelButton(this.game, 300, 200, "button_level", ">", 60, this.onClickNext, this, 1, 0, 1), this.game.add.existing(this.nextButton), this.nextButton.anchor.setTo(.5, .5), this.nextButton.x = this.background.x - 320 + (640 - .75 * this.nextButton.width), this.nextButton.y = this.background.y + 20, this.nextButton.setLabel(">"), 3 >= nbLevels && (this.nextButton.visible = !1), this.rex = this.game.add.sprite(0, 0, "player_sheet"), this.rex.anchor.setTo(.5, .5), this.rex.x = this.background.x + 5, this.rex.y = this.background.y + 132, this.rex.animations.add("walk", [0, 1, 2, 3, 2, 1], 7, !0), this.rex.animations.play("walk", 7, !0), null == this.game.lastLevelPlayed && (this.game.lastLevelPlayed = 1);
var nextLevel = this.game.lastLevelPlayed;
1 == this.game.lastLevelFinished && (nextLevel += 1, nextLevel >= nbLevels && (nextLevel += -1)), this.currentPos = Math.floor(nextLevel / 3), nextLevel % 3 == 0 && (this.currentPos += -1), this.changeButtons(0)
}, SelectionState.prototype.onClickPrev = function() {
this.changeButtons(-1)
}, SelectionState.prototype.onClickLevel = function(_button) {
CURRENT_LEVEL = _button.name, this.game.state.start("Level")
}, SelectionState.prototype.onClickNext = function() {
this.changeButtons(1)
}, SelectionState.prototype.changeButtons = function(_delta) {
this.currentPos += _delta;
var nbLayers = Math.floor(this.playButtons.length / 3);
this.playButtons.length % 3 > 0 && nbLayers++, this.currentPos <= 0 ? (this.currentPos = 0, this.prevButton.visible = !1) : this.prevButton.visible = !0, this.currentPos >= nbLayers - 1 ? (this.currentPos = nbLayers - 1, this.nextButton.visible = !1) : this.nextButton.visible = !0;
for (var i = 0; i < this.playButtons.length; i++) {
var button = this.playButtons[i];
button.visible = i >= 3 * this.currentPos && i < 3 * this.currentPos + 3 ? !0 : !1
}
};
Trigger = function(_level, _player) {
Phaser.Sprite.call(this, _level.game, -1e3, 0), _level.game.add.existing(this), this.level = _level, this.player = _player, this.planet = this.level.planet
}, Trigger.prototype = Object.create(Phaser.Sprite.prototype), Trigger.prototype.constructor = Trigger, Trigger.prototype.update = function() {
var planetScale = this.planet.scale.x;
this.scale.setTo(planetScale, planetScale), this.angleRad = this.anglePos * (3.14159 / 180);
var realRadius = .5 * this.planet.width + .5 * this.height - this.offsetY * planetScale;
this.x = realRadius * Math.cos(this.angleRad), this.y = realRadius * Math.sin(this.angleRad), this.angle = this.anglePos + 90
}, Trigger.prototype.doCallback = function() {
this.callback && Trigger[this.callback](this)
}, Trigger.showTutoEat = function(_trigger) {
_trigger.player.pause = !0;
new Popup(_trigger.level, "tutorial_miam")
}, Trigger.showTutoJump = function(_trigger) {
_trigger.player.pause = !0;
new Popup(_trigger.level, "tutorial_jump")
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment