Exemplo n.º 1
0
    private CodeTile CodeTileInitPos(int xPos, int yPos)
    {
        CodeTile curTile = new CodeTile();

        curTile.posX = xPos;
        curTile.posY = yPos;
        return(curTile);
    }
Exemplo n.º 2
0
    public Peep GeneratePeep()
    {
        if (SpawnRoadTiles.Count > 0)
        {
            Peep peep = Object.Instantiate(peepPrefab);
            peep.name = $"{peep.Type.ToString()} - {peep.FirstName} {peep.SirName}";
            peep.transform.SetParent(this.MyWorld.peepList.transform);
            peep.MyPeepGenerator    = this;
            peep.notificationCanvas = notificationCanvas;
            UpdatePeepCloths(peep);

            int      randomStartIndex = Random.Range(0, SpawnRoadTiles.Count - 1);
            CodeTile startTile        = SpawnRoadTiles[randomStartIndex];
            peep.OnTopOfTile = startTile;
            peep.pos         = new Vector3(startTile.posX + 0.5f, startTile.posY + 0.5f, 1);

            CodeTile ranRoad = GetRandomRoadTile(startTile);

            // Put this into the level config later:
            // Also make this a variable in the world that can change:
            int passingByChance = 40; // out of 100

            if (IsPassingBy(passingByChance))
            {
                CodeTile ranExitTile = GetRandomExitTile();
                int      tries       = 10;
                while (ranExitTile.posX == startTile.posX && ranExitTile.posY == startTile.posY && tries > 0)
                {
                    ranExitTile = GetRandomExitTile();
                    tries--;
                }

                if (AllRoadTiles.Contains(ranExitTile))
                {
                    peep.MyTasks.Push(new TaskLeave(peep));
                    peep.MyTasks.Push(new TaskWalk(peep, ranExitTile, AllRoadTiles, true));
                }

                string arrivalMessage = $"{peep.FirstName} {peep.SirName} arrived and is just passing by.";
                notificationCanvas.AddNotification(Notification.Type.peepArrival, arrivalMessage);
            }
            else
            {
                peep.MyTasks.Push(new TaskWalk(peep, ranRoad, AllRoadTiles, false));

                string arrivalMessage = $"{peep.FirstName} {peep.SirName} has arrived!";
                notificationCanvas.AddNotification(Notification.Type.peepArrival, arrivalMessage);
            }

            return(peep);
        }
        else
        {
            return(null);
        }
    }
Exemplo n.º 3
0
    public void Build()
    {
        Debug.Log($"Let's build the building: {buildingType}!");

        // Construct prefab
        Building createdBuilding = Instantiate(myBuildingPrefab);

        createdBuilding.Rotation = myRotation;
        createdBuilding.UpdateRotationSprite();

        this.world.AddBuilding(createdBuilding);

        foreach (BuildingSelectionSquare square in mySquares)
        {
            CodeTile curTile = square.TileUnderneath;

            // Both:
            curTile.ParentBuilding = createdBuilding;


            // Buildings:
            if (square.myType == BuildingSelectionSquare.Type.building)
            {
                curTile.UpdateTileType(CodeTile.Type.building);
                createdBuilding.TilesUnderneath.Add(curTile);
            }

            // Entrances:
            else
            {
                createdBuilding.Entrances.Add(curTile);

                if (createdBuilding.sign == null)
                {
                    SignPost sign = CreateSignPost(square.TileUnderneath.posX, square.TileUnderneath.posY, myRotation);
                    createdBuilding.sign = sign;
                    //sign.transform.SetParent(createdBuilding.transform);
                }
            }
        }
        this.world.TileMapUpdate();

        // Modify prefab:
        BuildingSelectionSquare bottomLeft = GetBottomLeftSquare();

        createdBuilding.originX = this.originX + bottomLeft.relativeX;
        createdBuilding.originY = this.originY + bottomLeft.relativeY;

        // Play sound effect

        // Notification:
        world.notificationCanvas.AddNotification(Notification.Type.userAction, $"Successfully constructed {createdBuilding.Name}.");
    }
Exemplo n.º 4
0
    // Update is called once per frame
    void Update()
    {
        if (text != null)
        {
            Vector2 point  = world.cameraPrefab.ScreenToWorldPoint(Input.mousePosition);
            int     worldX = (int)point.x;
            int     worldY = (int)point.y;

            currentTile = world.TileAt(worldX, worldY);

            CodeTile above = null;
            CodeTile right = null;
            CodeTile below = null;
            CodeTile left  = null;
            if (currentTile != null)
            {
                above = currentTile.TileAbove;
                right = currentTile.TileRight;
                below = currentTile.TileBelow;
                left  = currentTile.TileLeft;
            }

            text.text = $"{worldX},{worldY}";

            if (currentTile != null)
            {
                text.text += $"\t{currentTile.posX},{currentTile.posY}\n";
                if (above != null)
                {
                    text.text += $"\t↑({above.posX},{above.posY})";
                }
                if (right != null)
                {
                    text.text += $"\t➝({right.posX},{right.posY})";
                }
                if (below != null)
                {
                    text.text += $"\t↓({below.posX},{below.posY})";
                }
                if (left != null)
                {
                    text.text += $"\t←({left.posX},{left.posY})";
                }
                text.text += $"\nType: {currentTile.TileType}";
                string isEdge = currentTile.isMapEdge ? "yes" : "no";
                text.text += $"\nExit?: {isEdge}";
            }

            this.transform.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y + 60);
        }
    }
Exemplo n.º 5
0
    public TaskWalk(Peep peep, CodeTile targetTile, List <CodeTile> allRoads, bool ignoresFatigue)
    {
        this.MyPeep     = peep;
        this.TaskType   = TaskInfo.Type.WALK_RANDOM;
        this.StartTile  = peep.OnTopOfTile;
        this.TargetTile = targetTile;
        this.AllRoads   = allRoads;

        this.MyPath = TaskInfo.GenerateWalkQueue(this.StartTile, this.TargetTile, this.AllRoads);
        //Debug.Log($"Generated path with {this.MyPath.Count} steps between tile {StartTile.posX},{StartTile.posY} and target {TargetTile.posX},{TargetTile.posY}");

        this.curStep       = this.StartTile;
        this.nextStep      = this.GetNextStep();
        this.prevFacingPos = this.MyPeep.pos;

        this.IgnoresFatigue = ignoresFatigue;
    }
Exemplo n.º 6
0
    public CodeTile GetRandomRoadTile(CodeTile curTile)
    {
        int      tries    = 30;
        CodeTile nextTile = MyWorld.GetRandomRoadTile();

        while (tries > 0 && nextTile.posX == curTile.posX && nextTile.posY == curTile.posY)
        {
            nextTile = MyWorld.GetRandomRoadTile();
            tries--;
        }

        if (tries <= 0)
        {
            Debug.Log("Unable to find other path");
        }

        return(nextTile);
    }
Exemplo n.º 7
0
    private void CodeTilesInit()
    {
        WorldTiles = new CodeTile[WorldHeight][];
        CodeTile curTile;

        for (int h = 0; h < WorldHeight; h++)
        {
            WorldTiles[h] = new CodeTile[WorldWidth];
            for (int w = 0; w < WorldWidth; w++)
            {
                curTile = CodeTileInitPos(w, h);
                curTile.UpdateTileType(CodeTile.Type.grass);
                curTile.world    = this;
                WorldTiles[h][w] = curTile;
            }
        }
        SetTileNeighbors();
        codeTilesReady = true;
    }
Exemplo n.º 8
0
    public bool CheckNecessaryTasks(Peep peep)
    {
        if (peep.Stamina <= 0)
        {
            notificationCanvas.AddNotification(Notification.Type.peepDeparture, $"{peep.FirstName} {peep.SirName} is tired, going home.");
            CodeTile ranExitTile = GetRandomExitTile();

            if (AllRoadTiles.Contains(ranExitTile))
            {
                peep.MyTasks.Push(new TaskLeave(peep));
                peep.MyTasks.Push(new TaskWalk(peep, ranExitTile, AllRoadTiles, true));

                return(true);
            }
        }

        // No necessary tasks
        return(false);
    }
Exemplo n.º 9
0
    public void FollowMouse()
    {
        Vector2 point  = world.cameraPrefab.ScreenToWorldPoint(Input.mousePosition);
        int     worldX = (int)point.x;
        int     worldY = (int)point.y;

        if (worldX >= 0 && worldX <= world.WorldWidth)
        {
            lastValidMouseX = worldX;
        }

        if (worldY >= 0 && worldY <= world.WorldHeight)
        {
            lastValidMouseY = worldY;
        }

        TileUnderMouse = world.TileAt(lastValidMouseX, lastValidMouseY);

        this.originX = lastValidMouseX;
        this.originY = lastValidMouseY;
    }
Exemplo n.º 10
0
    public void SetPeepDirection()
    {
        CodeTile cur  = this.curStep;
        CodeTile next = this.nextStep;

        if (next == cur.TileAbove)
        {
            this.MyPeep.SetDirection(Peep.Direction.Up);
        }
        else if (next == cur.TileRight)
        {
            this.MyPeep.SetDirection(Peep.Direction.Right);
        }
        else if (next == cur.TileBelow)
        {
            this.MyPeep.SetDirection(Peep.Direction.Down);
        }
        else if (next == cur.TileLeft)
        {
            this.MyPeep.SetDirection(Peep.Direction.Left);
        }
    }
Exemplo n.º 11
0
    public Vector2 RetrieveFacingPositionOnRoad(Vector2 target, float amount)
    {
        Vector2  newTarget = target;
        CodeTile cur       = this.curStep;
        CodeTile next      = this.nextStep;

        if (next == cur.TileAbove)
        {
            newTarget.x += amount;
        }
        else if (next == cur.TileRight)
        {
            newTarget.y -= amount;
        }
        else if (next == cur.TileBelow)
        {
            newTarget.x -= amount;
        }
        else if (next == cur.TileLeft)
        {
            newTarget.y += amount;
        }
        return(newTarget);
    }
Exemplo n.º 12
0
    // Revisit this after implementing the tile.Above, tile.Left, etc...

    public static Stack <CodeTile> GenerateWalkQueue(CodeTile startTile, CodeTile targetTile, List <CodeTile> allRoadTiles)
    {
        Assert.IsTrue(allRoadTiles.Contains(startTile));
        Assert.IsTrue(allRoadTiles.Contains(targetTile));

        Queue <CodeTile> tileQueue   = new Queue <CodeTile>();                                // paths to test
        Stack <CodeTile> returnQueue = new Stack <CodeTile>();                                // our final path
        Dictionary <CodeTile, CodeTile> visitedTiles = new Dictionary <CodeTile, CodeTile>(); // tells us 1. where we visited, and 2. where from

        visitedTiles[startTile] = null;                                                       // our starting tile has no "from"

        tileQueue.Enqueue(startTile);
        CodeTile cur;

        while (tileQueue.Count > 0)
        {
            cur = tileQueue.Dequeue();

            // Target has been found:
            if (cur == targetTile)
            {
                returnQueue.Push(cur);
                CodeTile to   = cur;
                CodeTile from = visitedTiles[to];
                while (from != startTile && from != null)
                {
                    returnQueue.Push(from);
                    to = from;
                    if (visitedTiles.ContainsKey(to))
                    {
                        from = visitedTiles[to];
                    }
                    else
                    {
                    }
                }

                return(returnQueue);
            }

            // Target has not been found, push all neighbors not yet visited:
            else
            {
                CodeTile t = cur.TileAbove;
                if (t != null && t.isPath && !visitedTiles.ContainsKey(t))
                {
                    tileQueue.Enqueue(t);
                    visitedTiles[t] = cur;
                }

                t = cur.TileRight;
                if (t != null && t.isPath && !visitedTiles.ContainsKey(t))
                {
                    tileQueue.Enqueue(t);
                    visitedTiles[t] = cur;
                }

                t = cur.TileBelow;
                if (t != null && t.isPath && !visitedTiles.ContainsKey(t))
                {
                    tileQueue.Enqueue(t);
                    visitedTiles[t] = cur;
                }

                t = cur.TileLeft;
                if (t != null && t.isPath && !visitedTiles.ContainsKey(t))
                {
                    tileQueue.Enqueue(t);
                    visitedTiles[t] = cur;
                }
            } // end else (not found)
        }     // end while-queue size > 0

        return(returnQueue);
    }
Exemplo n.º 13
0
    private void SetTileTypeFromChar(int row, int col, char c)
    {
        CodeTile curTile = WorldTiles[row][col];

        switch (c)
        {
        case 'T':
            curTile.UpdateTileType(CodeTile.Type.tree);
            TreeTiles.Add(curTile);
            break;

        case 'S':
            curTile.UpdateTileType(CodeTile.Type.stone);
            StoneTiles.Add(curTile);
            break;

        case 'W':
            curTile.UpdateTileType(CodeTile.Type.water);
            break;

        case 'D':
            curTile.UpdateTileType(CodeTile.Type.road);
            curTile.isPath = true;
            RoadTiles.Add(curTile);
            break;

        case '1':
            curTile.UpdateTileType(CodeTile.Type.road);
            curTile.isPath    = true;
            curTile.isMapEdge = true;
            curTile.mapEdgeId = 1;
            SpawnTiles.Add(curTile);
            RoadTiles.Add(curTile);
            break;

        case '2':
            curTile.UpdateTileType(CodeTile.Type.road);
            curTile.isPath    = true;
            curTile.isMapEdge = true;
            curTile.mapEdgeId = 2;
            SpawnTiles.Add(curTile);
            RoadTiles.Add(curTile);
            break;

        case '3':
            curTile.UpdateTileType(CodeTile.Type.road);
            curTile.isPath    = true;
            curTile.isMapEdge = true;
            curTile.mapEdgeId = 3;
            SpawnTiles.Add(curTile);
            RoadTiles.Add(curTile);
            break;

        case '4':
            curTile.UpdateTileType(CodeTile.Type.road);
            curTile.isPath    = true;
            curTile.isMapEdge = true;
            curTile.mapEdgeId = 4;
            SpawnTiles.Add(curTile);
            RoadTiles.Add(curTile);
            break;

        case '5':
            curTile.UpdateTileType(CodeTile.Type.road);
            curTile.isPath    = true;
            curTile.isMapEdge = true;
            curTile.mapEdgeId = 5;
            SpawnTiles.Add(curTile);
            RoadTiles.Add(curTile);
            break;

        default:
            break;
        }
    }
Exemplo n.º 14
0
    public void GenerateTaskKing(Peep peep)
    {
        CodeTile ranRoad = GetRandomRoadTile(peep.OnTopOfTile);

        peep.MyTasks.Push(new TaskWalk(peep, ranRoad, AllRoadTiles, false));
    }
Exemplo n.º 15
0
    public void GenerateNextTask(Peep peep)
    {
        bool hasTask = CheckNecessaryTasks(peep);


        // Building occupy test:
        if (!hasTask)
        {
            List <CodeTile> entrances = this.MyWorld.RetrieveBuildingEntranceTiles(); // later use the specific entrance search
            if (entrances.Count > 0)
            {
                int      index          = Random.Range(0, entrances.Count);
                CodeTile entrance       = entrances[index];
                Building targetBuilding = entrance.ParentBuilding;


                string message = $"{peep.FirstName} is heading to the {targetBuilding.Name}";
                peep.notificationCanvas.AddNotification(Notification.Type.peepArrival, message);

                peep.MyTasks.Push(new TaskOccupyBuilding(peep, targetBuilding));
                peep.MyTasks.Push(new TaskWalk(peep, entrance, this.MyWorld.RetrieveAllRoadTiles(), false));

                hasTask = true;
            }
        }



        if (!hasTask)
        {
            switch (peep.Type)
            {
            case PeepInfo.Type.child: this.GenerateTaskChild(peep); break;

            case PeepInfo.Type.homeless: this.GenerateTaskHomeless(peep); break;

            case PeepInfo.Type.thief: this.GenerateTaskThief(peep); break;

            case PeepInfo.Type.farmer: this.GenerateTaskFarmer(peep); break;

            case PeepInfo.Type.trader: this.GenerateTaskTrader(peep); break;

            case PeepInfo.Type.bard: this.GenerateTaskBard(peep); break;

            case PeepInfo.Type.monk: this.GenerateTaskMonk(peep); break;

            case PeepInfo.Type.nun: this.GenerateTaskNun(peep); break;

            case PeepInfo.Type.priest: this.GenerateTaskPriest(peep); break;

            case PeepInfo.Type.bishop: this.GenerateTaskBishop(peep); break;

            case PeepInfo.Type.knight: this.GenerateTaskKnight(peep); break;

            case PeepInfo.Type.quester: this.GenerateTaskQuester(peep); break;

            case PeepInfo.Type.foreigner: this.GenerateTaskForeigner(peep); break;

            case PeepInfo.Type.witch: this.GenerateTaskWitch(peep); break;

            case PeepInfo.Type.elder: this.GenerateTaskElder(peep); break;

            case PeepInfo.Type.wizard: this.GenerateTaskWizard(peep); break;

            case PeepInfo.Type.lady: this.GenerateTaskLady(peep); break;

            case PeepInfo.Type.king: this.GenerateTaskKing(peep); break;

            default: break;
            }
        }
    }