コード例 #1
0
    public void LevelGenerate(LevelState lvl)
    {
        Team      team      = Global.GetComponent <Team>();
        EventBind checkGoal = new EventBind(this, nameof(GoalCheck));

        if (lvl == null)
        {
            lvl                  = new LevelState();
            lvl.stage            = maze.stage;
            lvl.seed             = random.Seed;
            lvl.idolsDistributed = idolCreator.idolsDistributed;
            if (inventory.GetItems() != null)
            {
                lvl.idolInventory = CodeConvert.Stringify(inventory.GetItems().ConvertAll(go => {
                    Idol t = go.GetComponent <Idol>(); return(t ? t.intMetaData : null);
                }));
            }
            else
            {
                lvl.idolInventory = "";
            }
            lvl.variables = CodeConvert.Stringify(mainDictionaryKeeper.Dictionary);
            lvl.allies    = CodeConvert.Stringify(team.members.ConvertAll(m => npcCreator.npcs.FindIndex(n => n.gameObject == m)));
            levels.Add(lvl);
        }
        else
        {
            Tokenizer tokenizer = new Tokenizer();
            // check if level is valid
            if (levels.IndexOf(lvl) < 0)
            {
                throw new Exception("TODO validate the level plz!");
            }
            // set allies
            int[] allies; CodeConvert.TryParse(lvl.allies, out allies, null, tokenizer);
            team.Clear();
            team.AddMember(firstPlayer);
            for (int i = 0; i < allies.Length; ++i)
            {
                int index = allies[i];
                if (index < 0)
                {
                    continue;
                }
                team.AddMember(npcCreator.npcs[index].gameObject);
            }
            // clear existing idols
            idolCreator.Clear();
            // reset inventory to match start state
            inventory.RemoveAllItems();
            int[][] invToLoad;
            CodeConvert.TryParse(lvl.idolInventory, out invToLoad, null, tokenizer);
            //Debug.Log(Show.Stringify(invToLoad,false));
            Vector3 playerLoc = Global.GetComponent <CharacterControlManager>().localPlayerInterfaceObject.transform.position;
            for (int i = 0; i < invToLoad.Length; ++i)
            {
                int[] t = invToLoad[i];
                if (t == null || t.Length == 0)
                {
                    continue;
                }
                GameObject idol = idolCreator.CreateIdol(t[0], t[1], checkGoal);
                idol.transform.position = playerLoc + Vector3.forward;
                inventory.AddItem(idol);
            }
            // set stage
            maze.stage = lvl.stage;
            // set seed
            random.Seed = lvl.seed;
            // set variables
            HashTable_stringobject d = mainDictionaryKeeper.Dictionary;
            CodeConvert.TryParse(lvl.variables, out d, null, tokenizer);
            // set
        }
        MarkTouchdown.ClearMarkers();
        clickToMove.ClearAllWaypoints();
        seedLabel.text = "level " + maze.stage + "." + Convert.ToBase64String(BitConverter.GetBytes(random.Seed));
        maze.Generate(random);
        Discovery.ResetAll();
        idolCreator.Generate(checkGoal);
        int len = Mathf.Min(maze.floorTileNeighborHistogram[2], idolCreator.idolMaterials.Count);

        npcCreator.GenerateMore(len);
        if (testingPickups)
        {
            idolCreator.GenerateMoreIdols(checkGoal);
        }
        // TODO maze should have a list of unfilled tiles sorted by weight
        for (int i = 0; i < npcCreator.npcs.Count; ++i)
        {
            maze.PlaceObjectOverTile(npcCreator.npcs[i].transform, maze.floorTiles[maze.floorTileNeighborHistogram[1] + i]);
        }
        team.AddMember(firstPlayer);
        maze.PlaceObjectOverTile(team.members[0].transform, maze.floorTiles[maze.floorTiles.Count - 1]);
        Vector3 pos = team.members[0].transform.position;

        for (int i = 0; i < team.members.Count; ++i)
        {
            team.members[i].transform.position = pos;
            CharacterMove cm = team.members[i].GetComponent <CharacterMove>();
            if (cm != null)
            {
                cm.SetAutoMovePosition(pos);
                cm.MoveForwardMovement = 0;
                cm.StrafeRightMovement = 0;
            }
        }
        UiText.SetColor(timer.gameObject, Color.white);
        timer.Start();
        GoalCheck(null);
        nextLevelButton.SetActive(false);
        bestTimeLabel.gameObject.SetActive(false);
    }
コード例 #2
0
    void Update()
    {
        if (visionParticle != null)
        {
            if (characterMover.JumpButtonTimed > 0 && !visionParticle.isPlaying)
            {
                visionParticle.Play();
            }
            else if (characterMover.JumpButtonTimed == 0 && visionParticle.isPlaying)
            {
                visionParticle.Stop();
            }
        }
        Coord mapSize = maze.Map.GetSize();

        if (mapAstar == null)
        {
            mapAstar = new Map2dAStar(() => canJump, maze, discovery.vision, _t, prefab_debug_astar);
        }
        mapAstar.UpdateMapSize();
        Vector3 p    = _t.position;
        Coord   here = maze.GetCoord(p);

        if (follower.waypoints.Count > 0)
        {
            Coord there = maze.GetCoord(follower.waypoints[0].positon);
            if (here == there)
            {
                follower.NotifyWayPointReached();
            }
        }
        List <Coord> moves = mapAstar.Moves(here, canJump);

        if (textOutput != null)
        {
            UiText.SetText(textOutput, here.ToString() + ":" + (p - maze.transform.position) + " " + moves.JoinToString(", "));
        }
        if (useVisionParticle && visionParticle)
        {
            timer -= Time.deltaTime;
            if (timer <= 0)
            {
                mapSize.ForEach(co => {
                    if (discovery.vision[co])
                    {
                        Vector3 po = maze.GetPosition(co);
                        po.y       = _t.position.y;
                        visionParticle.transform.position = po;
                        visionParticle.Emit(1);
                    }
                });
                timer = .5f;
            }
        }
        switch (aiBehavior)
        {
        case AiBehavior.RandomLocalEdges:
            if (!characterMover.IsAutoMoving())
            {
                Coord c = moves[Random.Next(moves.Count)];
                characterMover.SetAutoMovePosition(MoveablePosition(c, p));
            }
            break;

        case AiBehavior.RandomInVision:
            if (mapAstar.goal == here)
            {
                if (mapAstar.RandomVisibleNode(out Coord there, here))
                {
                    //Debug.Log("startover #");
                    //Debug.Log("goal " + there+ " "+astar.IsFinished());
                }
                else
                {
                    mapAstar.RandomNeighborNode(out there, here);
                }
                mapAstar.Start(here, there);
            }
            else
            {
                // iterate astar algorithm
                if (!mapAstar.IsFinished())
                {
                    mapAstar.Update();
                }
                else if (mapAstar.BestPath == null)
                {
                    //Debug.Log("f" + astar.IsFinished() + " " + astar.BestPath);
                    mapAstar.Start(here, here);
                    //Debug.Log("startover could not find path");
                }
                if (mapAstar.BestPath != null)
                {
                    if (mapAstar.BestPath != currentBestPath)
                    {
                        currentBestPath = mapAstar.BestPath;
                        List <Coord> nodes = new List <Coord>();
                        Coord        c     = mapAstar.start;
                        nodes.Add(c);
                        for (int i = currentBestPath.Count - 1; i >= 0; --i)
                        {
                            c = mapAstar.NextNode(c, currentBestPath[i]);
                            nodes.Add(c);
                        }
                        //Debug.Log(currentBestPath.JoinToString(", "));
                        indexOnBestPath = nodes.IndexOf(here);
                        if (indexOnBestPath < 0)
                        {
                            mapAstar.Start(here, mapAstar.goal);
                            //Debug.Log("startover new better path");
                        }
                        Vector3 pos = p;
                        follower.ClearWaypoints();
                        for (int i = 0; i < currentBestPath.Count; ++i)
                        {
                            pos = MoveablePosition(nodes[i + 1], pos);
                            //pos.y += follower.CharacterHeight;
                            //Show.Log(i + " " + nodes.Count + " " + currentBestPath.Count + " " + (currentBestPath.Count - i - 1));
                            MazeAStar.EdgeMoveType moveType = MazeAStar.GetEdgeMoveType(currentBestPath[currentBestPath.Count - i - 1]);
                            switch (moveType)
                            {
                            case MazeAStar.EdgeMoveType.Walk: follower.AddWaypoint(pos, false); break;

                            case MazeAStar.EdgeMoveType.Fall: follower.AddWaypoint(pos, false, 0, true); break;

                            case MazeAStar.EdgeMoveType.Jump: follower.AddWaypoint(pos, false, characterMover.jump.fullPressDuration); break;
                            }
                        }
                        follower.SetCurrentTarget(pos);
                        follower.UpdateLine();
                        follower.doPrediction = true;
                    }
                    else
                    {
                        if (!characterMover.IsAutoMoving() && follower.waypoints.Count == 0)
                        {
                            mapAstar.Start(here, here);
                            //Debug.Log("startover new level?");
                        }
                    }
                }
            }
            break;
        }
    }