Example #1
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
        }
Example #2
0
 public bool CheckEntityVisibility(GameEntity gent)
 {
     if (this.GetVisibleArea().Contains(gent.GetArea()))
     {
         if (!visibleEntities.Contains(gent))
         {
             visibleEntities.Add(gent);
         }
         return true;
     }
     else
     {
         if (visibleEntities.Contains(gent))
         {
             visibleEntities.Remove(gent);
         }
         return false;
     }
 }