CrisisAtSwissStation.LaserObject.Update C# (CSharp) Method

Update() public method

public Update ( float mX, float mY, Vector2 offset, int levelWidth ) : void
mX float
mY float
offset Vector2
levelWidth int
return void
        public void Update(float mX, float mY, Vector2 offset, int levelWidth)
        {
            if (amDrawing || amErasing)
            {
                //animation object
                myGameTime++;
                sourceRect = new Rectangle(xFrame * spriteWidth, yFrame * spriteHeight, spriteWidth, spriteHeight);

                animateTimer += myGameTime;

                if (animateTimer > animateInterval)
                {
                    xFrame++;

                    if (xFrame > numFrames - 1)
                    {
                        xFrame = 0;
                    }
                    myGameTime = 0;
                    animateTimer = 0;

                }

            }

            //Console.WriteLine("{0} {1}", mX, mY);
            //mx and my are screen coords but have been divided by scale so they are in "gamecoords"
            //therefore mouseX and mouseY are in screencoords
            mouseX = mX * SCALE;
            mouseY = mY * SCALE;

            int guyScreenPos = 0;
            if (dude.Position.X * CASSWorld.SCALE <= GameEngine.SCREEN_WIDTH / 2)
                guyScreenPos = (int)(dude.getWorld().getScreenCoords(dude.Position).X);
            else if (dude.Position.X * CASSWorld.SCALE >= levelWidth - GameEngine.SCREEN_WIDTH / 2)
            {
                guyScreenPos = (int)(dude.getWorld().getScreenCoords(dude.Position).X); ;
            }
            else
            {
                guyScreenPos = GameEngine.SCREEN_WIDTH / 2;
            }

            original = new Vector2(dude.Position.X * SCALE, dude.Position.Y * SCALE);
            end = new Vector2((mX + dude.Position.X) * SCALE - guyScreenPos, mY * SCALE);
            //end = new Vector2(mX*SCALE, mY * SCALE);

            //Vector2 gunpos = original; //+ adjustment;

            Box2DX.Collision.Segment myseg = new Segment();
            //myseg.P1 = new Box2DX.Common.Vec2(original.X / SCALE, original.Y / SCALE);
            myseg.P1 = new Box2DX.Common.Vec2(dude.Position.X, dude.Position.Y);
            myseg.P2 = new Box2DX.Common.Vec2(end.X / SCALE, end.Y / SCALE);

            lambda = 1;
            Box2DX.Common.Vec2 normal;

            //Console.WriteLine("{0}", interference[0]);
            for (int i = 0; i < 2; i++)
            {
                interference[i] = null;
            }

            //interference = world.RaycastOne(myseg, out lambda, out normal, false, null);
            //Console.WriteLine("hi");
            int numShapes = world.Raycast(myseg, interference, 3, false, null);
            //Console.WriteLine( "{0}", interference[0]);
            if (interference[0] == null)
            {
                canIDraw = true;
                canIErase = true;

                endpoint = new Vector2(mouseX, mouseY);
            }
            else if (interference[0].IsSensor)
            {

                if (interference[1] == null)
                {
                    canIDraw = true;
                    canIErase = true;

                    endpoint = new Vector2(mouseX, mouseY);
                }
                else if (interference[1].IsSensor)
                {
                    if (interference[2] == null)
                    {
                        canIDraw = true;
                        canIErase = true;
                        endpoint = new Vector2(mouseX, mouseY);
                    }
                    else
                    {
                        AABB aabb = new AABB();
                        aabb.LowerBound = Common.Utils.Convert(dude.getWorld().getGameCoords(new Vector2(mouseX, mouseY)) - new Vector2(0.1f));
                        aabb.UpperBound = Common.Utils.Convert(dude.getWorld().getGameCoords(new Vector2(mouseX, mouseY)) + new Vector2(0.1f));

                        Shape[] shapes = new Shape[1];
                        int nHit = world.Query(aabb, shapes, 1);

                        if (nHit > 0)
                        {
                            Body body1 = shapes[0].GetBody();
                            PhysicsObject po1 = (PhysicsObject)body1.GetUserData();
                            Body body2 = interference[2].GetBody();
                            PhysicsObject po2 = (PhysicsObject)body2.GetUserData();
                            if ((po1 is PaintedObject) && body1.Equals(body2))
                            {
                                canIErase = true;
                                canIDraw = false;
                            }
                            else
                            {
                                canIErase = false;
                                canIDraw = false;
                            }
                        }
                        else
                        {
                            canIErase = false;
                            canIDraw = false;
                        }

                        myseg.P1 = new Box2DX.Common.Vec2(dude.Position.X, dude.Position.Y + .7f);
                        Box2DX.Collision.Shape intersect = world.RaycastOne(myseg, out lambda, out normal, false, null);
                        Box2DX.Common.Vec2 p = (((1 - lambda) * (myseg.P1)) + (lambda * (myseg.P2)));
                        endpoint = dude.getWorld().getScreenCoords(Common.Utils.Convert(p));
                    }

                }
                else
                {
                    AABB aabb = new AABB();
                    aabb.LowerBound = Common.Utils.Convert(dude.getWorld().getGameCoords(new Vector2(mouseX, mouseY)) - new Vector2(0.1f));
                    aabb.UpperBound = Common.Utils.Convert(dude.getWorld().getGameCoords(new Vector2(mouseX, mouseY)) + new Vector2(0.1f));

                    Shape[] shapes = new Shape[1];
                    int nHit = world.Query(aabb, shapes, 1);

                    if (nHit > 0)
                    {
                        Body body1 = shapes[0].GetBody();
                        PhysicsObject po1 = (PhysicsObject)body1.GetUserData();
                        Body body2 = interference[1].GetBody();
                        PhysicsObject po2 = (PhysicsObject)body2.GetUserData();
                        if ((po1 is PaintedObject) && body1.Equals(body2))
                        {
                            canIErase = true;
                            canIDraw = false;
                        }
                        else
                        {
                            canIErase = false;
                            canIDraw = false;
                        }
                    }
                    else
                    {
                        canIErase = false;
                        canIDraw = false;
                    }

                    myseg.P1 = new Box2DX.Common.Vec2(dude.Position.X, dude.Position.Y + .6f);
                    Box2DX.Collision.Shape intersect = world.RaycastOne(myseg, out lambda, out normal, false, null);
                    Box2DX.Common.Vec2 p = (((1 - lambda) * (myseg.P1)) + (lambda * (myseg.P2)));
                    endpoint = dude.getWorld().getScreenCoords(Common.Utils.Convert(p));
                }

            }
            else
            {
                AABB aabb = new AABB();
                aabb.LowerBound = Common.Utils.Convert(dude.getWorld().getGameCoords(new Vector2(mouseX, mouseY)) - new Vector2(0.1f));
                aabb.UpperBound = Common.Utils.Convert(dude.getWorld().getGameCoords(new Vector2(mouseX, mouseY)) + new Vector2(0.1f));

                Shape[] shapes = new Shape[1];
                int nHit = world.Query(aabb, shapes, 1);

                if (nHit > 0)
                {
                    Body body1 = shapes[0].GetBody();
                    PhysicsObject po1 = (PhysicsObject)body1.GetUserData();
                    Body body2 = interference[0].GetBody();
                    PhysicsObject po2 = (PhysicsObject)body2.GetUserData();

                    if ((po1 is PaintedObject) && body1.Equals(body2))
                    {
                        canIErase = true;
                        canIDraw = false;
                    }
                    else
                    {
                        canIErase = false;
                        canIDraw = false;
                    }
                }
                else
                {
                    canIDraw = false;
                    canIErase = false;
                }

                Box2DX.Collision.Shape intersect = world.RaycastOne(myseg, out lambda, out normal, false, null);
                Box2DX.Common.Vec2 p = (((1 - lambda) * (myseg.P1)) + (lambda * (myseg.P2)));
                endpoint = dude.getWorld().getScreenCoords(Common.Utils.Convert(p));

            }

            //original animation stuff
            //startpoint = original + adjustment + offset;
            startpoint = original + offset;
            //endpoint = new Vector2(mouseX, mouseY);

            // move the start point to the end of his gun
            Vector2 cursorDirection = (endpoint - startpoint);
            cursorDirection.Normalize();
            startpoint = startpoint + (Constants.HALF_GUN * cursorDirection);
        }