Esempio n. 1
0
    void DragPreview()
    {
        Dictionary <Node, Action> actionLocations = actionController.actionLocations;

        foreach (Node n in actionLocations.Keys)
        {
            int x = n.x;
            int y = n.y;

            Action act = actionLocations[n];

            //display preview of action at tile
            GameObject go = actionController.GetPreview(act);
            go.transform.position = new Vector3(x, worldController.GetObjectFloat(x, y), y);                    //don't for
            dragPreviewGameObjects.Add(go);

            //align preview
            StructureData data = StructureDatabase.GetData(act.What);
            if (data != null)
            {
                float alignx = data.Alignx;
                float aligny = data.Aligny;
                go.transform.position += new Vector3(alignx, 0, aligny);
            }

            //change color of preview
            Material mat = new Material(canDo);

            //if can do, make it white
            if (actionController.CanDo(act, x, y) && act.Do != "demolish")
            {
                mat.color = new Color(1, 1, 1, .5f);
            }
            //else, if it's paint, make it invisible
            else if (act.Do == "paint")
            {
                go.SetActive(false);
            }
            //else if we're demolishing but we can't here
            else if (act.Do == "demolish" && !actionController.CanDo(act, x, y))
            {
                go.SetActive(false);
            }
            //else, make it red
            else
            {
                mat.color = new Color(1, 0, 0, .5f);
            }

            foreach (Transform child in go.transform)
            {
                Material[] mats = child.GetComponent <MeshRenderer>().materials;
                for (int a = 0; a < mats.Length; a++)
                {
                    mats[a] = mat;
                }
                child.GetComponent <MeshRenderer>().materials = mats;
            }
        }
    }
Esempio n. 2
0
    public void Start()
    {
        if (StructureDatabase.HasData(act.What) && desc != null)
        {
            desc.text = StructureDatabase.GetData(act.What).displayName;
        }
        else if (desc != null)
        {
            desc.text = act.What;
        }

        if (StructureDatabase.HasData(act.What) && priceLabel != null)
        {
            int price = StructureDatabase.GetModifiedCost(act.What);
            priceLabel.text = MoneyController.symbol + "" + price;
        }
        else if (priceLabel != null)
        {
            priceLabel.text = null;
        }

        Sprite i = Resources.Load <Sprite>("ActionImgs/" + act.What);

        if (i != null && img != null)
        {
            img.sprite = i;
        }
    }
Esempio n. 3
0
 public Database()
 {
     ItemDatabase      = new ItemDatabase();
     TileDatabase      = new TileDatabase();
     WorldDatabase     = new WorldDatabase();
     BiomeDatabase     = new BiomeDatabase();
     StructureDatabase = new StructureDatabase();
 }
    public void RotateStructure(float deg) {
		if (currentAction == null)
			return;
		if (currentAction.Do != "place")
			return;
		StructureData data = StructureDatabase.GetData(currentAction.What);

		//if old style-graphics are enabled, and the building is a basic square shape without any quirks, don't let it rotate
		if (data.sizex == data.sizey && !data.hasWaterTiles && !data.hasRoadTiles && Settings.oldGraphics)
			return;
		buildingRotation += deg;
        if (buildingRotation >= 360)
            buildingRotation = 0;
    }
	public float GetConstructCost(Dictionary<Node, Action> acts, Action a) {

		int count = acts.Count;

		int singleCost = StructureDatabase.GetModifiedCost(a.What);

		if (count == 0)
			return singleCost;

		foreach (Node n in acts.Keys)
			if (!CanDo(acts[n], n.x, n.y))
				count--;
		return singleCost * count;

    }
	public bool DoStraightLine(Action act) {

		if (!CanDrag(act))
			return false;

		else if (act.Do == "place") {

			StructureData data = StructureDatabase.GetData(act.What);
			return data.straightLineDrag;

		}

		return false;

	}
Esempio n. 7
0
 private void Start()
 {
     if (Action.Do.Contains("place"))
     {
         label.text = StructureDatabase.GetData(Action.What).displayName;
     }
     else if (Action.Do.Contains("open"))
     {
         label.text = Action.What + " Category";
     }
     else
     {
         label.text = Action.What;
     }
     categoryLabel.text = (BuildingType)Category + "";
     toggle.isOn        = ActionSelecter.actionList[ListIndex][Action];
 }
    //check if action can be dragged
    public bool CanDrag(Action act) {

        if (act == null)
            return false;

        if (act.Do == "paint" || act.Do == "demolish")
            return true;

        else if(act.Do == "place") {

            StructureData data = StructureDatabase.GetData(act.What);
            return data.canDrag;

        }

        return false;

    }
    public int GetPrice(Dictionary <Node, Action> acts, Action a)
    {
        int count = acts.Count;

        int singlePrice = StructureDatabase.GetModifiedPrice(a.What);
        int price       = 0;

        if (count == 0)
        {
            price = singlePrice;
        }

        else
        {
            price = singlePrice * count;
        }

        return(price);
    }
Esempio n. 10
0
    public void Start()
    {
        if (StructureDatabase.HasData(act.What) && desc != null)
        {
            StructureData s = StructureDatabase.GetData(act.What);
            desc.text  = s.DisplayName;
            price.text = StructureDatabase.GetModifiedPrice(act.What) + " shql.";

            Sprite i = Resources.Load <Sprite>("ActionImgs/" + act.What);

            if (i != null)
            {
                img.sprite = i;
            }
            else
            {
                Debug.Log(act.What + " has no image!");
            }
        }
    }
Esempio n. 11
0
    public void PerformActions(Dictionary<Node, Action> acts) {

        undoActions = new Dictionary<Node, Action>();

        foreach (Node n in acts.Keys) {

            int x = n.x;
            int y = n.y;

			//the reason why we use a dictionary instead of referring to currentAction is because this can be called
			//	for undo actions too, which doesn't refer to currentAction
            Action act = acts[n];

            if (CanDo(act, x, y)) {

				//spend money here and not by calling Get___Cost outside of the loop
				//	otherwise we're iterating through the list of nodes twice
				if (modeController.currentMode == Mode.Construct && act.Do == "place")
					moneyController.SpendOnConstruction(StructureDatabase.GetModifiedCost(act.What));

				else if (modeController.currentMode == Mode.Construct && currentAction.Do == "demolish") {
					Structure s = world.Map.GetBuildingAt(n).GetComponent<Structure>();
					moneyController.SpendOnConstruction(s.Sizex * s.Sizey * demolishCost);
				}

				else if (act.Do == "unplace")
					moneyController.SpendOnConstruction(StructureDatabase.GetModifiedCost(act.What) * -1);

				undoActions.Add(n, ReverseAction(act, n));
				Do(act, x, y);

			}

        }

	}
Esempio n. 12
0
    //place a building
    public Structure SpawnStructure(string type, int x, int y, float buildingRotation)
    {
        //don't do if not possible
        if (!CanSpawnStructure(type, x, y, buildingRotation))
        {
            return(null);
        }

        //retrieve data about structure
        StructureData data = StructureDatabase.GetData(type);

        int   sizex  = data.sizex;
        int   sizey  = data.sizey;
        float alignx = data.Alignx;
        float aligny = data.Aligny;

        //if placing a mapentrance or mapexit, remove existing one
        if (data.builtOnce)
        {
            //find object
            GameObject g = GameObject.Find(type);

            //only try to delete it if it exists
            if (g != null)
            {
                Structure m = g.GetComponent <Structure>();
                Demolish(m.X, m.Y);
            }
        }

        //destroy roads beneath area if they're there
        else if (data.hasRoadTiles)
        {
            for (int a = x; a < sizex + x; a++)
            {
                for (int b = y; b < sizey + y; b++)
                {
                    PlaceOnRoads = true;
                    Demolish(x, y);
                }
            }
        }

        //switch size dimensions if building is rotated
        if (buildingRotation % 180 != 0)
        {
            int tempszx = sizex;
            int tempszy = sizey;
            sizex = tempszy;
            sizey = tempszx;

            float tempalignx = alignx;
            float tempaligny = aligny;
            alignx = tempaligny;
            aligny = tempalignx;
        }

        GameObject go = SpawnObject("Structures", type, x, y, buildingRotation);

        go.transform.position += new Vector3(alignx, 0, aligny);

        //unless object is a mapentrance or mapexit, put its coords in its name
        go.name = type + "_" + x + "_" + y;
        if (data.builtOnce)
        {
            go.name = type;
        }

        //rename the area it takes up to its name
        Map.RenameArea(go.name, x, y, sizex, sizey);

        Structure s = go.GetComponent <Structure>();

        if (s == null)
        {
            Debug.LogError(type + " has no structure component");
        }
        s.X           = x;
        s.Y           = y;
        s.Sizex       = sizex;
        s.Sizey       = sizey;
        s.DisplayName = data.displayName;
        s.world       = this;
        s.Activate();

        return(s);
    }
Esempio n. 13
0
    void UpdateDragging(bool line)
    {
        Action        act   = actionController.currentAction;
        StructureData str   = StructureDatabase.GetData(act.What);
        int           sizex = str != null ? str.sizex : 1;
        int           sizey = str != null ? str.sizey : 1;

        // Start Drag
        if (Input.GetMouseButtonDown(0))
        {
            dragStartPosition = mouseCoords;
        }

        int start_x = dragStartPosition.x;
        int end_x   = mouseCoords.x;
        int start_y = dragStartPosition.y;
        int end_y   = mouseCoords.y;

        if (line)
        {
            int dist_x = Math.Abs(start_x - end_x);
            int dist_y = Math.Abs(start_y - end_y);

            if (dist_x > dist_y)
            {
                end_y = start_y;
            }
            else
            {
                end_x = start_x;
            }
        }

        //if buildings cannot fit evenly, reduce the end_x by their modulo
        if ((end_x - start_x) % sizex != 0)
        {
            end_x -= (end_x - start_x) % sizex;
        }

        //if buildings cannot fit evenly, reduce the end_y by their modulo
        if ((end_y - start_y) % sizey != 0)
        {
            end_y -= (end_y - start_y) % sizey;
        }

        // We may be dragging in the "wrong" direction, so flip things if needed.
        if (end_x < start_x)
        {
            int tmp = end_x;
            end_x   = start_x;
            start_x = tmp;
        }
        if (end_y < start_y)
        {
            int tmp = end_y;
            end_y   = start_y;
            start_y = tmp;
        }

        if (Input.GetMouseButton(0))
        {
            Dictionary <Node, Action> locs = new Dictionary <Node, Action>();

            // Display a preview of the drag area
            for (int x = start_x; x <= end_x; x += sizex)
            {
                for (int y = start_y; y <= end_y; y += sizey)
                {
                    locs.Add(new Node(x, y), act);
                }
            }

            actionController.actionLocations = locs;
        }
        else
        {
            DropPreview();
        }

        // Clean up old drag previews
        ClearDragPreviews();

        //New previews
        DragPreview();

        // End Drag
        if (Input.GetMouseButtonUp(0))
        {
            PerformSelectedActions();
        }

        //UpdateTooltip();
    }
Esempio n. 14
0
    void DropPreview()
    {
        //grab preview object from actioncontroller
        preview = actionController.GetPreview(actionController.currentAction);
        Transform prevTrans = preview.transform;

        //if the object is grabbed, move it to the mouse coords
        if (preview != null)
        {
            //set position including elevation
            Vector3 pos = mouseCoords.GetVector3();
            pos.y = worldController.GetObjectFloat((int)pos.x, (int)pos.z);
            prevTrans.position = pos;

            //if action is a strucutre, align it
            Action act = actionController.currentAction;
            if (act.Do == "place")
            {
                StructureData data = StructureDatabase.GetData(act.What);

                int   sizex  = data.sizex;
                int   sizey  = data.sizey;
                float alignx = data.Alignx;
                float aligny = data.Aligny;

                //switch size dimensions if building is rotated
                if (actionController.buildingRotation % 180 != 0)
                {
                    int tempszx = sizex;
                    int tempszy = sizey;
                    sizex = tempszy;
                    sizey = tempszx;

                    float tempalignx = alignx;
                    float tempaligny = aligny;
                    alignx = tempaligny;
                    aligny = tempalignx;
                }

                //BELOW FOR WHEN WE WANT THE CAMERA FACING BUILDINGS
                if (Settings.oldGraphics)
                {
                    if (sizex == sizey && !data.hasRoadTiles && !data.hasWaterTiles)
                    {
                        prevTrans.eulerAngles = cameraController.transform.transform.eulerAngles;
                    }
                    else
                    {
                        prevTrans.eulerAngles = new Vector3(0, actionController.buildingRotation, 0);
                    }
                }
                //FOR NORMAL 3D
                else
                {
                    prevTrans.eulerAngles = new Vector3(0, actionController.buildingRotation, 0);
                }

                prevTrans.position += new Vector3(alignx, 0, aligny);
            }

            Material mat = new Material(canDo);

            //if cannot do, make it red
            if (actionController.CanDo(act, mouseCoords.x, mouseCoords.y))
            {
                mat.color = new Color(1, 1, 1, .5f);
            }
            else
            {
                mat.color = new Color(1, 0, 0, .5f);
            }

            foreach (Transform child in prevTrans)
            {
                Material[] mats = child.GetComponent <MeshRenderer>().materials;
                for (int a = 0; a < mats.Length; a++)
                {
                    mats[a] = mat;
                }
                child.GetComponent <MeshRenderer>().materials = mats;
            }
        }
    }
Esempio n. 15
0
    void AlignMouse()
    {
        //containers for x and y coords
        int x = mouseCoords.x;
        int y = mouseCoords.y;

        //default mouse alignment is 1
        int sizex = 1;
        int sizey = 1;

        //if action is placing a structure, check its size
        Action act = actionController.currentAction;

        if (act == null)
        {
            return;
        }
        if (act.Do == "place")
        {
            StructureData data = StructureDatabase.GetData(act.What);

            sizex = data.sizex;
            sizey = data.sizey;

            //switch size dimensions if building is rotated
            if (actionController.buildingRotation % 180 != 0)
            {
                int tempszx = sizex;
                int tempszy = sizey;
                sizex = tempszy;
                sizey = tempszx;
            }
        }

        Vector3 rotation = cameraController.transform.eulerAngles;

        //ADJUST ALIGNMENT OF BUILDING IF MAPVIEW IS ROTATED
        if (rotation.y == 90 || rotation.y == 180)
        {
            y -= sizey - 1;
        }

        if (rotation.y == 270 || rotation.y == 180)
        {
            x -= sizex - 1;
        }

        //KEEP BUILDING PREVIEW WITHIN WORLD LIMITS
        World world = worldController.Map;

        if (world.OutOfBounds(x, y, sizex, sizey))
        {
            if (x + sizex > world.size.x || x < 0)
            {
                x = prevCoords.x;
            }
            if (y + sizey > world.size.y || y < 0)
            {
                y = prevCoords.y;
            }
        }

        //UPDATE MOUSE COORDS
        mouseCoords = new Node(x, y);

        //UPDATE PREV COORDS
        prevCoords = mouseCoords;
    }
Esempio n. 16
0
    public bool CanSpawnStructure(string type, int x, int y, float buildingRotation)
    {
        if (!StructureDatabase.HasData(type))
        {
            Debug.LogError("Structure databse does not contain " + type);
        }
        StructureData data = StructureDatabase.GetData(type);

        int sizex = data.sizex;
        int sizey = data.sizey;

        //switch size dimensions if building is rotated
        if (buildingRotation % 180 != 0)
        {
            int tempszx = sizex;
            int tempszy = sizey;
            sizex = tempszy;
            sizey = tempszx;
        }

        float elevation = Map.elevation[x, y];

        for (int a = x; a < x + sizex; a++)
        {
            for (int b = y; b < y + sizey; b++)
            {
                //coordinates within array
                int m = a - x;
                int n = b - y;

                if (Map.OutOfBounds(a, b))
                {
                    return(false);
                }

                //if building has road tiles beneath
                else if (data.hasRoadTiles)
                {
                    int[,] roads = ArrayFunctions.RotatedArray(data.roadTiles, buildingRotation);

                    //if there should be a road at this tile
                    if (roads[m, n] == 1)
                    {
                        //if no structure at all, return false
                        if (string.IsNullOrEmpty(Map.structures[a, b]))
                        {
                            return(false);
                        }

                        //else if there's not a road here or it does not contain "Road", return false
                        else if (Map.roads[a, b] != 2 || !Map.GetBuildingNameAt(a, b).Contains("Road_"))
                        {
                            return(false);
                        }
                    }
                }

                //else if there's a structure on this tile when there shouldn't be
                else if (!string.IsNullOrEmpty(Map.structures[a, b]))
                {
                    return(false);
                }

                //else if there's a walker on this tile (make sure that walker grid exists before checking for a list on it)
                else if (WalkerGrid != null)
                {
                    if (WalkerGrid[a, b] != null && !type.Contains("House") && !type.Contains("Rubble"))
                    {
                        return(false);
                    }
                }

                //else if the elevation is different
                if (Map.elevation[a, b] != elevation)
                {
                    return(false);
                }

                if (data.hasWaterTiles)
                {
                    int[,] water = ArrayFunctions.RotatedArray(data.waterTiles, buildingRotation);

                    if (Map.terrain[a, b] != (int)Terrain.Water && water[m, n] == 0 || Map.terrain[a, b] == (int)Terrain.Water && water[m, n] != 0)
                    {
                        return(false);
                    }
                }

                //else return false if there's water here and there shouldn't be
                else if (Map.terrain[a, b] == (int)Terrain.Water)
                {
                    return(false);
                }
            }
        }

        if (data.buildNearWater)
        {
            if (!Map.IsNearWater(x, y, sizex, sizex))
            {
                return(false);
            }
        }

        if (!string.IsNullOrEmpty(data.placeOnTerrain))
        {
            if (!Map.IsOnTile(x, y, sizex, sizey, Enums.terrainDict[data.placeOnTerrain]))
            {
                return(false);
            }
        }

        if (!string.IsNullOrEmpty(data.placeNearby))
        {
            if (!Map.IsNearStructure(x, y, sizex, sizey, data.placeNearby))
            {
                return(false);
            }
        }

        return(true);
    }
Esempio n. 17
0
    public override void UpdateDragging()
    {
        Action        act   = actionController.currentAction;
        StructureData str   = StructureDatabase.GetData(act.What);
        int           sizex = str != null ? str.Sizex : 1;
        int           sizey = str != null ? str.Sizey : 1;

        // Start Drag
        if (Input.GetMouseButtonDown(0))
        {
            dragStartPosition = touchCoords;
        }

        int start_x = Mathf.FloorToInt(dragStartPosition.x);
        int end_x   = Mathf.FloorToInt(touchCoords.x);
        int start_y = Mathf.FloorToInt(dragStartPosition.z);
        int end_y   = Mathf.FloorToInt(touchCoords.z);

        //if buildings cannot fit evenly, reduce the end_x by their modulo
        if ((end_x - start_x) % sizex != 0)
        {
            end_x -= (end_x - start_x) % sizex;
        }

        //if buildings cannot fit evenly, reduce the end_y by their modulo
        if ((end_y - start_y) % sizey != 0)
        {
            end_y -= (end_y - start_y) % sizey;
        }

        // We may be dragging in the "wrong" direction, so flip things if needed.
        if (end_x < start_x)
        {
            int tmp = end_x;
            end_x   = start_x;
            start_x = tmp;
        }
        if (end_y < start_y)
        {
            int tmp = end_y;
            end_y   = start_y;
            start_y = tmp;
        }

        //if clicking down, add every potential building spot
        if (Input.GetMouseButton(0))
        {
            Dictionary <Node, Action> locs = new Dictionary <Node, Action>();

            // Display a preview of the drag area
            for (int x = start_x; x <= end_x; x++)
            {
                for (int y = start_y; y <= end_y; y++)
                {
                    if (actionController.CanDo(act, x, y))
                    {
                        locs.Add(new Node(x, y), act);
                    }
                }
            }

            actionController.actionLocations = locs;
        }

        //otherwise just show preview where the mouse is right now
        else
        {
            DropPreview();
        }

        // Clean up old drag previews
        ClearDragPreviews();

        //New drag previews
        DragPreview();

        // End Drag
        if (Input.GetMouseButtonUp(0))
        {
            PerformSelectedActions();
        }
    }