From 5ac3159e389d9ba3d841231b20194c1ef9ae2d51 Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Thu, 26 Jun 2014 07:15:38 -0700 Subject: [PATCH] Refactor away from '== null' checks towards isSet --- moonlight/src/AISprite.js | 45 ++++++++++++++++++------------------ moonlight/src/GameStates.js | 4 ++-- moonlight/src/SoundSprite.js | 6 ++--- moonlight/src/Util.js | 2 +- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/moonlight/src/AISprite.js b/moonlight/src/AISprite.js index 3d3a246..dd1f6ff 100644 --- a/moonlight/src/AISprite.js +++ b/moonlight/src/AISprite.js @@ -48,7 +48,7 @@ var AISprite = function(game, x, y, key, frame) { } var viewrect = this.viewRectangle(); - if ( viewrect == null ) { + if ( isSet(viewrect) == false ) { return false; } var sprrect = positiveRectangle(spr.x, spr.y, 32, 32); @@ -64,7 +64,7 @@ var AISprite = function(game, x, y, key, frame) { this.startAwarenessTimer = function() { this.awareness_change_enabled = false; - if ( this.awareness_timer !== null ) + if ( isSet(this.awareness_timer) ) this.awareness_timer.stop(); this.awareness_timer = game.time.create(false); this.awareness_timer.add(this.sprite_awareness_duration, @@ -94,7 +94,7 @@ var AISprite = function(game, x, y, key, frame) { this.startAwarenessTimer(); setAwarenessState(this, state); - if ( this.awareness_effect !== null ) { + if ( isSet(this.awareness_effect) ) { this.awareness_effect.alive = false; this.awareness_effect.destroy(); this.awareness_effect = null; @@ -138,7 +138,7 @@ var AISprite = function(game, x, y, key, frame) { } this.clearWordBubble = function() { - if ( this.bubble_text !== null ) + if ( isSet(this.bubble_text) ) this.clear_bubble = true; this.enable_word_bubble = false; this.timer = game.time.create(false); @@ -148,7 +148,7 @@ var AISprite = function(game, x, y, key, frame) { this.setWordBubble = function() { - if ( this.bubble_text !== null || + if ( isSet(this.bubble_text) || this.sprite_group == undefined || this.enable_world_bubble == false) { return; @@ -336,7 +336,7 @@ var AISprite = function(game, x, y, key, frame) { this.animations.stop(); this.animations.play(getMovingAnimationName(this)); this.animations.stop(); - if ( this.rotation_timer !== null ) { + if ( isSet(this.rotation_timer) ) { this.rotation_timer.stop(); this.rotation_timer = null; } @@ -358,7 +358,7 @@ var AISprite = function(game, x, y, key, frame) { this.path_set(target, true, maxsteps, useNearestWalkable); this.path_tween_start(movingstate); } else { - if ( this.rotation_timer == null ) { + if ( isSet(this.rotation_timer) == false ) { this.rotation_timer = game.time.create(false); timerev = this.rotation_timer.add(250, this.turnUnseenDirection, this); this.rotation_timer.start() @@ -406,26 +406,27 @@ var AISprite = function(game, x, y, key, frame) { this.action_reportplayer = function() { if ( (this.path.length < 1) || this.path_index >= this.path.length) { - if ( this.target == null && + if ( isSet(this.target) == false && hasState(this, STATE_RUNNINGTOLIGHT) == false ) { var aiSprites = game.state.states.game.aiSprites; this.target = nearestInGroup(this, aiSprites, "townsfolk-guard"); } else if ( hasState(this, STATE_RUNNINGTOLIGHT) == false ) { - if ( this.target.rotation_timer !== null ) { - this.target.rotation_timer.stop(); - this.target.rotation_timer = null; + if ( isSet(this.target) ) { + if ( isSet(this.target.rotation_timer) ) { + this.target.rotation_timer.stop(); + this.target.rotation_timer = null; + } + if ( isSet(this.target.sprite_group) ) { + this.target.path_purge(); + this.target.setAwarenessEffect(STATE_ALERTED); + this.target.target = this.lastSawPlayerAt; + addState(this.target, STATE_RUNNINGTOREPORT); + } } - this.target.path_purge(); - this.target.setAwarenessEffect(STATE_ALERTED); - this.target.target = this.lastSawPlayerAt; - addState(this.target, STATE_RUNNINGTOREPORT); - this.path_tween_stop(); this.path_purge(); var staticLights = game.state.states.game.staticLights; this.target = nearestInGroup(this, staticLights); - console.log("Running to the nearest light"); - console.log(this.target); addState(this, STATE_RUNNINGTOLIGHT); } else { this.awareness_timer.stop(); @@ -437,7 +438,7 @@ var AISprite = function(game, x, y, key, frame) { return; } } - if ( this.target !== null ) { + if ( isSet(this.target) ) { this.chasetarget(this.target, STATE_ALERTED, STATE_MOVING | STATE_RUNNING, @@ -469,7 +470,7 @@ var AISprite = function(game, x, y, key, frame) { { if ( this.ready_to_update == false ) return; - if ( this.awareness_effect !== null ) { + if ( isSet(this.awareness_effect) ) { if ( this.awareness_effect.alive == false ) { this.awareness_effect.destroy(); this.awareness_effect = null; @@ -479,7 +480,7 @@ var AISprite = function(game, x, y, key, frame) { } } - if ( this.bubble_text !== null ) { + if ( isSet(this.bubble_text) ) { if ( this.clear_bubble == true ) { this.bubble_text.destroy(); this.bubble_sprite.destroy(); @@ -505,7 +506,7 @@ var AISprite = function(game, x, y, key, frame) { } this.update_new_values = function() { - if ( this.timer !== null ) + if ( isSet(this.timer) ) this.timer.stop(); this.animations.destroy(); this.clearWordBubble(); diff --git a/moonlight/src/GameStates.js b/moonlight/src/GameStates.js index 1f676c0..dd1e23d 100644 --- a/moonlight/src/GameStates.js +++ b/moonlight/src/GameStates.js @@ -281,7 +281,7 @@ GameState.prototype.update = function() if ( this.aiSprites.debug == true ) { function _draw_viewrect(x) { var r = x.viewRectangle(); - if ( r == null ) + if ( isSet(r) ) return; this.shadowTexture.context.fillStyle = 'rgb(128, 128, 128)'; this.shadowTexture.context.fillRect(r.left, @@ -292,7 +292,7 @@ GameState.prototype.update = function() this.aiSprites.forEach(_draw_viewrect, this); function _draw_aipath(x) { var p = x.path; - if ( p == null ) + if ( isSet(p) ) return; this.shadowTexture.context.fillStyle = 'rgb(255, 128, 128)'; p.forEach(function(r) { diff --git a/moonlight/src/SoundSprite.js b/moonlight/src/SoundSprite.js index c15208d..c548126 100644 --- a/moonlight/src/SoundSprite.js +++ b/moonlight/src/SoundSprite.js @@ -29,8 +29,8 @@ 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 ) { + if ( isSet(this.sound_key) == false ) { + if ( isSet(this.sound) ) { this.sound.stop(); } return; @@ -42,7 +42,7 @@ SoundSprite.prototype.update_new_values = function() { this.sound_forcerestart = parseBoolean(this.sound_forcerestart); this.sound_nofade = parseBoolean(this.sound_nofade); - if ( this.sound !== null ) + if ( isSet(this.sound) ) this.sound.stop(); this.sound = game.add.audio(this.sound_key, this.sound_volume, this.sound_loop); this.sound.play( diff --git a/moonlight/src/Util.js b/moonlight/src/Util.js index a6bd694..2f609d9 100644 --- a/moonlight/src/Util.js +++ b/moonlight/src/Util.js @@ -103,7 +103,7 @@ function nearestWalkableTile(spr) for ( var i = 1 ; i < 100 ; i++ ) { var rv = _walkable_inner(i); - if ( rv !== null ) { + if ( isSet(rv) ) { console.log("Found near walkable tile"); console.log([rv] + [spr.x / 32, spr.y / 32]); return rv