Creating Enemy Cone of vision

333 Views Asked by At

I am trying to create a cone of vision for my enemy class. Right now it checks to see if the player is within a radius before moving towards them and will move around randomly if they are not. I want to give it vision so the enemy does not always rotate towards the player.

Enemy Class

package 
{
    import flash.automation.ActionGenerator;
    import flash.events.Event;
    import flash.geom.Point;

    public class Enemy extends GameObject
    {

        var isDead:Boolean = false;

        var speed:Number = 3;

        var originalValue:Number = 50;

        var changeDirection:Number = originalValue;



        var checkDirection:Number = 49;

        var numberGen:Number;

        //var startTime:int = getTimer();

        //var $timer = getTimer();

        //var myTimer:int = getTimer();
        //var moveTimer:int = timer - $timer;

        public function Enemy()
        {
            super();
            target = target_mc;
        }

        override public function update():void
        {
            movement();

        }

        private function movement():void
        {
            if (changeDirection >= 0)
            {                   
                if (changeDirection > checkDirection)
                {
                    numberGen = (Math.random() * 360) / 180 * Math.PI;

                }

                var velocity:Point = new Point();
                var $list:Vector.<GameObject >  = getType(Player);

                var $currentDistance:Number = Number.MAX_VALUE;
                for (var i:int = 0; i < $list.length; i++)
                {
                    var currentPlayer:GameObject = $list[i];
                    if (MathUtil.isWithinRange(currentPlayer.width,currentPlayer.x,currentPlayer.y,width,x,y))
                    {
                        var $delta:Point = new Point  ;
                        $delta.x = currentPlayer.x - x;
                        $delta.y = currentPlayer.y - y;
                        if ($currentDistance > $delta.length)
                        {
                            $currentDistance = $delta.length;
                            velocity = $delta;
                            velocity.normalize(speed);
                        }
                    }
                }
                if(velocity.length == 0)
                    {
                        velocity = Point.polar(2, numberGen);
                    }

                    changeDirection--;

                    if (changeDirection == 0)
                    {
                        changeDirection = originalValue;
                    }
            }

            velocity.x = Math.floor(velocity.x * 10) * .1;
            velocity.y = Math.floor(velocity.y * 10) * .1;


            moveBy([Wall, Door], velocity.x, velocity.y, collides_mc);

            var $collides:GameObject = collision([Player]);
            if ($collides != null)
            {
                destroy();
            }

            if ($collides == null)
            {
                $collides = collision([Player],this);
                if ($collides != null)
                {
                    $collides.destroy();
                }
            }

            rotation = (Math.atan2(velocity.y, velocity.x) * 180 / Math.PI);
            collides_mc.rotation = -rotation;

            trace($collides);

        }

        override public function onCollideX($collision:GameObject):void
        {

        }

        override public function onCollideY($collision:GameObject):void
        {

        }

    }

}

GameObject Class, which contains all the functions and Enemy extends it

package
{
    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.sensors.Accelerometer;
    import flashx.textLayout.elements.ListElement;

    public class GameObject extends MovieClip
    {

        static public var list:Vector.<GameObject> = new Vector.<GameObject>;
        protected var hitBox:Sprite;

        public var target:MovieClip;        

        public function GameObject()
        {
            target=this;
            list.push(this);
        }

        public function update():void
        {

        }

        public function collision(typesColliding:Array, $target:DisplayObject = null):GameObject
        {
            if($target == null)
                $target = target;

            for (var i:int = list.length - 1; i >= 0; i--)
            {
                var item:GameObject = list[i], found:Boolean = false;

                for (var f:int = typesColliding.length - 1; f >= 0; f--)
                {
                    if (item is typesColliding[f])
                    {
                        found = true;
                        break;
                    }
                }

                if (found && $target.hitTestObject(item.target) && this != item)
                {
                    return item;
                }
            }
            return null;
        }

        public function moveBy(typesColliding:Array, $x:Number = 0, $y:Number = 0, $target:DisplayObject = null):void
        {
            var $collision:GameObject;
            x += $x;
            if (($collision = collision(typesColliding, $target)) != null)
            {
                x -= $x;
                onCollideX($collision);
            }

            y += $y;
            if (($collision = collision(typesColliding, $target)) != null)
            {
                y -= $y;
                onCollideY($collision);
            }
        }

        public function onCollideX($collision:GameObject):void 
        {

        }

        public function onCollideY($collision:GameObject):void 
        {

        }

        public function getType($class:Class):Vector.<GameObject>
        {
            var $list:Vector.<GameObject> = new Vector.<GameObject>;

            for (var i:int = 0; i < list.length; i++)
            {
                if (list[i] is $class)
                {
                    $list.push(list[i]);
                }
            }

            return $list;
        }

        public function destroy():void
        {
            var indexOf:int = list.indexOf(this);
            if (indexOf > -1)
                list.splice(indexOf, 1);

            if (parent)
                parent.removeChild(this);

            trace("removing item: "+this+" list: " + list.length + ": " + list);
        }
    }

}

Any help would be appreciated, thank you.

1

There are 1 best solutions below

2
On

Changing radius of vision to the cone of vision simply requires computing (after checking the distance) the angle between the direction enemy is facing and the player. If this angle is smaller then some T, then it is in the cone of vision of angle 2T (due to symmetry).

This obviously ignores any walls/obstacles that should limit vision, but it should give you the general idea.