コード例 #1
0
ファイル: GroundSimple.cs プロジェクト: Entrivax/42run
        public GroundSimple(Direction direction)
        {
            Mesh      = MeshToUse;
            Direction = direction;
            Length    = 6f;
            var rotation = DirectionHelper.GetRotationFromDirection(direction);
            var p1       = new Vector4(-3f, -0.5f, -Length, 1) * rotation;
            var p2       = new Vector4(3f, 0f, 0f, 1) * rotation;

            PossibleObstaclePositions = new Vector3[2];
            for (int i = 0; i < 2; i++)
            {
                PossibleObstaclePositions[i] = (new Vector4(0, 0, -i * 3, 1) * rotation).Xyz;
            }
            BoundingBox = new AxisAlignedBB(Vector3.ComponentMin(p1.Xyz, p2.Xyz), Vector3.ComponentMax(p1.Xyz, p2.Xyz));
        }
コード例 #2
0
ファイル: GroundFactory.cs プロジェクト: Entrivax/42run
        public static Ground[] NewGround(Vector3 position, Direction direction, out Vector3 next)
        {
            var groundNum = _random.Next(0, 100);

            Ground[] grounds;
            if (groundNum < 80)
            {
                var ground = new GroundSimple(direction)
                {
                    Position = position
                };
                grounds = new[] { ground };
                next    = position + DirectionHelper.GetVectorFromDirection(direction) * ground.Length;
            }
            else if (groundNum < 90)
            {
                var ground = new GroundCluster(direction)
                {
                    Position = position
                };
                grounds = new[] { ground };
                next    = position + DirectionHelper.GetVectorFromDirection(direction) * ground.Length;
            }
            else
            {
                var groundCount = _random.Next(5, 10);
                grounds = new Ground[groundCount];
                next    = position;
                for (int i = 0; i < groundCount; i++)
                {
                    var ground = new GroundStairs(direction)
                    {
                        Position = next
                    };
                    grounds[i] = ground;
                    next       = next + DirectionHelper.GetVectorFromDirection(direction) * ground.Length + new Vector3(0, 0.3f, 0);
                }
            }
            return(grounds);
        }
コード例 #3
0
 public Vector3 GetPosition()
 {
     return(_rotationRight * DirectionHelper.GetVectorFromDirection(CurrentDirection) * _sidewayMove + Position);
 }
コード例 #4
0
        public void Update(double time)
        {
            if (Dead)
            {
                return;
            }
            Score += _scoreIncrementation * (float)time;
            Speed += (float)time * _speedIncrease;
            if (Speed > _maxSpeed)
            {
                Speed = _maxSpeed;
            }
            _animTime += ((float)time * Speed) / 12.5f;
            if (KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.Right))
            {
                _sidewayMove -= _sidewaySpeed;
                if (_sidewayMove < -_maxSidewayDistance)
                {
                    _sidewayMove = -_maxSidewayDistance;
                }
            }
            else if (KeyboardHelper.GetKeyboardState().IsKeyDown(OpenTK.Input.Key.Left))
            {
                _sidewayMove += _sidewaySpeed;
                if (_sidewayMove > _maxSidewayDistance)
                {
                    _sidewayMove = _maxSidewayDistance;
                }
            }
            else
            {
                if (_sidewayMove > 0.01)
                {
                    _sidewayMove -= _sidewaySpeed;
                }
                else if (_sidewayMove < -0.01)
                {
                    _sidewayMove += _sidewaySpeed;
                }
                else
                {
                    _sidewayMove = 0;
                }
            }

            if (KeyboardHelper.IsKeyPressed(OpenTK.Input.Key.Up) && _onGround)
            {
                _onGround  = false;
                _yVelocity = _jumpPower;
            }

            var intersection = GetIntersection();

            if (intersection != null)
            {
                if (KeyboardHelper.IsKeyPressed(OpenTK.Input.Key.Left))
                {
                    Position = new Vector3(intersection.Position.X, Position.Y, intersection.Position.Z) + DirectionHelper.GetVectorFromDirection(intersection.Direction) * 3f;
                    var dir = (int)CurrentDirection - 1;
                    if (dir < 0)
                    {
                        dir = (int)Direction.WEST;
                    }
                    CurrentDirection  = (Direction)dir;
                    intersection.Used = true;
                }
                else if (KeyboardHelper.IsKeyPressed(OpenTK.Input.Key.Right))
                {
                    Position = new Vector3(intersection.Position.X, Position.Y, intersection.Position.Z) + DirectionHelper.GetVectorFromDirection(intersection.Direction) * 3f;
                    var dir = (int)CurrentDirection + 1;
                    if (dir > 3)
                    {
                        dir = (int)Direction.NORTH;
                    }
                    CurrentDirection  = (Direction)dir;
                    intersection.Used = true;
                }
            }

            _yVelocity -= _gravity;

            Position += DirectionHelper.GetVectorFromDirection(CurrentDirection) * Speed * (float)time + (Vector3.UnitY * _yVelocity);

            var pos = GetPosition();
            var intersectionWithGrounds = World.Grounds.Where(g => g.BoundingBox.IntersectWith(g.Position, BoundingBox, pos)).ToList();

            if (intersectionWithGrounds.Count != 0)
            {
                _yVelocity = 0;
            }
            float?maxY = null;

            for (int i = 0; i < intersectionWithGrounds.Count; i++)
            {
                var intersectionY = intersectionWithGrounds[i].BoundingBox.Max.Y + intersectionWithGrounds[i].Position.Y;
                if (maxY == null || maxY < intersectionY)
                {
                    maxY = intersectionY;
                }
            }

            if (intersectionWithGrounds.Count == 0)
            {
                _onGround = false;
            }

            if (maxY != null)
            {
                if (maxY > pos.Y + _maxStepSize)
                {
                    Dead = true;
                }
                else if (maxY > pos.Y)
                {
                    Position = new Vector3(Position.X, (float)maxY, Position.Z);
                }
                _onGround = true;
            }

            if (_onGround)
            {
                _lastY = Position.Y;
            }
            else if (Position.Y < _lastY - 5f)
            {
                Dead = true;
            }

            if (CollideWithObstacle())
            {
                Dead = true;
            }

            var coin = GetCoin();

            if (coin != null)
            {
                World.Coins.Remove(coin);
                Score += 20;
            }
        }
コード例 #5
0
        private void GenerateTerrain()
        {
            TerrainRemover leftTerrainRemover = null;
            var            coinsOffset        = new Vector3(0, 0.3f, 0);

            if ((_intersection.Directions & (int)Intersection.IntersectionDirection.LEFT) > 0)
            {
                var dir = (int)_intersection.Direction - 1;
                if (dir < 0)
                {
                    dir = (int)Direction.WEST;
                }
                var            direction         = (Direction)dir;
                int            toGenerate        = _rand.Next(10) + 5;
                List <Ground>  generatedGrounds  = new List <Ground>();
                var            dirVector         = DirectionHelper.GetVectorFromDirection(direction);
                var            interDir          = DirectionHelper.GetVectorFromDirection(_intersection.Direction);
                var            nextPosition      = _intersection.Position + dirVector * 5f + interDir * 3f;
                List <Vector3> obstaclePositions = new List <Vector3>();
                for (int i = 0; i < toGenerate; i++)
                {
                    var grounds = GroundFactory.NewGround(nextPosition, direction, out nextPosition);
                    Array.ForEach(grounds, ground => Array.ForEach(ground.PossibleObstaclePositions, possiblePos => obstaclePositions.Add(possiblePos + ground.Position)));
                    generatedGrounds.AddRange(grounds);
                }
                var intersection = GroundFactory.NewIntersection(_player, _player.World, nextPosition, direction, _rand.Next(1, 4));
                generatedGrounds.Add(intersection);
                _player.World.Grounds.AddRange(generatedGrounds);

                var rotation       = DirectionHelper.GetRotationFromDirection(direction);
                var wallsColliders = new List <Obstacle>();
                var coins          = new List <Coin>();

                obstaclePositions.ForEach(pos => {
                    if ((pos - _intersection.Position).LengthFast > 20)
                    {
                        var randValue = _rand.Next(4);
                        if (randValue == 0)
                        {
                            wallsColliders.Add(ObstacleFactory.NewObstacle(pos + interDir * _rand.Next(-1, 1) * 2, direction));
                        }
                        else if (randValue == 1)
                        {
                            coins.Add(new Coin(pos + interDir * _rand.Next(-1, 1) * 2 + coinsOffset));
                        }
                    }
                });
                _player.World.Coins.AddRange(coins);

                var w1p1 = new Vector3(new Vector4(-3f, 0f, -7f, 1) * rotation);
                var w1p2 = new Vector3(new Vector4(3f, 5f, -6f, 1) * rotation);
                wallsColliders.Add(new Obstacle(new AxisAlignedBB(Vector3.ComponentMin(w1p1, w1p2), Vector3.ComponentMax(w1p1, w1p2)), intersection.Position, Direction.NORTH));
                if (intersection.Directions == (int)Intersection.IntersectionDirection.LEFT)
                {
                    var w2p1 = new Vector3(new Vector4(3f, 0f, -6f, 1) * rotation);
                    var w2p2 = new Vector3(new Vector4(4f, 5f, 0f, 1) * rotation);
                    wallsColliders.Add(new Obstacle(new AxisAlignedBB(Vector3.ComponentMin(w2p1, w2p2), Vector3.ComponentMax(w2p1, w2p2)), intersection.Position, Direction.NORTH));
                }
                if (intersection.Directions == (int)Intersection.IntersectionDirection.RIGHT)
                {
                    var w2p1 = new Vector3(new Vector4(-4f, 0f, -6f, 1) * rotation);
                    var w2p2 = new Vector3(new Vector4(-3f, 5f, 0f, 1) * rotation);
                    wallsColliders.Add(new Obstacle(new AxisAlignedBB(Vector3.ComponentMin(w2p1, w2p2), Vector3.ComponentMax(w2p1, w2p2)), intersection.Position, Direction.NORTH));
                }
                _player.World.Obstacles.AddRange(wallsColliders);
                var ap1 = new Vector3(new Vector4(-3f, 0f, -6f, 1) * rotation);
                var ap2 = new Vector3(new Vector4(3f, 5f, 0f, 1) * rotation);
                leftTerrainRemover = new TerrainRemover(_player, generatedGrounds, new List <Trigger>(_player.World.TriggersToAdd.ToArray()), wallsColliders, coins)
                {
                    Position = _intersection.Position + dirVector * 12f + DirectionHelper.GetVectorFromDirection(_intersection.Direction) * 3f, BoundingBox = new AxisAlignedBB(Vector3.ComponentMin(ap1, ap2), Vector3.ComponentMax(ap1, ap2))
                };
                _player.World.TriggersToAdd.Add(leftTerrainRemover);
            }
            if ((_intersection.Directions & (int)Intersection.IntersectionDirection.RIGHT) > 0)
            {
                var dir = (int)_intersection.Direction + 1;
                if (dir > 3)
                {
                    dir = (int)Direction.NORTH;
                }
                var            direction         = (Direction)dir;
                int            toGenerate        = _rand.Next(10) + 5;
                List <Ground>  generatedGrounds  = new List <Ground>();
                var            dirVector         = DirectionHelper.GetVectorFromDirection(direction);
                var            interDir          = DirectionHelper.GetVectorFromDirection(_intersection.Direction);
                var            nextPosition      = _intersection.Position + dirVector * 5f + interDir * 3f;
                List <Vector3> obstaclePositions = new List <Vector3>();
                for (int i = 0; i < toGenerate; i++)
                {
                    var grounds = GroundFactory.NewGround(nextPosition, direction, out nextPosition);
                    Array.ForEach(grounds, ground => Array.ForEach(ground.PossibleObstaclePositions, possiblePos => obstaclePositions.Add(possiblePos + ground.Position)));
                    generatedGrounds.AddRange(grounds);
                }
                var intersection = GroundFactory.NewIntersection(_player, _player.World, nextPosition, direction, _rand.Next(1, 4));
                generatedGrounds.Add(intersection);
                _player.World.Grounds.AddRange(generatedGrounds);

                var triggersToKeep = new List <Trigger>(_player.World.TriggersToAdd.ToArray());

                if (leftTerrainRemover != null && triggersToKeep.Contains(leftTerrainRemover))
                {
                    triggersToKeep.Remove(leftTerrainRemover);
                }

                var rotation       = DirectionHelper.GetRotationFromDirection(direction);
                var wallsColliders = new List <Obstacle>();
                var coins          = new List <Coin>();

                obstaclePositions.ForEach(pos => {
                    if ((pos - _intersection.Position).LengthFast > 20)
                    {
                        var randValue = _rand.Next(4);
                        if (randValue == 0)
                        {
                            wallsColliders.Add(ObstacleFactory.NewObstacle(pos + interDir * _rand.Next(-1, 1) * 2, direction));
                        }
                        else if (randValue == 1)
                        {
                            coins.Add(new Coin(pos + interDir * _rand.Next(-1, 1) * 2 + coinsOffset));
                        }
                    }
                });
                _player.World.Coins.AddRange(coins);

                var w1p1 = new Vector3(new Vector4(-3f, 0f, -7f, 1) * rotation);
                var w1p2 = new Vector3(new Vector4(3f, 5f, -6f, 1) * rotation);
                wallsColliders.Add(new Obstacle(new AxisAlignedBB(Vector3.ComponentMin(w1p1, w1p2), Vector3.ComponentMax(w1p1, w1p2)), intersection.Position, Direction.NORTH));
                if (intersection.Directions == (int)Intersection.IntersectionDirection.LEFT)
                {
                    var w2p1 = new Vector3(new Vector4(3f, 0f, -6f, 1) * rotation);
                    var w2p2 = new Vector3(new Vector4(4f, 5f, 0f, 1) * rotation);
                    wallsColliders.Add(new Obstacle(new AxisAlignedBB(Vector3.ComponentMin(w2p1, w2p2), Vector3.ComponentMax(w2p1, w2p2)), intersection.Position, Direction.NORTH));
                }
                if (intersection.Directions == (int)Intersection.IntersectionDirection.RIGHT)
                {
                    var w2p1 = new Vector3(new Vector4(-4f, 0f, -6f, 1) * rotation);
                    var w2p2 = new Vector3(new Vector4(-3f, 5f, 0f, 1) * rotation);
                    wallsColliders.Add(new Obstacle(new AxisAlignedBB(Vector3.ComponentMin(w2p1, w2p2), Vector3.ComponentMax(w2p1, w2p2)), intersection.Position, Direction.NORTH));
                }
                _player.World.Obstacles.AddRange(wallsColliders);
                var ap1            = new Vector3(new Vector4(-3f, 0f, -6f, 1) * rotation);
                var ap2            = new Vector3(new Vector4(3f, 5f, 0f, 1) * rotation);
                var terrainRemover = new TerrainRemover(_player, generatedGrounds, triggersToKeep, wallsColliders, coins)
                {
                    Position = _intersection.Position + dirVector * 12f + DirectionHelper.GetVectorFromDirection(_intersection.Direction) * 3f, BoundingBox = new AxisAlignedBB(Vector3.ComponentMin(ap1, ap2), Vector3.ComponentMax(ap1, ap2))
                };
                _player.World.TriggersToAdd.Add(terrainRemover);
            }
        }