user218130
user218130

Reputation: 1

Creating a line of sight

I'm creating a survival zombie game and when the zombie is close to the player the player can attack the zombie. How can I create a line of sight of the player and if the zombie is in a specific radius and angle of the player (Basically close to the player) then the player can attack. I manage to work out the player facing forward but how can i create the angle like +- 15 from the way the player is facing and if the zombies are within the radius and angle of the player. So far this is what I have done

package 
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import flash.display.Graphics;

public class Player extends MovieClip
{
//Player Setting
var walkSpeed:Number = 4;
var walkRight:Boolean = false;
var walkLeft:Boolean = false;
var walkUp:Boolean = false;
var walkDown:Boolean = false;
var attacking:Boolean = false;
var radius:Number = 60;

public function Player()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,walk);
addEventListener(Event.ENTER_FRAME,Update);
stage.addEventListener(KeyboardEvent.KEY_UP,stopWalk);
stage.addEventListener(MouseEvent.CLICK,attack);
}

function walk(event:KeyboardEvent)
{
//When Key is Down
if (event.keyCode == 68)
{
walkRight = true;
}
if (event.keyCode == 87)
{
walkUp = true;
}
if (event.keyCode == 65)
{
walkLeft = true;
}
if (event.keyCode == 83)
{
walkDown = true;
}
}

function Update(event:Event)
{

//if attacking is true then key moves are false;
if ((attacking == true))
{
walkRight = false;
walkLeft = false;
walkUp = false;
walkDown = false;
}
else if ((attacking == false))
{
//Else if attacking is false then move and rotate to mouse;
var dx = parent.mouseX - x;
var dy = parent.mouseY - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;

                               var radiusx:Number = x+radius*Math.cos(rotation/180*Math.PI);
       var radiusy:Number = y+radius*Math.sin(rotation/180*Math.PI);

if ((walkRight == true))
{
x +=  walkSpeed;
gotoAndStop(2);
}
if ((walkUp == true))
{
y -=  walkSpeed;
gotoAndStop(2);
}
if ((walkLeft == true))
{
x -=  walkSpeed;
gotoAndStop(2);
}
if ((walkDown == true))
{
y +=  walkSpeed;
gotoAndStop(2);
}
}
}

//Calculate the distance between the two
public function distanceBetween(player:Object,zombies:Object):Number
{
var dx:Number = player.x - zombies.x;
var dy:Number = player.y - zombies.y;
return Math.sqrt(((dx * dx) + dy * dy));
}

function stopWalk(event:KeyboardEvent)
{
if ((attacking == false))
{
if (event.keyCode == 68)
{
event.keyCode = 0;
walkRight = false;
gotoAndStop(1);
}
if (event.keyCode == 87)
{
event.keyCode = 0;
walkUp = false;
gotoAndStop(1);
}
if (event.keyCode == 65)
{
event.keyCode = 0;
walkLeft = false;
gotoAndStop(1);
}
if (event.keyCode == 83)
{
event.keyCode = 0;
walkDown = false;
gotoAndStop(1);
}
}
}

function attack(event:MouseEvent)
{
if ((attacking == false))
{
attacking = true;
gotoAndStop(3);
if (hitTestObject(MovieClip(root).Zo))
{
trace('ouch');

}
}
}
}
}

Upvotes: 0

Views: 120

Answers (1)

prototypical
prototypical

Reputation: 6751

In psuedo code, here's one way you could do this :

// check distance between player and zombie
  // if zombie is close enough 
       //get angle between player and zombie
       //check difference between player facing and angle
       //if difference between angle <= 15 degrees 
          //attack zombie

Here is a function that will return the angle from your player to a given target :

// I used the code from your code above to create this function
function getAngle(targetX:Number, targetY:Number):Number
{
     var dx = targetX - player.x;
     var dy = targetY - player.y;
     var angle = Math.atan2(dy,dx) / Math.PI * 180;

     return angle;
}

This is the distance function in your code, that I modified to accept a movieclips :

public function distanceBetween(player:MovieClip,zombie:MovieClip):Number
{
    var dx:Number = player.x - zombie.x;
    var dy:Number = player.y - zombie.y;
    return Math.sqrt(((dx * dx) + dy * dy));
}

So you could use the psuedo code above to create code to solve your problem :

// check distance between player and zombie
if (distanceBetween(player,zombie) <= maxRadius)
{
    // get angle between player and zombie
    var angleToZombie:Number = getAngle(zombie.x, zombie.y);

    // I am assuming that the facing is going to be towards the mouse
    // calculate facing of player
    var currentFacing:Number = getAngle(mouseX, mouseY);

    //check difference between player facing and angle
    if (Math.abs(currentFacing - angleToZombie) <= 15)
    {
         // attack the zombie
    }
}

Upvotes: 1

Related Questions