예제 #1
0
            internal void Move(Vector2 velocity)
            {
                if (velocity != Vector2.Zero)
                {
                    #region Move loop

                    Vector2 move       = velocity;
                    float   newMoveDir = 0;//in which direction the last collision was directed

                    float t = 0;
                    while (true)
                    {
                        if (move.Length() < 0.001f)
                        {
                            break;
                        }

                        M_Rectangle rect = new M_Rectangle(pos, Vector2.Zero);
                        rect.Enlarge(RADIUS + 1);
                        rect.Expand(move);

                        CollisionResult cr = new CollisionResult();
                        env.EachWall(rect, (x, y) =>
                        {
                            CollisionResult cr2 = mask.DistToPolygon(new M_Rectangle(x, y, 1, 1).ToPolygon(), move);
                            cr.AddCollisionResult(cr2, move);
                        });

                        if (!cr.distance.HasValue || t + cr.distance.Value >= 1)
                        {
                            if (t > 1)
                            {
                                t = 1;
                            }
                            pos += move * (1 - t);
                            break;
                        }

                        #region step move

                        //move to nearest dist
                        pos += move * cr.distance.Value;
                        t   += cr.distance.Value;

                        Vector2 lastMoveN = new Vector2(-move.Y, move.X);

                        Vector2 tangent = new Vector2(-cr.axisCol.Y, cr.axisCol.X);

                        if (COLLISIONSPEEDWASTE)
                        {
                            move = Vector2.Dot(velocity, tangent) * tangent;
                        }
                        else
                        {
                            float moveLength = move.Length();
                            move = Vector2.Dot(velocity, tangent) * tangent;
                            if (move != Vector2.Zero)
                            {
                                move = Vector2.Normalize(move) * moveLength;
                            }
                        }
                        if (newMoveDir == 0)
                        {
                            newMoveDir = Vector2.Dot(lastMoveN, move);
                        }
                        else if (Math.Sign(newMoveDir) != Math.Sign(Vector2.Dot(lastMoveN, move)))
                        {
                            //sackgasse
                            move = Vector2.Zero;
                        }

                        if (move.Length() < SPEED * 0.1f)
                        {
                            move = Vector2.Zero;
                        }

                        #endregion
                    }

                    #endregion
                }
            }
예제 #2
0
        private void Move()
        {
            if (velocity != Vector2.Zero)
            {
                #region Move loop

                float t = 0;
                while (true)
                {
                    if (velocity.Length() < 0.001f)
                    {
                        break;
                    }

                    M_Rectangle rect = new M_Rectangle(pos, Vector2.Zero);
                    rect.Enlarge(RADIUS + 1);
                    rect.Expand(velocity);

                    EnvShooter.Bot botHit = null;

                    CollisionResult cr = new CollisionResult();
                    env.EachWall(rect, (x, y) =>
                    {
                        CollisionResult cr2 = mask.DistToPolygon(new M_Rectangle(x, y, 1, 1).ToPolygon(), velocity);
                        cr.AddCollisionResult(cr2, velocity);
                    });
                    for (int i = 0; i < env.bots.Length; i++)
                    {
                        if (env.bots[i].Alive)
                        {
                            CollisionResult cr2 = mask.DistToCircle(env.bots[i].mask, velocity);
                            if (cr.AddCollisionResult(cr2, velocity))
                            {
                                botHit = env.bots[i];
                            }
                        }
                    }

                    if (!cr.distance.HasValue || t + cr.distance.Value >= 1)
                    {
                        if (t > 1)
                        {
                            t = 1;
                        }
                        pos += velocity * (1 - t);
                        break;
                    }

                    #region step velocity

                    //velocity to nearest dist
                    pos += velocity * cr.distance.Value;
                    t   += cr.distance.Value;

                    Vector2 lastMoveN = new Vector2(-velocity.Y, velocity.X);

                    Vector2 tangent = new Vector2(-cr.axisCol.Y, cr.axisCol.X);


                    velocity = Vector2.Dot(velocity, tangent) * tangent - Vector2.Dot(velocity, cr.axisCol) * cr.axisCol;

                    if (botHit != null)
                    {
                        if (id != -1)
                        {
                            float damage = velocity.Length() * DAMAGE;
                            botHit.health -= damage;
                            if (botHit.Id != id)
                            {
                                env.bots[id].damageDealt += damage;
                            }
                            else
                            {
                                env.bots[id].damageDealt -= damage;
                            }
                            if (botHit.health < 0)
                            {
                                botHit.health = 0;
                                if (botHit.Id != id)
                                {
                                    env.bots[id].kills++;
                                }
                                else
                                {
                                    env.bots[id].kills--;
                                }
                            }
                        }
                    }

                    #endregion
                }

                #endregion
            }
        }