コード例 #1
0
ファイル: Player.cs プロジェクト: bradleat/trafps
        /// <param name="State">0 - standing, 1 - walking, 2 running, 3 - sprinting</param>
        /// <param name="MoveDirection">The Direction the player is moving as a Vector2, eg. a for a player running forwards, use Vector2.Forward</param>
        public void Update(Vector2 MoveDirection, int State, bool Jump)
        {
            if (Jump == false)
                wasJumping = false;

            switch (State)
            {
                case 0:
                    speed *= 0.8f;
                    break;
                case 1:
                    speed += (walkSpeed - speed) * 0.8f;
                    break;
                case 2:
                    speed += (runSpeed - speed) * 0.8f;
                    break;
                case 3:
                    speed += (sprintSpeed - speed) * 0.8f;
                    break;
            }

            oldPosition = position;

            velocity.X = MoveDirection.X * speed;
            velocity.Z = MoveDirection.Y * speed;
            velocity.Y += Gravity;

            position += Vector3.Transform(Velocity, Matrix.CreateRotationY(rotation));

            intersection = new Intersection();

                Primatives.Line Line = new Primatives.Line(position - boundingBoxSize.Y * Vector3.UnitY / 2, position + boundingBoxSize.Y * Vector3.UnitY / 2);

                Stack<Primatives.Triangle> triangles = world.OctMesh(Line.Vertices[0]);

                //for (int i = 0; i < world.Mesh.Triangles.Length; i++)
                foreach(Primatives.Triangle triangle in triangles)
                {
                    float intersectionPoint = intersection.LineTriangle(triangle, Line);
                    if (intersectionPoint != -1)
                    {
                        velocity.Y = 0f;
                        if (intersectionPoint < 0.3f)
                        {
                            position.Y += intersectionPoint * boundingBoxSize.Y;
                            if (Jump == true && wasJumping == false)
                            {
                                velocity.Y = jumpVelocity;
                                wasJumping = true;
                            }
                        }
                        else
                        {
                            position = oldPosition;
                        }
                    }
                }
        }