//basic constructor at some point more of these variables will be XML read and passed in instead of hard-coded. public Enemy(int startX, int startY) : base(new Vector2(startX, startY)) { this.Scale = ENEMYSCALE; this.lazerBeam = new Beam(); this.exlcamationPoint = new Sprite(); exlcamationPoint.Scale = .4f; questionMark = new Sprite(); questionMark.Scale = QUESTION_SCALE; this.isDead = false; this.isPatrolling = true; this.isInvestigating = false; this.canFallWhilePatrolling = false; this.hasStartedLazer = false; this.onGround = false; this.maxSpeed = 200f; this.patrolStart = startX; this.velocity.X = (float) PATROLSPEED; timeSinceLastFire = 0; this.patrolspeed = PATROLSPEED; this.patrolRange = PATROL_RANGE; this.vertViewRange = VERT_VIEW_RANGE; this.horizViewRange = HORIZ_VIEW_RANGE; this.soundRange = SOUND_RANGE; this.investigateMaxWait = INVESTIGATE_MAX_WAIT; this.lazerChargeTime = LAZER_CHARGE_TIME; this.lazerDuration = LAZER_DURATION; this.fireInterval = MIN_FIRE_INTERVAL; this.weaponType = WEAPON_DEFAULT; emitter = new Particle_Engine.LazerChargeEmitter(this.position, lazerChargeTime); }
//update things. will eventually check line-of-sight, shooting, and walking public int Update(float dt, Player player, Bullet[] bullets,Tile[,] layout, Boolean noise,Boolean fallNoise, Tile[] nearby) { Vector2 playerPosition = player.position; int retval = -1; //if the guard is patrolling, let it patrol if (this.isPatrolling) { drawQuestionMark = false; if (patrolRange == 0 && Math.Abs(this.position.X - patrolStart) <= 5) { velocity.X = 0; } else if (position.X >= patrolStart + patrolRange || this.collideRight) { velocity.X = -patrolspeed; } else if (position.X <= patrolStart - patrolRange || this.collideLeft) { velocity.X = patrolspeed; } } //if it can see the player, shoot at it! if (this.canSeePlayer(playerPosition, layout)) { drawQuestionMark = false; drawExclamationPoint = true; //dont resume patrol if we can see the player! waitTimer = 0; if (weaponType == 1) { timeSinceLastFire += dt; if (timeSinceLastFire > fireInterval) { fireBullet(this.position - playerPosition, bullets); timeSinceLastFire = 0; retval = 1; } } else if (weaponType == 2) { if (!hasStartedLazer) { hasStartedLazer = true; timeSinceLastFire = 0; Particle_Engine.ParticleEngine.removeEmmiter(emitter); emitter = new Particle_Engine.LazerChargeEmitter(this.position, lazerChargeTime); Particle_Engine.ParticleEngine.addEmitter(emitter); } else { timeSinceLastFire += dt; velocity.X = 0; if (timeSinceLastFire > lazerChargeTime) { shouldFire = true; } if (timeSinceLastFire + 1 > lazerChargeTime) retval = 2; } } isPatrolling = false; isInvestigating = true; destination = playerPosition; } else { if (hasStartedLazer) Particle_Engine.ParticleEngine.removeEmmiter(emitter); shouldFire = false; drawExclamationPoint = false; hasStartedLazer = false; timeSinceLastFire = fireInterval + LAZER_CHARGE_TIME + 100; } //if there is noise while patrolling if (noise) { Vector2 distance = player.theHook.position - this.position; //if the guard can actually hear it if (distance.Length() < soundRange) { waitTimer = 0; this.isPatrolling = false; this.isInvestigating = true; drawQuestionMark = true; this.destination = player.theHook.position; } } if (fallNoise) { Vector2 distance = player.position - this.position; //if the guard can actually hear it if (distance.Length() < soundRange) { waitTimer = 0; this.isPatrolling = false; drawQuestionMark = true; this.isInvestigating = true; this.destination = player.position; } } //if we're investigating something (like a noise, say) if (this.isInvestigating) { drawQuestionMark = true; Vector2 distance = this.destination - this.position; //if we're close or if the next tile isn't solid, stop or if we seem to have gotten stuck if (Math.Abs(distance.X) <= 5 || !this.nextTileIsSolid(nearby) || collideLeft || collideRight) { this.velocity.X = 0; waitTimer += dt; if (waitTimer > investigateMaxWait) { isInvestigating = false; drawQuestionMark = false; isPatrolling = true; this.velocity.X = -patrolspeed * ((this.position.X - this.patrolStart) / (Math.Abs(this.position.X - this.patrolStart))); waitTimer = 0; } } //otherwise continue (or start) moving at PATROLSPEED towards the noise else if (distance.X * velocity.X <= 0 && !hasStartedLazer) { this.velocity.X = patrolspeed * (distance.X / Math.Abs(distance.X)); } } if (this.hasStartedLazer) { if (timeSinceLastFire - lazerChargeTime >= lazerDuration) { timeSinceLastFire = 0; this.hasStartedLazer = false; } } if (velocity.X < -.1f) facingRight = false; if (velocity.X > .1f) facingRight = true; if (!nextTileIsSolid(nearby)) { this.velocity.X *= -1; } applyGravity(); base.Update(dt); return retval; }