State Machine Cant get to work

64 views Asked by At

Learning to code so this might sound noobish...

Using Action Script I am trying to make my Agents react to my segments so they run away. So I need to connect part of my Main code to my Agent code Here is my code :

Main.as

package 
{

    import agent.Agent;
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    import flash.geom.Point;
    import flash.display.Stage;

    public class Main extends Sprite 
    {
        private var score:Score;
        private var count:int;

        private var bleh: int = 0;
        private var Agents:Vector.<Agent>;
        private var segments:Array;
        private var numSegments:uint = 20;
        private var player:Point = new Point (200, 200)
        private var friend:Agent = new Agent;
        private var snakeHead:SnakeHead;
        private var snakeTail:SnakeTail;
        private var background: Sprite;

        private var border: Bushes;

        private var xVel: Number;
        private var yVel: Number;


        public function Main():void 
        {
            xVel = 0;
            yVel = 0;
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            snakeHead = new SnakeHead();
            snakeTail = new SnakeTail();
            score = new Score();
            border = new Bushes;
            score.x = 11; 
            score.y = 14;
            addChild(score);
            stage.addChild(border);
            count = 0;
            addChildAt(snakeHead,0);
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            segments = new Array();
            for(var i:uint = 0; i < numSegments; i++)
            {
                var segment:Segment = new Segment (10, 20);
                addChildAt(segment,0);
                addChildAt(snakeTail,0);
                segments.push(segment);
            }
            //updatePoint();
            addEventListener(Event.ENTER_FRAME, onEnterFrame);

            removeEventListener(Event.ADDED_TO_STAGE, init);
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            // entry point
            background = new LevelOne(); //creates the background
            addChildAt(background,0);

            Agents = new Vector.<Agent>();
            addEventListener(Event.ENTER_FRAME, gameloop);
            for (var x:int = 0; x < 3; x++) 
            {
                var a:Agent = new Agent();
                addChild(a);
                Agents.push(a);
                a.x = 400;
                a.y = 300;
                a.name = "Name"+x;
                trace (x);
            }
            stage.addEventListener(MouseEvent.CLICK, createAgent);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
        }

        private function createAgent(e:MouseEvent):void 
        {
            var a:Agent = new Agent();

            addChild(a);
            Agents.push(a);
            a.x = mouseX;
            a.y = mouseY;
        }

        private function gameloop(evt:Event):void 
        {
            for (var i:int = 0; i < Agents.length; i++) 
            {
                Agents[i].update();
                if (snakeHead.hitTestObject(Agents[i]))
                {
                    //trace (Agents[i].name);
                    removeAgent(Agents[i]);
                    Agents.splice(i,1);
                    count ++;
                    score.lowercasetext.text = count.toString();
                    trace("count: " + count);
                }

            }               
        }
        private function onEnterFrame(evt:Event):void
        {

            player.x += xVel;
            player.y += yVel;

            drag(snakeHead, player.x, player.y);

            drag(segments[0], snakeHead.x, snakeHead.y);

            for(var i:uint = 1; i < numSegments; i++)

            {
                var segmentA:Segment = segments[i];
                var segmentB:Segment = segments[i - 1];
                drag(segmentA, segmentB.x, segmentB.y);

                var PlayerHalfWidth: Number = segments[i].width / 2;
                var PlayerHalfHeight: Number = segments[i].height / 2;

                if (segments[i].x + PlayerHalfWidth > stage.stageWidth) {

                    segments[i].x = stage.stageWidth - PlayerHalfWidth;
                } else if (segments[i].x - PlayerHalfWidth < 0) {
                    segments[i].x = 0 + PlayerHalfWidth;
                }

                if (segments[i].y + PlayerHalfHeight > stage.stageHeight) {

                    segments[i].y = stage.stageHeight - PlayerHalfHeight;
                }else if (segments[i].y - PlayerHalfHeight < 0) {
                    segments[i].y = 0 + PlayerHalfHeight;
                }

                var playerHalfWidth: Number = segments[i - 1].width / 2;
                var playerHalfHeight: Number = segments[i - 1].height / 2;

                if (segments[i - 1].x + playerHalfWidth > stage.stageWidth) {

                    segments[i - 1].x = stage.stageWidth - playerHalfWidth;
                } else if (segments[i - 1].x - playerHalfWidth < 0) {
                    segments[i - 1].x = 0 + playerHalfWidth;
                }

                if (segments[i - 1].y + playerHalfHeight > stage.stageHeight) {

                    segments[i - 1].y = stage.stageHeight - playerHalfHeight;
                }else if (segments[i - 1].y - playerHalfHeight < 0) {
                    segments[i - 1].y = 0 + playerHalfHeight;
                }
            }
            drag(snakeTail, segments[19].x, segments[19].y);

            var HeadHalfWidth: Number = snakeHead.width / 2;
            var HeadHalfHeight: Number = snakeHead.height / 2;

            if (snakeHead.x + HeadHalfWidth > stage.stageWidth) {

            snakeHead.x = stage.stageWidth - HeadHalfWidth;
            } else if (snakeHead.x - HeadHalfWidth < 0) {
                snakeHead.x = 0 + HeadHalfWidth;
            }

            if (snakeHead.y + HeadHalfHeight > stage.stageHeight) {

                snakeHead.y = stage.stageHeight - HeadHalfHeight;
            }else if (snakeHead.y - HeadHalfHeight < 0) {
                snakeHead.y = 0 + HeadHalfHeight;
            }



            //drag(segments[19], snakeTail.x, snakeTail.y);

            /*for each (var a: Agent in Agents) {
                a.update();
                trace ("Follow me on Twitter.");
                if(segments[0].hitTestObject(a))
                {
                    trace("True");
                    trace(bleh + " " + "F*CKING HELL!");
                    trace(a.name);
                    stage.removeChild(Agents[1]); 
                }
                }*/

        }
        private function removeAgent(a:Agent):void 
        {
            a.parent.removeChild(a);
                    trace("Collision Detected!");
        }

        private function keyDown (evt: KeyboardEvent): void {
            //87=w 68=d 83=s 65=a
            if (evt.keyCode == 87) 
            {
                player;
                yVel = -5;
            } 
            if (evt.keyCode == 83) 
            {
                player;
                yVel = 5;
            } 
            if (evt.keyCode == 68) 
            {
                player;
                xVel = 5;

            } 
            if (evt.keyCode == 65) 
            {
                player;
                xVel = -5;
            }
            //trace (player.x + " " + player.y);
            trace (xVel + "   " + yVel);
        }
        private function keyUp (evt: KeyboardEvent): void {
            //87=w 68=d 83=s 65=a
            if (evt.keyCode == 87) 
            {
                player;
                yVel = 0;
            } 
            else if (evt.keyCode == 83) 
            {
                player;
                yVel = 0;
            } 
            else if (evt.keyCode == 68) 
            {
                player;
                xVel = 0;

            } 
            else if (evt.keyCode == 65) 
            {
                player;
                xVel = 0;
            }
        }
        private function drag(segment:MovieClip, xpos:Number, ypos:Number):void
        {

            var dx:Number = xpos - segment.x;
            var dy:Number = ypos - segment.y;
            var angle:Number = Math.atan2(dy, dx);
            segment.rotation = angle * 180 / Math.PI;

            var w:Number = segment.getPin().x - segment.x;
            var h:Number = segment.getPin().y - segment.y;
            segment.x = xpos - w;
            segment.y = ypos - h;
        }


    }
}

Agent.as

package agent 
{
    import agent.states.ChaseState;
    import agent.states.ConfusionState;
    import agent.states.FleeState;
    import agent.states.IAgentState;
    import agent.states.IdleState;
    import agent.states.WanderState;
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.geom.Point;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.display.MovieClip;
    import flash.events.*;
    import Segment;
    import Main;
    public class Agent extends MovieClip
    {
        private var mouses:mouse;
        public static const IDLE:IAgentState = new IdleState(); //Define possible states as static constants
        public static const WANDER:IAgentState = new WanderState();
        public static const CHASE:IAgentState = new ChaseState();
        public static const FLEE:IAgentState = new FleeState();
        public static const CONFUSED:IAgentState = new ConfusionState();

        public var snake:Segment;
        private const RAD_DEG:Number = 180 / Math.PI;

        private var _previousState:IAgentState; //The previous executing state
        private var _currentState:IAgentState; //The currently executing state
        private var _pointer:Shape;
        private var _tf:TextField;
        public var velocity:Point = new Point();
        public var speed:Number = 0;

        public var fleeRadius:Number = 100; //If the mouse is "seen" within this radius, we want to flee
        public var chaseRadius:Number = 50; //If the mouse is "seen" within this radius, we want to chase
        public var numCycles:int = 0; //Number of updates that have executed for the current state. Timing utility.

        public function Agent() 
        {

            //Boring stuff here
            _tf = new TextField();
            _tf.defaultTextFormat = new TextFormat("_sans", 10);
            _tf.autoSize = TextFieldAutoSize.LEFT;
            _pointer = new Shape();
            mouses = new mouse();
            snake = new Segment(1, 1);
            /*g.beginFill(0);
            g.drawCircle(0, 0, 5);
            g.endFill();
            g.moveTo(0, -5);
            g.beginFill(0);
            g.lineTo(10, 0);
            g.lineTo(0, 5);
            g.endFill();*/
            addChild(mouses);
            addChild(_tf);

            _currentState = IDLE; //Set the initial state


        }
        /**
         * Outputs a line of text above the agent's head
         * @param   str
         */
        public function say(str:String):void {
            _tf.text = str;
            _tf.y = -_tf.height - 2;
        }

        /**
         * Trig utility methods
         */
        public function get canSeeMouse():Boolean {
            var dot:Number = snake.x * velocity.x + snake.y * velocity.y;
            return dot > 0;

        }
        public function get distanceToMouse():Number {
            var dx:Number = x - snake.x;
            var dy:Number = y - snake.y;
            return Math.sqrt(dx * dx + dy * dy);
        }

        public function randomDirection():void {
            var a:Number = Math.random() * 6.28;
            velocity.x = Math.cos(a);
            velocity.y = Math.sin(a);
        }
        public function faceMouse(multiplier:Number = 1):void {
            var dx:Number = snake.x - x;
            var dy:Number = snake.y - y;
            var rad:Number = Math.atan2(dy, dx);
            velocity.x = multiplier*Math.cos(rad);
            velocity.y = multiplier*Math.sin(rad);
        }

        /**
         * Update the current state, then update the graphics
         */
        public function update():void {
            if (!_currentState) return; //If there's no behavior, we do nothing
            numCycles++; 
            _currentState.update(this);
            x += velocity.x*speed;
            y += velocity.y*speed;

            if (x + velocity.x > stage.stageWidth || x + velocity.x < 0) {
                x = Math.max(0, Math.min(stage.stageWidth, x));
                velocity.x *= -1;
            }
            if (y + velocity.y > stage.stageHeight || y + velocity.y < 0) {
                y = Math.max(0, Math.min(stage.stageHeight, y));
                velocity.y *= -1;
            }
            mouses.rotation = RAD_DEG * Math.atan2(velocity.y, velocity.x);
        }
        public function setState(newState:IAgentState):void {
            if (_currentState == newState) return;
            if (_currentState) {
                _currentState.exit(this);
            }
            _previousState = _currentState;
            _currentState = newState;
            _currentState.enter(this);
            numCycles = 0;
        }

        public function get previousState():IAgentState { return _previousState; }

        public function get currentState():IAgentState { return _currentState; }

    }

}
0

There are 0 answers