This repository has been archived on 2026-05-18. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
moonlight-skulk/moonlight/js/moonlight-skulk.js

1351 lines
36 KiB
JavaScript
Raw Normal View History

2014-06-14 12:00:08 -07:00
STATE_NONE = 0;
2014-06-11 21:57:20 -07:00
STATE_UNAWARE = 1 << 1;
STATE_CONCERNED = 1 << 2;
STATE_ALERTED = 1 << 3;
STATE_LOSTHIM = 1 << 4;
2014-06-14 11:59:41 -07:00
STATE_RUNNING = 1 << 5;
STATE_FACE_LEFT = 1 << 6;
STATE_FACE_RIGHT = 1 << 7;
STATE_FACE_UP = 1 << 8;
STATE_FACE_DOWN = 1 << 9;
STATE_MOVING = 1 << 10;
STATES_AWARENESS = (STATE_UNAWARE | STATE_CONCERNED | STATE_ALERTED | STATE_LOSTHIM);
2014-06-14 11:59:41 -07:00
STATES_MOVEMENT = (STATE_MOVING | STATE_RUNNING);
STATES_FACE = (STATE_FACE_LEFT | STATE_FACE_RIGHT | STATE_FACE_DOWN | STATE_FACE_UP);
2014-06-11 22:25:47 -07:00
SPRITE_TOWNSFOLK_MALE = 1;
2014-06-11 22:29:49 -07:00
SPRITE_TOWNSFOLK_FEMALE = 2;
SPRITE_TOWNSFOLK_GUARD = 3;
2014-06-11 21:57:20 -07:00
SPRITE_TOWNSFOLK_MALE1 = 1;
SPRITE_TOWNSFOLK_MALE2 = 2;
SPRITE_TOWNSFOLK_MALE3 = 3;
SPRITE_TOWNSFOLK_MALE4 = 4;
2014-06-14 11:59:41 -07:00
2014-06-11 21:57:20 -07:00
SPRITE_TOWNSFOLK_FEMALE1 = 5;
SPRITE_TOWNSFOLK_FEMALE2 = 6;
SPRITE_TOWNSFOLK_FEMALE3 = 7;
SPRITE_TOWNSFOLK_FEMALE4 = 8;
2014-06-14 11:59:41 -07:00
2014-06-11 21:57:20 -07:00
SPRITE_TOWNSFOLK_GUARD1 = 9;
SPRITE_TOWNSFOLK_GUARD2 = 10;
2014-06-14 16:20:59 -07:00
var game = new Phaser.Game(640, 480, Phaser.AUTO, '');
2014-06-12 23:40:01 -07:00
2014-06-12 23:18:40 -07:00
// Create torch objects
// Light constructor
2014-06-14 20:03:55 -07:00
var Light = function(game, x, y, key, frame, radius, fade, color_start, color_stop, flicker, always_render, light_meter) {
2014-06-12 23:55:02 -07:00
color_start = ( typeof color_start == undefined ? color_start : 'rgba(255, 255, 255, 1.0)');
2014-06-12 23:57:46 -07:00
color_stop = ( typeof color_stop == undefined ? color_stop : 'rgba(255, 255, 255, 0.0)');
2014-06-12 23:43:46 -07:00
fade = ( typeof fade == undefined ? fade : 0.25);
radius = ( typeof radius == undefined ? radius : 64);
flicker = ( typeof flicker == undefined ? flicker : false);
2014-06-13 01:20:35 -07:00
always_render = ( typeof always_render == undefined ? always_render : false);
2014-06-14 20:03:55 -07:00
lightmeter = ( typeof light_meter == undefined ? lightmeter : 1.0 );
2014-06-12 23:18:40 -07:00
Phaser.Sprite.call(this, game, x, y, null);
// Set the pivot point for this sprite to the center
this.anchor.setTo(0.5, 0.5);
2014-06-12 23:57:46 -07:00
2014-06-12 23:55:36 -07:00
this.color_start = color_start;
this.color_stop = color_stop;
2014-06-12 23:18:40 -07:00
this.radius = radius;
2014-06-14 20:27:00 -07:00
this.rendered_radius = radius;
2014-06-12 23:18:40 -07:00
this.fade = radius * fade
2014-06-14 20:03:55 -07:00
this.light_meter = lightmeter;
2014-06-13 01:20:35 -07:00
this.always_render = always_render
2014-06-12 23:18:40 -07:00
this.rect = new Phaser.Rectangle(this.x - radius, this.y - radius, radius * 2, radius * 2)
this.flicker = flicker;
};
2014-06-12 23:56:22 -07:00
// Lightes are a type of Phaser.Sprite
Light.prototype = Object.create(Phaser.Sprite.prototype);
Light.prototype.constructor = Light;
2014-06-13 00:03:27 -07:00
Light.prototype.update_new_values = function() {
2014-06-14 20:03:55 -07:00
this.light_meter = parseInt(this.light_meter);
2014-06-13 00:09:43 -07:00
this.radius = parseInt(this.radius);
2014-06-13 00:09:01 -07:00
this.fade = this.radius * Number(this.fade);
2014-06-14 16:46:50 -07:00
this.flicker = parseBoolean(this.flicker);
this.always_render = parseBoolean(this.always_render);
2014-06-12 23:55:02 -07:00
this.rect = new Phaser.Rectangle(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2)
}
2014-06-13 22:35:55 -07:00
function SoundSprite(game, x, y, key, frame,
sound_key,
sound_marker,
sound_position,
sound_volume,
sound_loop,
2014-06-13 23:07:01 -07:00
sound_forcerestart,
2014-06-15 11:53:13 -07:00
sound_distance,
2014-06-13 23:07:01 -07:00
sound_nofade)
2014-06-13 22:35:55 -07:00
{
Phaser.Sprite.call(this, game, x, y, null);
this.sound_key = sound_key;
this.sound_marker = ( typeof sound_marker == undefined ? sound_marker : '');
2014-06-14 00:20:53 -07:00
this.sound_volume = ( typeof sound_volume == undefined ? sound_volume : 1.0 );
this.sound_position = ( typeof sound_position == undefined ? sound_position : 1.0 );
this.sound_loop = ( typeof sound_loop == undefined ? sound_loop : true );
2014-06-15 12:25:44 -07:00
this.sound_forcerestart = ( typeof sound_forcerestart == undefined ? sound_forcerestart : false );
2014-06-15 11:53:13 -07:00
var def_distance = Math.sqrt(
Number((game.camera.width/2) * (game.camera.width/2)) +
Number((game.camera.height/2) * (game.camera.height/2))
);
this.sound_distance = ( typeof sound_distance == undefined ? sound_distance : def_distance);
2014-06-14 00:20:53 -07:00
this.sound_nofade = (typeof sound_nofade == undefined ? sound_nofade : false);
2014-06-13 22:35:55 -07:00
this.sound = null;
}
SoundSprite.prototype = Object.create(Phaser.Sprite.prototype);
SoundSprite.prototype.constructor = Light;
SoundSprite.prototype.update_new_values = function() {
if ( this.sound_key == null ) {
if ( this.sound !== null ) {
this.sound.stop();
}
return;
}
2014-06-14 00:20:53 -07:00
this.sound_position = parseInt(this.sound_position);
2014-06-15 11:53:13 -07:00
this.sound_distance = Number(this.sound_distance);
2014-06-14 00:19:07 -07:00
this.sound_volume = Number(this.sound_volume);
2014-06-14 16:46:50 -07:00
this.sound_loop = parseBoolean(this.sound_loop);
this.sound_forcerestart = parseBoolean(this.sound_forcerestart);
this.sound_nofade = parseBoolean(this.sound_nofade);
2014-06-14 00:19:07 -07:00
2014-06-15 12:11:18 -07:00
if ( this.sound !== null )
this.sound.stop();
2014-06-15 12:13:04 -07:00
this.sound = game.add.audio(this.sound_key, this.sound_volume, this.sound_loop);
2014-06-13 22:52:02 -07:00
this.sound.play(
2014-06-13 22:35:55 -07:00
this.sound_marker,
this.sound_position,
this.sound_volume,
this.sound_loop,
this.sound_forcerestart);
}
2014-06-13 23:23:28 -07:00
SoundSprite.prototype.adjust_relative_to = function(spr) {
if ( this.sound_nofade == true ) {
2014-06-14 00:19:07 -07:00
this.sound.volume = this.sound_volume;
2014-06-13 23:21:42 -07:00
return;
}
// The volume of any given sound is equal to the length of the
// hypotenuse of a triangle drawn from the point (p) to the
// sprite in question
2014-06-13 23:23:28 -07:00
var xd = (spr.x - this.x);
2014-06-13 23:21:42 -07:00
if ( xd < 0 )
xd = -(xd);
2014-06-13 23:23:28 -07:00
var yd = (spr.y - this.y);
2014-06-13 23:21:42 -07:00
if ( yd < 0 )
yd = -(yd);
2014-06-13 23:41:42 -07:00
2014-06-15 11:53:13 -07:00
var hyp = Math.sqrt(Number(xd * xd) + Number(yd * yd));
2014-06-13 23:52:43 -07:00
2014-06-15 11:53:13 -07:00
this.sound.volume = (1.0 - Number(hyp / this.sound_distance));
2014-06-14 00:06:30 -07:00
// Math.max doesn't work here??
if ( this.sound.volume < 0 )
this.sound.volume = 0;
2014-06-13 23:56:48 -07:00
2014-06-13 23:21:42 -07:00
}
var moonlightSettings = {
'map' : {
'tilesets': [
2014-06-15 11:00:21 -07:00
{
'name': 'bigtop',
'path': 'gfx/tiles/bigtop.png'
},
2014-06-15 12:38:05 -07:00
{
'name': '002-Woods01',
'path': 'gfx/tiles/002-Woods01.png'
},
2014-06-12 21:57:03 -07:00
{
2014-06-12 21:58:14 -07:00
'name': '009-CastleTown01',
'path': 'gfx/tiles/009-CastleTown01.png'
},
2014-06-12 21:57:03 -07:00
{
'name': '010-CastleTown02',
2014-06-12 21:58:14 -07:00
'path': 'gfx/tiles/010-CastleTown02.png'
2014-06-12 21:57:03 -07:00
},
{
'name': '025-Castle01',
'path': 'gfx/tiles/025-Castle01.png'
},
{
'name': '026-Castle02',
'path': 'gfx/tiles/026-Castle02.png'
},
{
'name': '027-Castle03',
'path': 'gfx/tiles/027-Castle03.png'
},
{
'name': '027-Castle03',
'path': 'gfx/tiles/027-Castle03.png'
},
{
'name': '028-Church01',
'path': 'gfx/tiles/028-Church01.png'
},
{
'name': '029-Church02',
'path': 'gfx/tiles/029-Church02.png'
},
{
'name': '034-Bridge01',
2014-06-12 21:58:14 -07:00
'path': 'gfx/tiles/034-Bridge01.png'
2014-06-12 21:57:03 -07:00
},
{
'name': '035-Ruins01',
'path': 'gfx/tiles/035-Ruins01.png'
},
{
'name': '037-Fort01',
'path': 'gfx/tiles/037-Fort01.png'
},
{
'name': '038-Fort02',
'path': 'gfx/tiles/038-Fort02.png'
},
{
'name': '039-Tower01',
'path': 'gfx/tiles/039-Tower01.png'
},
{
'name': '040-Tower02',
'path': 'gfx/tiles/040-Tower02.png'
},
{
'name': '041-EvilCastle01',
'path': 'gfx/tiles/041-EvilCastle01.png'
},
{
'name': '042-EvilCastle02',
'path': 'gfx/tiles/042-EvilCastle02.png'
},
{
'name': '048-Sewer01',
'path': 'gfx/tiles/048-Sewer01.png'
},
{
'name': '004-Mountain01',
'path': 'gfx/tiles/004-Mountain01.png'
2014-06-13 02:37:08 -07:00
},
{
'name': '!Door1',
'path': 'gfx/tiles/Doors.png'
}
],
2014-06-12 21:57:03 -07:00
'layers': {
'0 - NonCollide Base': {
'collides': false,
2014-06-12 23:04:33 -07:00
'collisionBetween': [0, 0],
2014-06-13 00:25:39 -07:00
'type': 'tiles',
'inject_sprites': false
2014-06-12 21:57:03 -07:00
},
'0 - Collide Base': {
'collides': true,
2014-06-12 23:04:33 -07:00
'collisionBetween': [0, 9999],
2014-06-13 00:25:39 -07:00
'type': 'tiles',
'inject_sprites': false
2014-06-12 21:57:03 -07:00
},
2014-06-12 22:19:32 -07:00
'0 - NonCollide Overlay - Pathways': {
2014-06-12 22:04:42 -07:00
'collides': false,
2014-06-12 23:04:33 -07:00
'collisionBetween': [0, 9999],
2014-06-13 00:25:39 -07:00
'type': 'tiles',
'inject_sprites': false
2014-06-12 21:57:03 -07:00
},
2014-06-13 02:37:08 -07:00
'0 - NonCollide Overlay - Below Player': {
'collides': false,
'collisionBetween': [0, 9999],
'type': 'tiles',
'inject_sprites': false
},
2014-06-12 22:17:22 -07:00
'0 - Collide Overlay - Ground Objects': {
2014-06-12 22:04:42 -07:00
'collides': true,
2014-06-12 23:04:33 -07:00
'collisionBetween': [0, 9999],
2014-06-13 00:25:39 -07:00
'type': 'tiles',
2014-06-13 00:26:50 -07:00
'inject_sprites': true
2014-06-12 22:18:25 -07:00
},
2014-06-15 12:44:12 -07:00
'0 - NonCollide Overlay - Above Player (Short)': {
'collides': false,
'collisionBetween': [0, 9999],
'type': 'tiles',
'inject_sprites': false
},
'0 - NonCollide Overlay - Above Player (Tall)': {
2014-06-12 22:17:22 -07:00
'collides': false,
2014-06-12 23:04:33 -07:00
'collisionBetween': [0, 9999],
2014-06-13 00:25:39 -07:00
'type': 'tiles',
2014-06-13 00:26:50 -07:00
'inject_sprites': false
2014-06-12 22:17:22 -07:00
}
2014-06-12 21:57:03 -07:00
},
2014-06-15 11:00:56 -07:00
'path': 'gfx/map.json'
},
2014-06-13 22:35:55 -07:00
'sounds': [
{
'name': 'fountain',
'path': 'sfx/fountain.wav'
},
2014-06-14 12:48:36 -07:00
{
'name': 'fire',
'path': 'sfx/fire.ogg'
2014-06-15 11:00:21 -07:00
},
{
'name': 'calliope',
'path': 'sfx/calliope.mp3'
2014-06-13 22:35:55 -07:00
}
],
'images': [
{
2014-06-14 20:41:32 -07:00
'name': 'lightbox',
2014-06-14 20:39:03 -07:00
'path': 'gfx/ui/lightbox.png'
},
{
2014-06-14 20:41:32 -07:00
'name': 'lightbar',
2014-06-14 20:39:03 -07:00
'path': 'gfx/ui/lightbar.png'
2014-06-12 01:46:05 -07:00
},
{
2014-06-14 10:59:00 -07:00
'name': 'wordbubble',
'path': 'gfx/effects/wordbubble.png'
}
],
'spritesheets': [
{
'name': 'flame',
'path': 'gfx/effects/flame.png',
'width': 32,
'height': 32,
'frames': 96
},
{
'name': 'balloon',
'path': 'gfx/effects/Balloon.png',
'width': 32,
'height': 32,
'frames': 80
},
{
'name': 'player',
'path': 'gfx/sprites/sprite-player.png',
'width': 32,
'height': 32,
'frames': 12
},
{
'name': 'townsfolk-male-1',
'path': 'gfx/sprites/sprite-townsfolk-male-1.png',
'width': 32,
'height': 32,
'frames': 12
},
{
'name': 'townsfolk-male-2',
'path': 'gfx/sprites/sprite-townsfolk-male-2.png',
'width': 32,
'height': 32,
'frames': 12
},
{
'name': 'townsfolk-male-3',
'path': 'gfx/sprites/sprite-townsfolk-male-3.png',
'width': 32,
'height': 32,
'frames': 12
},
{
'name': 'townsfolk-male-4',
'path': 'gfx/sprites/sprite-townsfolk-male-4.png',
'width': 32,
'height': 32,
'frames': 12
},
{
'name': 'townsfolk-female-1',
'path': 'gfx/sprites/sprite-townsfolk-female-1.png',
'width': 32,
'height': 32,
'frames': 12
},
{
'name': 'townsfolk-female-2',
'path': 'gfx/sprites/sprite-townsfolk-female-2.png',
'width': 32,
'height': 32,
'frames': 12
},
{
'name': 'townsfolk-female-3',
'path': 'gfx/sprites/sprite-townsfolk-female-3.png',
'width': 32,
'height': 32,
'frames': 12
},
{
'name': 'townsfolk-female-4',
'path': 'gfx/sprites/sprite-townsfolk-female-4.png',
'width': 32,
'height': 32,
'frames': 12
},
{
'name': 'townsfolk-guard-1',
'path': 'gfx/sprites/sprite-townsfolk-guard-1.png',
'width': 32,
'height': 32,
'frames': 12
},
{
'name': 'townsfolk-guard-2',
'path': 'gfx/sprites/sprite-townsfolk-guard-2.png',
'width': 32,
'height': 32,
'frames': 12
}
],
'animations': {
'alerted': {
'frames': [0, 1, 2, 3, 4, 5, 6, 7],
'speed': 4,
'loop': false
},
'concerned': {
'frames': [8, 9, 10, 11, 12, 13, 14, 15],
'speed': 4,
'loop': false
},
'relieved': {
'frames': [40, 41, 42, 43, 44, 45, 46, 47],
'speed': 4,
'loop': false
},
'angry': {
'frames': [48, 49, 50, 51, 52, 53, 54, 55],
'speed': 4,
'loop': false
},
'bipedwalkdown': {
'frames': [1, 2, 0],
'speed': 4,
'loop': true
},
'bipedwalkleft': {
'frames': [4, 5, 3],
'speed': 4,
'loop': true
},
'bipedwalkright': {
'frames': [7, 8, 6],
'speed': 4,
'loop': true
},
'bipedwalkup': {
'frames': [10, 11, 9],
'speed': 4,
'loop': true
},
'bipedrundown': {
'frames': [1, 2, 0],
'speed': 12,
'loop': true
},
'bipedrunleft': {
'frames': [4, 5, 3],
'speed': 12,
'loop': true
},
'bipedrunright': {
'frames': [7, 8, 6],
'speed': 12,
'loop': true
},
'bipedrunup': {
'frames': [10, 11, 9],
'speed': 12,
'loop': true
},
'campfire_small': {
'frames': [6, 7, 8],
'speed': 6,
'loop': true
2014-06-15 11:00:21 -07:00
},
'fire_small': {
'frames': [9, 10, 11],
'speed': 6,
'loop': true
}
}
};
2014-06-11 21:56:46 -07:00
var moonlightDialog = {
"status": {
2014-06-11 22:51:05 -07:00
"townsfolk-male" : {
"unaware" : [
2014-06-14 09:58:39 -07:00
"I'd rather be fishing.",
"Different day, same old stuff.",
"Oi! Where'd that trouble run\noff to now then?",
"The missus is off shoppin', and\nhere I am sittin' on\nme Jack Jones.",
"Oy I'm gonna have a butchers at\nthat new tailor's knickers\nhe has for sale.",
"I'm off to the pub to see the\nlads and chew the fat.",
"♪ ♫ Whistling ♪ ♫"
2014-06-11 21:56:46 -07:00
],
2014-06-11 22:51:05 -07:00
"concerned" : [
2014-06-14 09:58:39 -07:00
"Wha… whats that? Whos there?",
"Did you hear that?",
"Either Im hearin things, or I\nneed to stop drinkin midday.",
"Oi? I dont want no tomfoolery;\ncome out if youre there!",
"Must be them darned kids again.",
"Whats that?",
"Did you see that?"
2014-06-11 22:02:11 -07:00
],
2014-06-11 22:51:05 -07:00
"alerted" : [
2014-06-14 09:58:39 -07:00
"Don't you come no closer, you hear?",
"Egads!",
"I'm getting outta here!",
"What's going on?!",
"Holy bejeezus!",
"Did you see that?",
"What're you doing?!",
"Get away!",
"Get away from me!",
"Stay away! I know Kung-fu! ... but\nthat would require bravery \nI don't have",
"Guards! GUARDS!"
2014-06-11 22:02:11 -07:00
],
2014-06-11 22:51:05 -07:00
"losthim" : [
2014-06-14 09:58:39 -07:00
"Whew. Glad thats over.",
"I wasnt scared!",
"Mustve been intimidated by\nmy manly physique.",
"Thats right! Run away!",
"Aye, and dont-cha come back!",
"Spoony Bard...",
"Bloody wanker!"
2014-06-11 22:02:11 -07:00
]
2014-06-11 21:56:46 -07:00
},
2014-06-11 22:51:05 -07:00
"townsfolk-female" : {
2014-06-11 23:43:59 -07:00
"unaware" : [
2014-06-14 09:58:39 -07:00
"My retro shake brings all the\nboys to the yard.",
"I'm off to get my Barnet sorted\nout. Ill be the best looking\nlady at the gala.",
"It's always all itsy bitsy with\nthem boys at the Rub-a-Dub.",
"I need to get this shopping\nsorted out.",
"What a lovely evening. Perfect\nfor skulking, I would imagine."
2014-06-11 23:43:59 -07:00
],
"concerned" : [
2014-06-14 09:58:39 -07:00
"Wha… whats that? Whos there?",
"Did you hear that?",
"Martha? Is that you?",
"I don't want no tomfoolery.\nGo away!",
"What was that? This is how horror\ntheatre bits start…",
"What's that?",
"Did you see that?"
],
2014-06-11 23:43:59 -07:00
"alerted" : [
2014-06-14 09:58:39 -07:00
"Eeeek!",
"Stay away from me!",
"Guards! Guards!",
"What in the nine hells?",
"Get back or I'll swoon!",
"Help! He's after me virtue!"
2014-06-11 23:43:59 -07:00
],
"losthim" : [
2014-06-14 09:58:39 -07:00
"Good riddance! Theres too many\nmale protagonists in\ngames anyhow!",
"I sure am glad thats over.",
"This town is going straight to hell.",
"I hope he doesnt come back.",
"I hope hes caught and hanged!"
2014-06-11 23:43:59 -07:00
]
2014-06-11 21:56:46 -07:00
},
2014-06-11 22:51:05 -07:00
"townsfolk-guard" : {
2014-06-11 23:43:59 -07:00
"unaware" : [
2014-06-14 09:58:39 -07:00
"Just doing my civic duty.",
"Good day, citizens.",
"Honor. Liberty. Justice.\nOh, and pancakes…\nI love pancakes.",
"No loitering.",
"I am the law.",
"May Evil beware and may\nGood dress warmly and\neat plenty of fresh vegetables.",
"We're sworn to protect The City."
2014-06-11 23:43:59 -07:00
],
"concerned" : [
2014-06-14 09:58:39 -07:00
"I sense law-breaking abound.",
"Did you hear something?",
"Did you see that?",
"I know you're around here\nsomewhere, rat…",
"Don't make me look for\nyou in hard-to-reach places!",
"The eyes play tricks\nlike tiny, round devils."
2014-06-11 23:43:59 -07:00
],
"alerted" : [
2014-06-14 09:58:39 -07:00
"Surrender lawbreaker!",
"Halt!",
"Halt! In the name of the… umm, er… me!",
"Prepare for justice, criminal!",
"I am justice!",
"Theres no escaping the law!",
"Surrender thief!",
"Prepare to taste steel!",
"Clear the area! Nobody\npanic! I'll catch him!"
2014-06-11 23:43:59 -07:00
],
"losthim" : [
2014-06-14 09:58:39 -07:00
"Ill get you next time,\ncriminal scum.",
"Defeat is a harsh mistress.",
"Evil men may get away, but\njustice fights another day.",
"Wickedness flees, evading the\ncold steel of righteousness."
2014-06-11 23:43:59 -07:00
]
2014-06-11 21:56:46 -07:00
}
},
"conversations": {
"townsfolk-male": {
"townsfolk-female": [],
"townsfolk-male": [],
"townsfolk-guard": []
},
"townsfolk-female": {
"townsfolk-male": [],
"townsfolk-female": [],
"townsfolk-guard": [],
},
"townsfolk-guard": {
"townsfolk-male": [],
"townsfolk-female": [],
"townsfolk-guard": []
}
2014-06-14 09:58:39 -07:00
}
2014-06-11 21:56:46 -07:00
};
2014-06-14 09:58:39 -07:00
function stringSize(str, font)
{
var f = font || '12px arial',
2014-06-14 10:46:50 -07:00
o = $('<div>' + str + '</div>')
2014-06-14 09:58:39 -07:00
.css({'position': 'absolute', 'float': 'left', 'white-space': 'nowrap', 'visibility': 'hidden', 'font': f})
.appendTo($('body')),
w = o.width(),
h = o.height();
o.remove();
return [w, h];
}
var EffectSprite = function(game, x, y, key, frame, animation) {
this.update_new_values = function() {
this.animations.destroy();
this.loadTexture(this.sprite_key, 0);
addAnimation(this, this.sprite_animation);
this.animations.play(this.sprite_animation);
}
Phaser.Sprite.call(this, game, x, y, null);
game.physics.arcade.enable(this);
this.collide_with_map = true;
this.collide_with_player = false;
}
EffectSprite.prototype = Object.create(Phaser.Sprite.prototype);
EffectSprite.prototype.constructor = EffectSprite;
var AISprite = function(game, x, y, key, frame) {
2014-06-14 16:17:08 -07:00
this.viewRectangle = function() {
var offset = [];
var size = [];
2014-06-14 14:54:33 -07:00
if ( hasState(this, STATE_FACE_LEFT) ) {
offset = [0, -32];
size = [-this.view_distance, 96];
2014-06-14 14:54:33 -07:00
} else if ( hasState(this, STATE_FACE_RIGHT) ) {
offset = [32, -32];
size = [32 + this.view_distance, 96];
2014-06-14 14:54:33 -07:00
} else if ( hasState(this, STATE_FACE_DOWN) ) {
offset = [-32, 0];
size = [96, this.view_distance];
2014-06-14 15:36:11 -07:00
} else if ( hasState(this, STATE_FACE_UP) ) {
offset = [-32, 0];
size = [96, -this.view_distance];
2014-06-14 16:38:58 -07:00
} else {
return null;
}
if ( hasState(this, STATE_ALERTED) ) {
offset = [offset[0] * 2, offset[1] * 2];
size = [size[0] * 2, size[1] * 2];
}
return new Phaser.Rectangle(this.x + offset[0],
this.y + offset[1],
size[0],
size[1]);
2014-06-14 16:38:58 -07:00
}
this.canSeeSprite = function(spr, debug) {
var xd = (spr.x - this.x);
if ( xd < 0 )
xd = -(xd);
var yd = (spr.y - this.y);
if ( yd < 0 )
yd = -(yd);
var hyp = Math.sqrt(Number(xd * xd) + Number(yd * yd));
if ( hyp > this.view_distance ) {
if ( debug == true )
console.log(spr + " is too far away");
return false;
}
var viewrect = this.viewRectangle();
if ( viewrect == null )
return false;
2014-06-14 16:48:09 -07:00
var sprrect = new Phaser.Rectangle(spr.x, spr.y, 32, 32);
2014-06-14 16:38:58 -07:00
if ( viewrect.intersects(sprrect) || viewrect.containsRect(sprrect) ) {
return true;
}
return false;
}
this.enableAwarenessChange = function(state) {
this.awareness_change_enabled = true;
}
this.setAwarenessEffect = function(state) {
var animkey = "";
if ( hasState(this, state) == true ) {
return;
} else if ( (state == STATE_LOSTHIM) &&
(hasState(this, STATE_ALERTED) == false) &&
(hasState(this, STATE_CONCERNED) == false) ) {
return;
}
setAwarenessState(this, state);
if ( this.awareness_change_enabled == false &&
state != STATE_ALERTED ) {
return;
}
this.awareness_change_enabled = false;
if ( this.awareness_timer !== null )
this.awareness_timer.stop();
this.awareness_timer = game.time.create(false);
this.awareness_timer.add(this.sprite_awareness_duration,
this.enableAwarenessChange,
this);
this.awareness_timer.start()
if ( this.awareness_effect !== null ) {
this.awareness_effect.alive = false;
this.awareness_effect.destroy();
this.awareness_effect = null;
}
if ( state == STATE_ALERTED ) {
animkey = "alerted";
} else if ( state == STATE_CONCERNED ) {
animkey = "concerned";
} else if ( state == STATE_LOSTHIM ) {
if ( this.sprite_group == "townsfolk-guard" ) {
animkey = "angry";
} else {
animkey = "relieved";
}
}
if ( animkey == "" )
return;
this.clearWordBubble();
this.awareness_effect = game.state.states.game.add.sprite(
this.x + 16,
this.y - 16,
'balloon');
addAnimation(this.awareness_effect, animkey);
this.awareness_effect.play(animkey, null, false, true);
}
2014-06-14 16:38:58 -07:00
this.enableWordBubble = function() {
this.enable_word_bubble = true;
this.timer = game.time.create(false);
var timerdelta = 10000 + (game.rnd.integerInRange(0, 20) * 1000);
timerev = this.timer.add(timerdelta, this.setWordBubble, this);
this.timer.start()
}
this.clearWordBubble = function() {
if ( this.bubble_text !== null )
this.clear_bubble = true;
this.enable_word_bubble = false;
this.timer = game.time.create(false);
timerev = this.timer.add(1000, this.enableWordBubble, this);
this.timer.start()
}
this.setWordBubble = function()
{
if ( this.bubble_text !== null || this.sprite_group == undefined || this.enable_world_bubble == false) {
return;
}
aistate = this.state & ( STATE_UNAWARE | STATE_CONCERNED | STATE_ALERTED | STATE_LOSTHIM );
switch ( aistate ) {
case STATE_UNAWARE: {
aistate = "unaware";
break;
}
case STATE_CONCERNED: {
aistate = "concerned";
break;
}
case STATE_ALERTED: {
aistate = "alerted";
break;
}
case STATE_LOSTHIM: {
aistate = "losthim";
break;
}
}
var mylines = moonlightDialog['status'][this.sprite_group][aistate];
text = mylines[game.rnd.integerInRange(0, mylines.length-1)];
style = {font: '14px Arial Bold', fill: '#ffffff'}
this.text_size = stringSize(text, style['font']);
this.bubble_sprite = game.add.sprite(this.x, this.y, 'wordbubble');
this.bubble_sprite.anchor.setTo(0.5, 0.5);
this.bubble_text = game.add.text(this.x, this.y, text, style);
this.snap_bubble_position();
this.timer = game.time.create(false);
timerev = this.timer.add(5000, this.clearWordBubble, this);
this.timer.start()
}
this.snap_bubble_position = function()
{
this.bubble_sprite.x = this.x + 16;
this.bubble_sprite.y = this.y - 33;
this.bubble_text.position.x = this.x + 16 - 150 + 8;
this.bubble_text.position.y = this.y - 67 + 8;
}
this.update = function()
{
var running = false;
var newstate = STATE_NONE;
if ( this.awareness_effect !== null ) {
if ( this.awareness_effect.alive == false ) {
this.awareness_effect.destroy();
this.awareness_effect = null;
} else {
this.awareness_effect.x = this.x + 16;
this.awareness_effect.y = this.y - 16;
}
}
2014-06-14 16:38:58 -07:00
if ( this.bubble_text !== null ) {
if ( this.clear_bubble == true ) {
this.bubble_text.destroy();
this.bubble_sprite.destroy();
this.bubble_text = null;
this.bubble_sprite = null;
this.clear_bubble = false;
} else {
this.snap_bubble_position();
}
}
2014-06-15 13:00:57 -07:00
if ( this.sprite_canmove == false) {
this.body.immovable = true;
2014-06-14 16:38:58 -07:00
return;
2014-06-15 13:00:57 -07:00
}
2014-06-14 16:38:58 -07:00
if ( game.rnd.integerInRange(0, 100) < 95 )
return;
if ( game.rnd.integerInRange(0, 100) > 90 ) {
newstate = STATE_RUNNING;
}
switch ( game.rnd.integerInRange(0, 4) ) {
case 0: {
newstate = newstate | (STATE_FACE_RIGHT | STATE_MOVING);
break;
}
case 1: {
newstate = newstate | (STATE_FACE_LEFT | STATE_MOVING);
break;
}
case 2: {
newstate = newstate | (STATE_FACE_UP | STATE_MOVING);
break;
}
case 3: {
newstate = newstate | (STATE_FACE_DOWN | STATE_MOVING);
}
}
setMovingState(this, newstate);
setSpriteMovement(this);
}
this.update_new_values = function() {
2014-06-14 16:43:42 -07:00
console.log(this);
2014-06-14 16:38:58 -07:00
if ( this.timer !== null )
this.timer.stop();
this.animations.destroy();
this.clearWordBubble();
this.state = STATE_UNAWARE;
this.sprite_can_see_lightmeter = Number(this.sprite_can_see_lightmeter);
2014-06-15 12:53:45 -07:00
this.sprite_canmove = parseBoolean(this.sprite_canmove);
this.sprite_awareness_duration = parseInt(this.sprite_awareness_duration);
2014-06-14 16:46:50 -07:00
this.collide_with_player = parseBoolean(this.collide_with_player);
this.collide_with_map = parseBoolean(this.collide_with_map);
this.carries_light = parseBoolean(this.carries_light);
this.loadTexture(this.sprite_name, 0);
addAnimation(this, 'bipedwalkleft');
addAnimation(this, 'bipedwalkright');
addAnimation(this, 'bipedwalkup');
addAnimation(this, 'bipedwalkdown');
addAnimation(this, 'bipedrunleft');
addAnimation(this, 'bipedrunright');
addAnimation(this, 'bipedrunup');
addAnimation(this, 'bipedrundown');
2014-06-14 12:10:24 -07:00
setMovingState(this, STATE_FACE_DOWN);
2014-06-14 11:59:41 -07:00
setSpriteMovement(this);
}
var spritenames_by_type = [
'townsfolk-male-1',
'townsfolk-male-2',
'townsfolk-male-3',
'townsfolk-male-4',
'townsfolk-female-1',
'townsfolk-female-2',
'townsfolk-female-3',
'townsfolk-female-4',
'townsfolk-guard-1',
'townsfolk-guard-2'
];
2014-06-14 10:09:46 -07:00
2014-06-14 10:25:20 -07:00
Phaser.Sprite.call(this, game, x, y, null);
2014-06-11 21:03:01 -07:00
game.physics.arcade.enable(this);
this.awareness_change_enabled = true;
this.lightmeter = 1.0;
this.sprite_can_see_lightmeter = 0.5;
this.awareness_effect = null;
this.awareness_timer = null;
this.sprite_awareness_duration = 30000;
2014-06-15 12:53:45 -07:00
this.sprite_canmove = 'true';
2014-06-14 16:37:18 -07:00
this.collide_with_player = 'true';
this.collide_with_map = 'true';
this.carries_light = 'false';
2014-06-14 14:44:37 -07:00
this.view_distance = 32 * 5;
2014-06-14 10:22:10 -07:00
this.timer = null;
2014-06-14 11:01:47 -07:00
this.bubble_text = null;
2014-06-14 10:26:09 -07:00
this.enable_word_bubble = false;
2014-06-11 21:19:34 -07:00
this.body.collideWorldBounds = true;
this.sprite_name = "townsfolk-male-1";
this.sprite_group = "townsfolk-male";
2014-06-14 10:25:08 -07:00
this.update_new_values();
2014-06-11 21:00:59 -07:00
}
2014-06-11 22:05:54 -07:00
AISprite.prototype = Object.create(Phaser.Sprite.prototype);
AISprite.prototype.constructor = AISprite;
2014-06-14 14:44:37 -07:00
function rotatePoints(arr, x, y, degrees)
{
arr.forEach(function(p) {
p.rotate(x, y, degrees, true);
}, this);
}
2014-06-11 10:05:44 -07:00
var GameState = function(game) {
2014-06-11 10:05:02 -07:00
}
2014-06-11 21:17:23 -07:00
function addAnimation(obj, anim)
{
a = moonlightSettings['animations'][anim]
obj.animations.add(anim, a['frames'], a['speed'], a['loop'])
}
2014-06-11 10:06:03 -07:00
GameState.prototype.preload = function()
{
for (var k in moonlightSettings['map']['tilesets']) {
var ts = moonlightSettings['map']['tilesets'][k];
this.load.image(ts['name'], ts['path']);
}
for (var k in moonlightSettings['images']) {
var i = moonlightSettings['images'][k];
this.load.image(i['name'], i['path']);
}
2014-06-13 22:35:55 -07:00
for (var k in moonlightSettings['sounds']) {
var s = moonlightSettings['sounds'][k];
2014-06-13 22:52:02 -07:00
this.load.audio(s['name'], s['path']);
2014-06-13 22:35:55 -07:00
}
for (var k in moonlightSettings['spritesheets']) {
var s = moonlightSettings['spritesheets'][k]
game.load.spritesheet(s['name'], s['path'], s['width'], s['height'], s['frames'])
}
2014-06-12 22:09:23 -07:00
this.load.tilemap('map',
moonlightSettings['map']['path'],
null,
Phaser.Tilemap.TILED_JSON);
2014-06-12 22:11:16 -07:00
}
GameState.prototype.create = function()
{
this.map = this.add.tilemap('map');
for (var k in moonlightSettings['map']['tilesets']) {
var ts = moonlightSettings['map']['tilesets'][k];
2014-06-12 22:10:12 -07:00
this.map.addTilesetImage(ts['name']);
}
2014-06-12 21:57:03 -07:00
this.map_collision_layers = [];
for (var ln in moonlightSettings['map']['layers']) {
2014-06-12 21:58:51 -07:00
lp = moonlightSettings['map']['layers'][ln];
2014-06-12 23:04:33 -07:00
if ( lp['type'] == "tiles" ) {
layer = this.map.createLayer(ln);
this.map.setCollisionBetween(
lp['collisionBetween'][0],
lp['collisionBetween'][1],
lp['collides'],
ln
);
2014-06-13 00:25:39 -07:00
if ( lp['inject_sprites'] == true ) {
this.aiSprites = game.add.group();
this.map.createFromObjects('AI', 3544, 'player', 0, true, false, this.aiSprites, AISprite);
this.aiSprites.forEach(function(spr) {
spr.update_new_values();
}, this)
2014-06-15 11:55:00 -07:00
player = this.add.sprite((10 * 32), (17 * 32), 'player');
player.lightmeter = 0;
2014-06-13 00:25:39 -07:00
};
2014-06-12 23:04:33 -07:00
if ( lp['collides'] == true ) {
this.map_collision_layers.push(layer);
}
layer.resizeWorld();
2014-06-12 22:20:58 -07:00
}
2014-06-12 21:57:03 -07:00
}
2014-06-12 23:04:33 -07:00
this.physics.arcade.enable(player);
2014-06-12 22:33:32 -07:00
player.body.center = new Phaser.Point(player.body.width / 2, player.body.height + player.body.halfHeight);
player.body.collideWorldBounds = true;
2014-06-11 21:17:23 -07:00
addAnimation(player, 'bipedwalkleft');
addAnimation(player, 'bipedwalkright');
addAnimation(player, 'bipedwalkup');
addAnimation(player, 'bipedwalkdown');
addAnimation(player, 'bipedrunleft');
addAnimation(player, 'bipedrunright');
addAnimation(player, 'bipedrunup');
addAnimation(player, 'bipedrundown');
this.camera.follow(player, Phaser.Camera.FOLLOW_TOPDOWN);
controls = game.input.keyboard.createCursorKeys();
2014-06-15 11:54:14 -07:00
this.effectSprites = game.add.group();
this.map.createFromObjects('EffectSprites', 5, 'player', 0, true, false, this.effectSprites, EffectSprite);
this.effectSprites.forEach(function(spr) {
spr.update_new_values();
}, this)
this.shadowTexture = game.add.bitmapData(game.world.width, game.world.height);
2014-06-11 21:26:31 -07:00
// drop this lower to make the map darker
2014-06-14 16:27:37 -07:00
this.shadowTextureColor = 'rgb(60, 60, 60)';
this.shadowSprite = game.add.image(0, 0, this.shadowTexture);
this.shadowSprite.blendMode = Phaser.blendModes.MULTIPLY;
2014-06-12 23:28:15 -07:00
this.staticLights = game.add.group();
2014-06-12 23:31:36 -07:00
this.map.createFromObjects('Lights', 97, 'player', 0, true, false, this.staticLights, Light);
2014-06-12 23:55:02 -07:00
this.staticLights.forEach(function(light) {
2014-06-13 00:03:27 -07:00
light.update_new_values();
2014-06-12 23:55:02 -07:00
}, this)
2014-06-13 22:35:55 -07:00
this.staticSounds = game.add.group();
2014-06-14 00:16:17 -07:00
this.map.createFromObjects('Sounds', 11, 'player', 0, true, false, this.staticSounds, SoundSprite);
2014-06-13 22:35:55 -07:00
this.staticSounds.forEach(function(snd) {
2014-06-13 22:40:13 -07:00
snd.update_new_values();
2014-06-13 22:35:55 -07:00
}, this)
2014-06-14 20:39:03 -07:00
this.uigroup = game.add.group();
this.game.time.advancedTiming = true;
this.fpsText = this.game.add.text(
20, 20, '', { font: '16px Arial', fill: '#ffffff' }, this.uigroup
);
this.lightbox = this.game.add.image(game.camera.width / 2 - 50,
game.camera.height - 40,
'lightbox',
0,
this.uigroup);
this.lightbar = this.game.add.image(this.lightbox.x + 3,
this.lightbox.y + 3,
'lightbar',
0,
this.uigroup);
2014-06-14 20:48:38 -07:00
this.lightbar_image = game.cache.getImage('lightbar');
2014-06-14 20:51:42 -07:00
this.lightbar_crop = new Phaser.Rectangle(0,
0,
2014-06-14 20:48:38 -07:00
game.cache.getImage('lightbar').width,
game.cache.getImage('lightbar').height);
2014-06-14 20:39:03 -07:00
this.uigroup.setAll('fixedToCamera', true);
}
2014-06-11 10:06:03 -07:00
GameState.prototype.updateShadowTexture = function() {
this.shadowTexture.context.fillStyle = this.shadowTextureColor;
2014-06-11 11:05:59 -07:00
this.shadowTexture.context.fillRect(0, 0, game.world.width, game.world.height);
2014-06-11 19:57:11 -07:00
this.staticLights.forEach(function(light) {
2014-06-13 01:21:50 -07:00
if ( light.always_render !== true ) {
var r1 = new Phaser.Rectangle(this.game.camera.x,
this.game.camera.y,
this.game.camera.width,
this.game.camera.height);
if ( ! light.rect.intersects(r1) ) {
2014-06-13 01:21:50 -07:00
return;
}
2014-06-11 20:01:00 -07:00
}
if ( light.flicker ) {
var radius = light.radius + game.rnd.integerInRange(1,10);
} else {
var radius = light.radius;
}
2014-06-14 20:27:00 -07:00
light.rendered_radius = radius;
var gradient =
this.shadowTexture.context.createRadialGradient(
2014-06-11 11:00:09 -07:00
light.x + 16, light.y + 16, light.fade,
2014-06-11 10:49:02 -07:00
light.x + 16, light.y + 16, radius);
2014-06-12 23:55:02 -07:00
gradient.addColorStop(0, light.color_start);
gradient.addColorStop(1, light.color_stop);
this.shadowTexture.context.beginPath();
this.shadowTexture.context.fillStyle = gradient;
2014-06-11 10:49:39 -07:00
this.shadowTexture.context.arc(light.x + 16, light.y + 16, radius, 0, Math.PI*2);
this.shadowTexture.context.fill();
}, this);
this.shadowTexture.dirty = true;
};
2014-06-14 11:59:41 -07:00
function delState(spr, state)
{
2014-06-14 12:10:24 -07:00
if ( hasState(spr, state) )
spr.state = spr.state ^ state;
2014-06-14 11:59:41 -07:00
}
function addState(spr, state)
{
spr.state = spr.state | state;
}
2014-06-14 12:10:24 -07:00
function setMovingState(spr, state)
{
delState(spr, STATE_FACE_LEFT);
delState(spr, STATE_FACE_RIGHT);
2014-06-14 12:10:50 -07:00
delState(spr, STATE_FACE_DOWN);
2014-06-14 12:10:24 -07:00
delState(spr, STATE_FACE_UP);
delState(spr, STATE_MOVING);
delState(spr, STATE_RUNNING);
2014-06-14 12:11:59 -07:00
addState(spr, state);
2014-06-14 12:10:24 -07:00
}
function setAwarenessState(spr, state)
{
delState(spr, STATE_UNAWARE);
delState(spr, STATE_CONCERNED);
delState(spr, STATE_ALERTED);
delState(spr, STATE_LOSTHIM);
addState(spr, state);
}
2014-06-14 11:59:41 -07:00
function exchangeState(spr, state1, state2)
{
delState(spr, state1);
addState(spr, state2);
}
function hasState(spr, state)
{
2014-06-14 12:05:34 -07:00
if ( (spr.state & state) == state )
2014-06-14 12:03:53 -07:00
return true;
return false;
2014-06-14 11:59:41 -07:00
}
function spriteFacing(spr)
{
if ( hasState(spr, STATE_FACE_LEFT) )
return "left";
if ( hasState(spr, STATE_FACE_RIGHT) )
return "right";
if ( hasState(spr, STATE_FACE_DOWN) )
return "down";
if ( hasState(spr, STATE_FACE_UP) )
return "up";
}
2014-06-14 16:46:50 -07:00
function parseBoolean(val)
{
return ( val == 'true' || val == true );
}
2014-06-14 11:59:41 -07:00
function setSpriteMovement(spr)
{
var x = 0;
var y = 0;
2014-06-14 11:59:41 -07:00
var dir = spriteFacing(spr);
spr.body.setSize(16, 16, 8, 16);
2014-06-14 11:59:41 -07:00
if ( hasState(spr, STATE_RUNNING) ) {
x = 200;
y = 200;
spr.animations.play("bipedrun" + dir);
2014-06-14 11:59:41 -07:00
} else if ( hasState(spr, STATE_MOVING) ) {
x = 75;
y = 75;
spr.animations.play("bipedwalk" + dir);
2014-06-14 11:59:41 -07:00
} else {
spr.body.velocity.x = 0;
spr.body.velocity.y = 0;
spr.animations.stop();
return;
}
if ( dir == "left" ) {
spr.body.velocity.x = -x;
spr.body.velocity.y = 0;
} else if ( dir == "right" ) {
spr.body.velocity.x = x;
spr.body.velocity.y = 0;
} else if ( dir == "up" ) {
spr.body.velocity.x = 0;
spr.body.velocity.y = -y;
} else if ( dir == "down" ) {
spr.body.velocity.x = 0;
spr.body.velocity.y = y;
}
}
2014-06-11 10:06:03 -07:00
GameState.prototype.check_input = function()
{
player.body.velocity.x = 0;
player.body.velocity.y = 0;
velocityMod = 0;
2014-06-14 11:59:41 -07:00
var newstate = 0;
if ( controls.up.isDown) {
2014-06-14 11:59:41 -07:00
if ( controls.up.shiftKey ) {
2014-06-14 12:12:38 -07:00
newstate = (STATE_FACE_UP | STATE_MOVING | STATE_RUNNING);
2014-06-14 11:59:41 -07:00
} else {
2014-06-14 12:12:38 -07:00
newstate = (STATE_FACE_UP | STATE_MOVING );
2014-06-14 11:59:41 -07:00
}
} else if ( controls.down.isDown ) {
2014-06-14 11:59:41 -07:00
if ( controls.down.shiftKey ) {
newstate = (STATE_FACE_DOWN | STATE_MOVING | STATE_RUNNING);
} else {
newstate = (STATE_FACE_DOWN | STATE_MOVING );
}
} else if ( controls.left.isDown ) {
2014-06-14 11:59:41 -07:00
if ( controls.left.shiftKey ) {
newstate = (STATE_FACE_LEFT | STATE_MOVING | STATE_RUNNING);
} else {
newstate = (STATE_FACE_LEFT | STATE_MOVING );
}
} else if ( controls.right.isDown ) {
2014-06-14 11:59:41 -07:00
if ( controls.right.shiftKey ) {
newstate = (STATE_FACE_RIGHT | STATE_MOVING | STATE_RUNNING);
} else {
newstate = (STATE_FACE_RIGHT | STATE_MOVING );
}
} else {
2014-06-14 11:59:41 -07:00
newstate = STATE_NONE;
}
2014-06-14 11:59:41 -07:00
2014-06-14 12:10:24 -07:00
setMovingState(player, newstate);
2014-06-14 11:59:41 -07:00
setSpriteMovement(player);
}
2014-06-14 20:04:21 -07:00
GameState.prototype.update_player_lightmeter = function() {
2014-06-14 20:03:55 -07:00
lightValue = 0;
this.staticLights.forEach(function(light) {
2014-06-14 20:30:17 -07:00
var left = player.x;
2014-06-14 20:31:33 -07:00
var top = player.y + 32;
2014-06-14 20:30:17 -07:00
if ( player.y < this.y )
2014-06-14 20:31:33 -07:00
top = player.y;
2014-06-14 20:30:17 -07:00
if ( player.x + this.x )
left = player.x + 32;
line = new Phaser.Line(left, top, light.x + 16, light.y + 16);
2014-06-14 20:27:00 -07:00
if ( line.length > light.rendered_radius)
2014-06-14 20:03:55 -07:00
return;
2014-06-14 20:24:01 -07:00
var length = line.length;
2014-06-14 20:27:00 -07:00
var lv = light.light_meter - (Number(length) / Number(light.rendered_radius));
2014-06-14 20:03:55 -07:00
if ( lv > lightValue ) {
lightValue = lv;
}
}, this)
player.lightmeter = lightValue;
2014-06-14 20:50:45 -07:00
this.lightbar_crop.width = (this.lightbar_image.width * lightValue);
2014-06-14 20:48:38 -07:00
this.lightbar.crop(this.lightbar_crop);
2014-06-14 20:03:55 -07:00
}
2014-06-11 10:06:03 -07:00
GameState.prototype.update = function()
{
2014-06-11 10:06:32 -07:00
this.check_input();
2014-06-14 20:03:55 -07:00
this.update_player_lightmeter();
2014-06-12 22:26:57 -07:00
for (var ln in this.map_collision_layers ) {
layer = this.map_collision_layers[ln];
2014-06-12 21:57:03 -07:00
this.physics.arcade.collide(player, layer);
}
2014-06-11 21:09:22 -07:00
2014-06-13 22:54:06 -07:00
function _fix_audio_relative(x) {
2014-06-13 23:23:28 -07:00
x.adjust_relative_to(player);
2014-06-13 22:54:06 -07:00
}
this.staticSounds.forEach(_fix_audio_relative, this);
2014-06-11 21:46:32 -07:00
function _inner_collide(x) {
if ( x.collide_with_map == true ) {
for ( var ln in this.map_collision_layers ) {
layer = this.map_collision_layers[ln];
this.physics.arcade.collide(x, layer);
}
2014-06-12 21:57:03 -07:00
}
if ( x.collide_with_player == false )
return;
if ( x.canSeeSprite(player, false) == true ) {
if ( player.lightmeter >= x.sprite_can_see_lightmeter ) {
x.setAwarenessEffect(STATE_ALERTED);
} else {
x.setAwarenessEffect(STATE_CONCERNED);
}
} else {
x.setAwarenessEffect(STATE_LOSTHIM);
}
2014-06-11 21:46:32 -07:00
this.physics.arcade.collide(x, player);
2014-06-11 21:42:36 -07:00
}
this.effectSprites.forEach(_inner_collide, this);
2014-06-11 22:05:54 -07:00
this.aiSprites.forEach(_inner_collide, this);
this.updateShadowTexture();
2014-06-14 16:17:08 -07:00
// function _draw_viewrect(x) {
// var r = x.viewRectangle();
// if ( r == null )
// return;
// this.shadowTexture.context.fillStyle = 'rgb(128, 128, 128)';
// this.shadowTexture.context.fillRect(r.left,
// r.top,
// r.width,
// r.height);
// }
// this.aiSprites.forEach(_draw_viewrect, this);
2014-06-14 16:17:08 -07:00
2014-06-12 00:23:31 -07:00
if (game.time.fps !== 0) {
this.fpsText.setText(game.time.fps + ' FPS');
}
}
2014-06-11 10:05:02 -07:00
game.state.add('game', GameState, true);