Game Development Reference
In-Depth Information
The checkLineOfSight function
The checkLineOfSight function is used to allow the enemy to turn and fire at the player. What is
the line of sight? The enemy tanks will turn and fire at the player only if there are no walls
blocking their view of the player tank. We calculate this by finding the direction the player is in
relation to the enemy tank, and then we loop though all of the tiles separating them in a straight
line (only strictly vertical and horizontal). During this loop, if we hit a wall or go off the screen, then
the enemy tank will not fire at the player. If we reach the same tile as the player, the enemy tank
will fire at the player.
An attribute of the Level instance called enemyIntelligence will be used to determine the
likelihood of the enemy using line of sight if it is not in the same region as the player. This number
will be a value from 0-100. The greater the number, the better the chance is that the enemy will
turn and fire at the player if it is in the line of sight of the enemy tank. We have not implemented
any of the level-specific variables (such as enemyIntelligence ) for the enemy yet, but those will
be added when we clean up the final game later in this chapter. For now, we'll use a placeholder
value of 50 for the enemyIntelligence , as this variable is named in the following code:
private function checkLineOfSight(prey:TileByTileBlitSprite,
predator:TileByTileBlitSprite):void {
//rotation reference
//up=0;
//right=90;
//down=180;
//left=-90;
var testDX:int;
var testDY:int;
var testRotation:int;
var checkCol:int;
var checkRow:int;
var difference:int;
var differenceCtr:int;
var act:Boolean = false;
//1. test if they are in the same col or row
//2. if in same col (horizontalDiff), check if verticalDiff is pos or neg
//3. if in same row (verticalDiff), check if horizontalDiff is pos or neg
//**placeholder
var enemyIntelligence:int = 50;
if (prey.currentRegion == predator.currentRegion) {
act = true;
}else if (int(Math.random() * 100) < enemyIntelligence) {
//trace("act based on intel");
//if not in same region, then turn toward player
//first be sure to turn toward the player
var horizontalDiff:int = predator.currCol - prey.currCol;
var verticalDiff:int = predator.currRow - prey.currRow;
if (verticalDiff >0) {
Search WWH ::




Custom Search