Пример #1
0
    public void nextObstacles(PathPrefab prefab, PathPrefab prev, Difficulty level)
    {
        switch (level)
        {
        case Difficulty.EASY: {
            int type    = new int[] { 0, 0, 0, 0, 1, 1, 1, 2 }[rng.Next(8)];
            int pattern = rng.Next(setpieces[type].Length);
            RenderSetPiece(new List <Tuple <ObjectType, int> >(setpieces[type][pattern]), prefab);
            break;
        }

        case Difficulty.MED: {
            int type    = new int[] { 0, 0, 0, 1, 1, 1, 2, 3 }[rng.Next(7)];
            int pattern = rng.Next(setpieces[type].Length);
            RenderSetPiece(new List <Tuple <ObjectType, int> >(setpieces[type][pattern]), prefab);
            break;
        }

        case Difficulty.HARD: {
            int type    = new int[] { 0, 0, 1, 1, 2, 2, 3, 3 }[rng.Next(7)];
            int pattern = rng.Next(setpieces[type].Length);
            RenderSetPiece(new List <Tuple <ObjectType, int> >(setpieces[type][pattern]), prefab);
            break;
        }
        }
    }
Пример #2
0
    void RenderSetPiece(List <int> coinPos, PathPrefab prefab)
    {
        // handle empty set piece
        if (coinPos.Count == 0)
        {
            // set any last coint block and return
            prefab.lastCoin = rng.Next(3);
            return;
        }

        GameObject path = prefab.gameObject;

        foreach (int val in coinPos)
        {
            // coin cannot be placed on obstacle
            // TODO: perform other checks
            if (prefab.pathObjects[val] != ObjectType.NONE)
            {
                continue;
            }

            Vector3    pos  = buildPoint + new Vector3(val / 10 * offsetx, offsety, val % 10 * offsetz);
            GameObject coin = Instantiate(coinPrefab) as GameObject;
            coin.GetComponent <Animator>().SetFloat("Offset", Random.Range(0f, 1f));
            coin.transform.parent        = path.transform;
            coin.transform.localPosition = pos;
            coin.transform.rotation      = Quaternion.identity;
        }

        prefab.lastCoin = coinPos[coinPos.Count - 1];
    }
Пример #3
0
    public (PathPrefab, PathPrefab) AddPathBlock()
    {
        nextDirection = CheckAndChoose()[mazePathIndex++];

        // calculate next path values if this path is about to finish
        CheckPathBuffer();

        buildPoint.position = buildPoint.position + DirectedOffset(lastDirection, nextDirection);
        buildPoint.rotation = DirectedRotation(nextDirection);
        GameObject nextPath = Instantiate(pathPrefab, buildPoint.position, buildPoint.rotation);

        nextPath.name = "Clone " + count.ToString();
        count++;
        PathPrefab prefab = nextPath.GetComponent <PathPrefab>();

        // set path values
        prefab.blockDirection = nextDirection;
        prefab.blockObject    = nextPath;

        PathPrefab temp = pathQueue.Add(prefab);

        if (temp != null)
        {
            Destroy(temp.gameObject);
        }

        temp          = lastPath;
        lastPath      = prefab;
        lastDirection = nextDirection;

        return(prefab, temp);
    }
Пример #4
0
    public void SetupRamp(GameObject rampObject)
    {
        pathQueue = new CircularQueue <PathPrefab>(queueLength);
        buildPoint.transform.position = new Vector3(0, 0, 30);
        tearDownComplete = false;
        foreach (Transform child in rampObject.transform)
        {
            ramp = child.gameObject;
            ramp.GetComponent <BoxCollider>().enabled = false;
            pathQueue.Add(ramp.GetComponent <PathPrefab>());
        }

        lastDirection = Direction.North;
        lastPath      = ramp.GetComponent <PathPrefab>();

        mazePathA      = MazeCreator.FindPath(mazeDim, mazeDim, lastDirection);
        choosePathA    = true;
        mazePathIndex  = 0;
        mazePathBuffer = 10;
    }
Пример #5
0
    // Debug function
    IEnumerator TestCoinOrientation()
    {
        buildPoint.transform.position = new Vector3(-30, 0, 30);
        GameObject stand, head;
        Direction  cur = Direction.North; // testing only one direction

        foreach (Direction e in System.Enum.GetValues(typeof(Direction)))
        {
            foreach (int firstpiece in new int[] { 0, 1, 2 })
            {
                foreach (int secondpiece in new int[] { 0, 1, 2 })
                {
                    // ignore opposite direction block
                    if (e == cur.Opposite())
                    {
                        continue;
                    }

                    // bootstrap first piece
                    stand = Instantiate(pathPrefab, buildPoint.transform.position, DirectedRotation(cur));
                    PathPrefab standPrefab = stand.GetComponent <PathPrefab>();
                    standPrefab.blockDirection = cur;
                    standPrefab.blockObject    = stand;
                    coinGenerator.DebugNextCoins(1, firstpiece, standPrefab);

                    // create second piece as it would be created in game
                    head = Instantiate(pathPrefab, buildPoint.position + DirectedOffset(cur, e), DirectedRotation(e));
                    PathPrefab headPrefab = head.GetComponent <PathPrefab>();
                    headPrefab.blockDirection = e;
                    headPrefab.blockObject    = head;
                    coinGenerator.DebugNextCoins(1, secondpiece, headPrefab, standPrefab);

                    // next iteration after 2 seconds
                    yield return(new WaitForSeconds(2));

                    Destroy(head);
                    Destroy(stand);
                }
            }
        }
    }
Пример #6
0
    void RenderSetPiece(List <Tuple <ObjectType, int> > piece, PathPrefab prefab)
    {
        GameObject path = prefab.gameObject;

        foreach (Tuple <ObjectType, int> tup in piece)
        {
            Vector3 pos;
            if (tup.Item1 == ObjectType.ROCK)
            {
                pos = new Vector3(tup.Item2 / 10 * offsetx, offsety, tup.Item2 % 10 * offsetz);
            }
            else
            {
                // pre configured x and y values
                pos = new Vector3(0f, 0f, tup.Item2 % 10 * offsetz);
            }
            GameObject obs = Instantiate(GetObjectPrefab(tup.Item1)) as GameObject;
            obs.transform.parent          = path.transform;
            obs.transform.localPosition   = pos;
            obs.transform.rotation        = path.transform.rotation; // parent path orientation
            prefab.pathObjects[tup.Item2] = tup.Item1;
        }
    }
Пример #7
0
    public void nextCoins(PathPrefab prefab, PathPrefab prev, Difficulty level)
    {
        // bias type of coin path based on difficulty
        switch (level)
        {
        case Difficulty.EASY: {
            int type = new int[] { 0, 0, 1, 1, 2, 2, 3, 4 }[rng.Next(7)];
            RenderSetPiece(CreateLigature(type, rng.Next(setpieces[type].Length), prefab, prev), prefab);
            break;
        }

        case Difficulty.MED: {
            int type = new int[] { 0, 0, 0, 1, 2, 2, 3, 3, 4, 4 }[rng.Next(10)];
            RenderSetPiece(CreateLigature(type, rng.Next(setpieces[type].Length), prefab, prev), prefab);
            break;
        }

        case Difficulty.HARD: {
            int type = new int[] { 0, 0, 0, 0, 1, 3, 3, 3, 4, 4 }[rng.Next(10)];
            RenderSetPiece(CreateLigature(type, rng.Next(setpieces[type].Length), prefab, prev), prefab);
            break;
        }
        }
    }
Пример #8
0
    List <int> CreateLigature(int type, int piece, PathPrefab prefab, PathPrefab prev)
    {
        List <int>        coinPos = new List <int>(setpieces[type][piece]);
        RelativeDirection turn    = prev.blockDirection.relative(prefab.blockDirection);

        // trim excess coins
        switch (turn)
        {
        // In a left turn trim excess coins to the right
        case RelativeDirection.LEFT: {
            switch (prev.lastCoin / 10)
            {
            case 0: {
                coinPos.Remove(1);
                coinPos.Remove(11);
                coinPos.Remove(21);
                goto case 1;
            }

            case 1: {
                coinPos.Remove(0);
                coinPos.Remove(10);
                coinPos.Remove(20);
                goto default;
            }

            default: break;
            }
            break;
        }

        // In a right turn trim excess coins to the left
        case RelativeDirection.RIGHT: {
            switch (prev.lastCoin / 10)
            {
            case 2: {
                coinPos.Remove(1);
                coinPos.Remove(11);
                coinPos.Remove(21);
                goto case 1;
            }

            case 1: {
                coinPos.Remove(0);
                coinPos.Remove(10);
                coinPos.Remove(20);
                goto default;
            }

            default: break;
            }
            break;
        }
        }

        // join lines by extending from previous block vertically up until it
        // meets horizontal (left, right) line in current block
        switch (turn)
        {
        case RelativeDirection.LEFT: {
            switch (prev.lastCoin / 10)
            {
            case 0: {
                if (coinPos.Contains(2))
                {
                    break;
                }
                coinPos.Add(2);

                if (coinPos.Contains(12))
                {
                    break;
                }
                coinPos.Add(12);

                if (coinPos.Contains(22))
                {
                    break;
                }
                coinPos.Add(22);

                break;
            }

            case 1: {
                if (coinPos.Contains(1))
                {
                    break;
                }
                coinPos.Add(1);

                if (coinPos.Contains(11))
                {
                    break;
                }
                coinPos.Add(11);

                if (coinPos.Contains(21))
                {
                    break;
                }
                coinPos.Add(21);

                break;
            }

            case 2: {
                if (coinPos.Contains(0))
                {
                    break;
                }
                coinPos.Add(0);

                if (coinPos.Contains(10))
                {
                    break;
                }
                coinPos.Add(10);

                if (coinPos.Contains(20))
                {
                    break;
                }
                coinPos.Add(20);

                break;
            }
            }

            break;
        }

        case RelativeDirection.RIGHT: {
            switch (prev.lastCoin / 10)
            {
            case 0: {
                if (coinPos.Contains(20))
                {
                    break;
                }
                coinPos.Add(20);

                if (coinPos.Contains(10))
                {
                    break;
                }
                coinPos.Add(10);

                if (coinPos.Contains(0))
                {
                    break;
                }
                coinPos.Add(0);

                break;
            }

            case 1: {
                if (coinPos.Contains(21))
                {
                    break;
                }
                coinPos.Add(21);

                if (coinPos.Contains(11))
                {
                    break;
                }
                coinPos.Add(11);

                if (coinPos.Contains(1))
                {
                    break;
                }
                coinPos.Add(1);

                break;
            }

            case 2: {
                if (coinPos.Contains(22))
                {
                    break;
                }
                coinPos.Add(22);

                if (coinPos.Contains(12))
                {
                    break;
                }
                coinPos.Add(12);

                if (coinPos.Contains(2))
                {
                    break;
                }
                coinPos.Add(2);

                break;
            }
            }
            break;
        }
        }

        return(coinPos);
    }
Пример #9
0
 public void DebugNextCoins(int type, int piece, PathPrefab prefab, PathPrefab prev)
 {
     RenderSetPiece(CreateLigature(type, piece, prefab, prev), prefab);
 }
Пример #10
0
 public void DebugNextCoins(int type, int piece, PathPrefab prefab)
 {
     RenderSetPiece(new List <int>(setpieces[type][piece]), prefab);
 }
Пример #11
0
 public void RegisterPath(PathPrefab path)
 {
     _physicsPrefab.Paths.Add(path.Name, path);
 }
Пример #12
0
        public static PrefabEntity CreatePrefab()
        {
            PrefabEntity robot = new PrefabEntity {
                Name = "Robot"
            };

            /* gear bodies */
            ShapePrefab gearShape = new ShapePrefab {
                Type = ShapeType.Circle, XRadius = 14f, Density = 1f
            };

            BodyPrefab gear1 = new BodyPrefab
            {
                Name          = "Gear1",
                Friction      = 0.5f,
                Restitution   = 0.1f,
                LocalPosition = new Vector2(-27.93f, -20.13f),
                Static        = false,
                Shapes        = new List <ShapePrefab> {
                    gearShape
                },
            };

            BodyPrefab gear2 = new BodyPrefab
            {
                Name          = "Gear2",
                Friction      = 0.5f,
                Restitution   = 0.1f,
                LocalPosition = new Vector2(27.93f, -20.13f),
                Static        = false,
                Shapes        = new List <ShapePrefab> {
                    gearShape
                },
            };

            BodyPrefab gear3 = new BodyPrefab
            {
                Name          = "Gear3",
                Friction      = 0.5f,
                Restitution   = 0.1f,
                LocalPosition = new Vector2(0f, 28.25f),
                Static        = false,
                Shapes        = new List <ShapePrefab> {
                    gearShape
                },
            };

            robot.RegisterBody(gear1);
            robot.RegisterBody(gear2);
            robot.RegisterBody(gear3);

            /* chassis body */
            ShapePrefab chassisShape = new ShapePrefab {
                Type = ShapeType.Polygon, Density = 4f, Polygon = new List <Vertices> {
                    new Vertices()
                }
            };

            chassisShape.Polygon[0].Add(new Vector2(-15.41f, -13.87f));
            chassisShape.Polygon[0].Add(new Vector2(0f, 13.74f));
            chassisShape.Polygon[0].Add(new Vector2(15.41f, -13.87f));

            BodyPrefab chassis = new BodyPrefab
            {
                Name          = "Chassis",
                Restitution   = 0.1f,
                Friction      = 0.5f,
                LocalPosition = Vector2.Zero,
                Static        = false,
                Shapes        = new List <ShapePrefab> {
                    chassisShape
                },
            };

            robot.RegisterBody(chassis, true);               // is main body

            /* gear joints */
            const float jointLengthModifier = 1f;

            JointPrefab gearJoint1 = new JointPrefab
            {
                Type           = JointType.Line,
                Name           = "GearJoint1",
                Body1          = "Chassis",
                Body2          = "Gear1",
                Anchor         = gear1.LocalPosition,
                Axis           = new Vector2(0.66f, -0.33f) * jointLengthModifier,
                MaxMotorTorque = 10f,
                Frequency      = 10f,
                DampingRatio   = 0.85f,
                MotorEnabled   = true,
            };

            JointPrefab gearJoint2 = new JointPrefab
            {
                Type           = JointType.Line,
                Name           = "GearJoint2",
                Body1          = "Chassis",
                Body2          = "Gear2",
                Anchor         = gear2.LocalPosition,
                Axis           = new Vector2(-0.66f, -0.33f) * jointLengthModifier,
                MaxMotorTorque = 10f,
                Frequency      = 10f,
                DampingRatio   = 0.85f,
                MotorEnabled   = true
            };

            JointPrefab gearJoint3 = new JointPrefab
            {
                Type           = JointType.Line,
                Name           = "GearJoint3",
                Body1          = "Chassis",
                Body2          = "Gear3",
                Anchor         = gear3.LocalPosition,
                Axis           = new Vector2(0f, 0.76f) * jointLengthModifier,
                MaxMotorTorque = 10f,
                Frequency      = 10f,
                DampingRatio   = 0.85f,
                MotorEnabled   = true
            };

            robot.RegisterJoint(gearJoint1);
            robot.RegisterJoint(gearJoint2);
            robot.RegisterJoint(gearJoint3);

            ShapePrefab linkShape = new ShapePrefab
            {
                Type    = ShapeType.Rectangle,
                Width   = 2.51f,
                Height  = 8.88f,
                Density = 2f,
                Offset  = Vector2.Zero
            };

            ShapePrefab linkFeetShape = new ShapePrefab
            {
                Type    = ShapeType.Rectangle,
                Width   = 2.51f,
                Height  = 4f,
                Density = 2f,
                Offset  = new Vector2(2f, 0f)
            };

            BodyPrefab link = new BodyPrefab
            {
                Name   = "Link",
                Shapes = new List <ShapePrefab> {
                    linkShape, linkFeetShape
                },
                Friction    = 1f,
                Restitution = 0f,
            };

            PathPrefab track = new PathPrefab
            {
                Name                = "Track",
                Anchor1             = new Vector2(-1.5f, -4.4f),
                Anchor2             = new Vector2(-1.5f, 4.4f),
                BodyCount           = 29,
                CollideConnected    = false,
                ConnectFirstAndLast = true,
                JointType           = JointType.Revolute,
                Path                = new List <Vector2>
                {
                    new Vector2(60f, -32f),
                    new Vector2(0f, 51f),
                    new Vector2(-50f, -32f),
                    new Vector2(55f, -38f)
                },
                Body = link
            };

            robot.RegisterPath(track);

            /* scene nodes */
            MeshSceneNodePrefab gearNode1 = new MeshSceneNodePrefab
            {
                Name     = "Gear1",
                Mesh     = "models/robo_wheel",
                Material = "RobotMaterial",
            };

            MeshSceneNodePrefab gearNode2 = new MeshSceneNodePrefab
            {
                Name     = "Gear2",
                Mesh     = "models/robo_wheel",
                Material = "RobotMaterial",
            };

            MeshSceneNodePrefab gearNode3 = new MeshSceneNodePrefab
            {
                Name     = "Gear3",
                Mesh     = "models/robo_wheel",
                Material = "RobotMaterial",
            };

            robot.RegisterMeshSceneNode(gearNode1);
            robot.RegisterMeshSceneNode(gearNode2);
            robot.RegisterMeshSceneNode(gearNode3);


            MeshSceneNodePrefab chassisNode = new MeshSceneNodePrefab
            {
                Name     = "ChassisNode",
                Mesh     = "models/robo_body",
                Material = "RobotMaterial",
            };

            robot.RegisterMeshSceneNode(chassisNode);

            ArrayMeshSceneNodePrefab linkNode = new ArrayMeshSceneNodePrefab
            {
                Name       = "Links",
                Mesh       = "models/robo_link",
                Material   = "RobotMaterial",
                Path       = "Track",
                StartIndex = 0,
                EndIndex   = 28
            };

            robot.RegisterArrayMeshSceneNode(linkNode);

            ControllerPrefab gear1Controller = new ControllerPrefab
            {
                Name          = "Gear1BodyController",
                Type          = ControllerType.BodyController,
                Body          = "Gear1",
                Transformable = "Gear1"
            };

            robot.RegisterController(gear1Controller);

            ControllerPrefab gear2Controller = new ControllerPrefab
            {
                Name          = "Gear2BodyController",
                Type          = ControllerType.BodyController,
                Body          = "Gear2",
                Transformable = "Gear2"
            };

            robot.RegisterController(gear2Controller);

            ControllerPrefab gear3Controller = new ControllerPrefab
            {
                Name          = "Gear3BodyController",
                Type          = ControllerType.BodyController,
                Body          = "Gear3",
                Transformable = "Gear3"
            };

            ControllerPrefab chassisController = new ControllerPrefab
            {
                Name          = "ChassisController",
                Type          = ControllerType.BodyController,
                Body          = "Chassis",
                Transformable = "ChassisNode"
            };

            robot.RegisterController(chassisController);

            robot.RegisterController(gear3Controller);

            return(robot);
        }