Ejemplo n.º 1
0
 public void SetSpeed(Vector newSpeed)
 {
     if (newSpeed != this.speed)
     {
         this.speed = newSpeed;
         this.SetNeedsUpdate();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// This entity is colliding into another. This entity is the one doing the colliding. 
        /// </summary>
        /// <param name="collidingEntity"></param>
        public void Collision(GameEntity collidingEntity)
        {
            Vector collisionVector = new Vector(this.position.x - collidingEntity.GetPosition().x + this.speed.X,
                this.position.x - collidingEntity.GetPosition().x + this.speed.Y);

            string firingGuy = "";

            if(this.GetTypeName() == "Projectile")
            {
                //the projectile should not hit the entity that fired it
                Projectile projEnt = this.As<Projectile>();
                if (collidingEntity == projEnt.GetFiringEntity())
                {
                    return;
                }

                firingGuy = projEnt.GetFiringEntity().GetName();
            }

            //perform custom collision events first
            if (this.collisionEvent != null)
            {
                this.collisionEvent.Invoke(collidingEntity);
            }
            if (collidingEntity.collisionEvent != null)
            {
                collidingEntity.collisionEvent.Invoke(collidingEntity);
            }

            // Determine where the collision happens
            Point collisionPosition = new Point(0, 0);
            if (Math.Abs(this.speed.X) > Math.Abs(collidingEntity.speed.X))
            {
                collisionPosition.x = (Math.Abs(collidingEntity.speed.X) / Math.Abs(this.speed.X))
                    * Math.Abs(this.position.x - collidingEntity.position.x);
            }
            else
            {
                collisionPosition.x = (Math.Abs(this.speed.X) / Math.Abs(collidingEntity.speed.X))
                    * Math.Abs(collidingEntity.position.x - this.position.x);
            }

            // If the colliding entity is neither above nor below, check X collision.
            if (!collidingEntity.GetArea().Above(this.GetArea()) && !collidingEntity.GetArea().Below(this.GetArea()))
            {
                if ((this.position.x + this.speed.X > collidingEntity.position.x + collidingEntity.speed.X &&
                    this.position.x + this.speed.X < collidingEntity.position.x + collidingEntity.speed.X + collidingEntity.Width) ||
                    (this.position.x + this.speed.X < collidingEntity.position.x + collidingEntity.speed.X &&
                    this.position.x + this.speed.X + this.Width > collidingEntity.position.x + collidingEntity.speed.X))
                {
                    //newSpeed.x = 0;
                    //colNewSpeed.x = 0;
                }
            }

            // If colliding entity is neither to the left nor the right, check Y collision
            if (!(collidingEntity.GetArea().Right < this.GetArea().Left)
                && !(collidingEntity.GetArea().Left > this.GetArea().Right))
            {
                // If colliding from above...
                // "Minimum" speed is entity position minus colliding entity top.
                if (this.position.y + this.speed.Y < collidingEntity.position.y + collidingEntity.speed.Y &&
                    this.position.y + this.speed.Y + this.Height > collidingEntity.position.y + collidingEntity.speed.Y)
                {
                    this.SetPosition(this.GetPosition().x, collidingEntity.GetArea().Top);
                }

                // If colliding from below,
                // "Maximum" speed is colliding entity top minus entity bottom
                if (this.position.y + this.speed.Y > collidingEntity.position.y + collidingEntity.speed.Y &&
                    this.position.y + this.speed.Y < collidingEntity.position.y + collidingEntity.speed.Y + collidingEntity.Height)
                {
                    this.SetPosition(this.GetPosition().x, collidingEntity.GetArea().Bottom - this.GetArea().height);
                }
            }

            // Collision means speed = 0;
            this.SetSpeed(new Vector(0, 0));
            collidingEntity.SetSpeed(new Vector(0, 0));

            // Colliding with a tile.
            if (collidingEntity.GetTypeName() == "Tile")
            {
                // Debug.log(collisionVector.ToString());
            }

            // Well, we don't need to deal damage from a terrain collider.
            if (this.GetTypeName() == "TerrainCollider")
            {
                return;
            }

            int damage = 10;

            //deal damage to the other entity
            if (collidingEntity.GetTypeName() == "LivingGameEntity")
            {
                LivingGameEntity livingTarget = collidingEntity.As<LivingGameEntity>();
                /*
                Debug.log(this.GetName() + " " + this.GetId() + " at " + this.GetPosition().x + ", " + this.GetPosition().y +
                    " dealing " + damage + " damage to " + livingTarget.GetName() + " (" + livingTarget.GetId() + ") at " + livingTarget.GetPosition().x + ", " + livingTarget.GetPosition().y +
                    ". IsProjectile: " + isProj.ToString() + ", firingGuy is " + firingGuy + ", This type is " + this.GetType().Name.ToString());
                */
                Debug.log("Damaging " + livingTarget.GetName() + " by " + damage);
                livingTarget.Damage(damage);
            }
            //collidingEntity.dam
        }
Ejemplo n.º 3
0
 public void SetMinSpeed(double xMagnitude, double yMagnitude)
 {
     this.minSpeed = new Vector(xMagnitude, yMagnitude);
 }
Ejemplo n.º 4
0
        public void CalculateSpeed()
        {
            //only do projectile temporarily...
            if(this.GetTypeName() == "GameEntitySpawner" || this.GetTypeName() == "Projectile")
            {
                return;
            }

            decelerate();

            //we probably want to make a vector that either uses or can handle doubles, rather than converting to int
            if (this.desiredDirection == MovementDirection.Left)
            {
                this.speed.X -= this.acceleration;
            }
            else if (this.desiredDirection == MovementDirection.Right)
            {
                this.speed.X += this.acceleration;
            }
            else if (this.desiredDirection == MovementDirection.Down)
            {
                this.speed.Y -= this.acceleration;
            }
            else if (this.desiredDirection == MovementDirection.Up)
            {
                this.speed.Y += this.acceleration;
            }
            // Instant braking
            else if (this.desiredDirection == MovementDirection.None)
            {
                // AI controllers decelerate differently for now.
                if (this.GetTypeName().Contains("LivingGameEntity") && ((LivingGameEntity)this).GetAI() == null)
                {
                    if (this.speed.X > 0)
                    {
                        this.speed.X -= this.acceleration;
                    }
                    else if (this.speed.X < 0)
                    {
                        this.speed.X += this.acceleration;
                    }
                    if (this.speed.Y > 0)
                    {
                        this.speed.Y -= this.acceleration;
                    }
                    else if (this.speed.Y < 0)
                    {
                        this.speed.Y += this.acceleration;
                    }
                }
                else
                {
                    this.speed = new Vector(0, 0);
                }
            }

            checkSpeedRates();
        }
Ejemplo n.º 5
0
 public void AddSpeed(Vector newSpeed)
 {
     this.speed.X += newSpeed.X;
     this.speed.Y += newSpeed.Y;
 }
Ejemplo n.º 6
0
 public void SetGravity(Vector newGravity)
 {
     this.stageGravity = newGravity;
 }
Ejemplo n.º 7
0
 public static double Distance(Vector vector1, Vector vector2)
 {
     return Math.Abs(vector2.Y - vector1.Y) + Math.Abs(vector2.X - vector1.X);
 }
        private void ThinkAboutWhereToGo()
        {
            // Implement tracking of expected location, and handling of displacement at top of function (here) if not at expected location.

            // If something has slated this entity to move differently, do that instead.
            if (this.alternateMovementProcess != null)
            {
                this.alternateMovementProcess.Invoke(this);
                return;
            }

            // Default to not moving
            body.SetDirection(MovementDirection.None);

            // If we have previously temporarily altered the speed, restore it
            if (oldSpeed != null)
            {
                this.body.SetSpeed(oldSpeed);
                oldSpeed = null;
            }

            // Pick a direction (if any) to move
            // If there is a current movement path, move toward the current node
            if (this.currentPath != null)
            {
                if (this.currentPath.GetNodeCount() == 0)
                {
                    Debug.log("A non null path has 0 nodes for entity " + this.GetBody().GetId() + " (" + this.GetBody().GetName() + ")");
                }

                //the current node that we are targeted at / walking toward
                Point curNode = this.currentPath.GetNode(this.currentNode);
                //Point curNode = this.currentPath.GetNextNode();

                //there are no more nodes, the entity has reached the end of the path
                if (curNode == null)
                {
                    //Script.Eval("console.log('The current node in the current AI path is null for some entity.')");
                    this.currentPath = null;
                    //stop the entity
                    this.body.SetSpeed(new Vector(0, 0));

                    // If we need to do something at the completion of the path, do it.
                    if (this.DestinationArrival != null)
                    {
                        this.DestinationArrival.Invoke(this);
                    }
                }
                else
                {
                    // The difference between the current horizontal position and the target horizontal position
                    double hOffset = body.GetPosition().x - curNode.x;
                    // The difference between the current vertical position and the target vertical position
                    double vOffset = body.GetPosition().y - curNode.y;

                    //I wonder if these should be rounded?
                    //hOffset = Helpah.Round(hOffset);
                    //vOffset = Helpah.Round(vOffset);

                    //if there's more horizontal distance than vertical, we want to go that way
                    if (Math.Abs(hOffset) > Math.Abs(vOffset))
                    {
                        //if we're to the right of the target, move left
                        if (hOffset > 0)
                        {
                            body.SetDirection(MovementDirection.Left);
                        }
                        else
                        {
                            body.SetDirection(MovementDirection.Right);
                        }
                    }
                    else
                    {
                        if (vOffset > 0)
                        {
                            body.SetDirection(MovementDirection.Down);
                        }
                        else
                        {
                            body.SetDirection(MovementDirection.Up);
                        }
                    }

                    //if the horizontal distance is less than or equal to the current speed divided by the acceleration, begin decelerating
                    if (hOffset != 0 && Math.Abs(hOffset) <= body.GetSpeed().X / body.GetAcceleration())
                    {
                        body.SetDirection(MovementDirection.None);

                        //if the body needs to travel a distance less than its normal acceleration
                        if (Math.Abs(hOffset) < body.GetAcceleration())
                        {
                            //this.oldSpeed = body.GetSpeed();
                            //this.oldSpeed.x = 0;
                            body.SetSpeed(new Vector(hOffset, body.GetSpeed().Y));
                        }
                    }

                    //if the vertical distance is less than or equal to the current speed divided by the acceleration, begin decelerating
                    if (vOffset != 0 && Math.Abs(vOffset) <= body.GetSpeed().Y / body.GetAcceleration())
                    {
                        //Debug.log("Setting speed manually (voffset).");
                        body.SetDirection(MovementDirection.None);

                        //if the body needs to travel a distance less than its normal acceleration
                        if (Math.Abs(vOffset) < body.GetAcceleration())
                        {
                            //this.oldSpeed = body.GetSpeed();
                            body.SetSpeed(new Vector(body.GetSpeed().X, vOffset));
                        }
                    }

                    //the entity is at the same position as the node, time to target the next one
                    //if (Helpah.Round(hOffset, 2) == 0 && Helpah.Round(vOffset, 2) == 0)
                    if(hOffset == 0 && vOffset == 0)
                    {
                        body.SetDirection(MovementDirection.None);
                        this.currentNode++;
                    }

                    Debug.Watch("Desired direction:", body.GetDirection().ToString());
                }
            }
        }