/**
     * Create an Obstacle in the scene (generally used on first load).
     */
    virtual protected void CreateObstacle(string buildingTypeId, GridPosition pos)
    {
        if (!types.ContainsKey(buildingTypeId))
        {
            Debug.LogWarning("Tried to create an obstacle with an invalid ID");
            return;
        }
        BuildingTypeData type = types[buildingTypeId];

        if (!type.isObstacle)
        {
            Debug.LogError("Tried to create an obstacle with non-obstacle building data");
            return;
        }
        GameObject go = (GameObject)Instantiate(buildingPrefab);

        go.transform.parent = gameView.transform;
        Building obstacle = go.GetComponent <Building>();

        obstacle.Init(type, pos);
        BuildingModeGrid.GetInstance().AddObjectAtPosition(obstacle, pos);
        buildings.Add(obstacle.uid, obstacle);
        obstacle.Acknowledge();
        if ((int)saveMode < (int)SaveMode.SAVE_MOSTLY)
        {
            PersistenceManager.GetInstance().Save();
        }
    }
Example #2
0
    public float GetHeight(TileData t, bool random)
    {
        TerrainTypeData  tD = _TileTypeDataManager.GetTerrainData(t.Terrain);
        BuildingTypeData bD = _TileTypeDataManager.GetBuildingData(t.Building);
        Vector2          range;

        if (bD.SetHeight)
        {
            range = bD.Height;
        }
        else if (tD.SetHeight)
        {
            range = tD.Height;
        }
        else if (random)
        {
            range = new Vector2();
        }
        else
        {
            return(t.Height);
        }

        return(Random.Range(range.x, range.y));
    }
    private void AddBuildingPanel(BuildingTypeData type)
    {
        GameObject            panelGo = (GameObject)NGUITools.AddChild(buildingScrollPanel, buildingPanelPrefab);
        UIBuildingSelectPanel panel   = panelGo.GetComponent <UIBuildingSelectPanel>();

        panel.InitialiseWithBuildingType(type);
        buildingSelectPanels.Add(panel);
    }
 /**
  * Return true if there are enough resources to build the given building.
  */
 public bool CanBuild(BuildingTypeData building)
 {
     if (Resources >= building.cost)
     {
         return(true);
     }
     return(false);
 }
Example #5
0
 /**
  * Set up the building with the given type data.
  */
 virtual public void InitialiseWithBuildingType(BuildingTypeData type)
 {
     this.type             = type;
     titleLabel.text       = type.name;
     descriptionLabel.text = type.description;
     costLabel.text        = type.cost.ToString();
     sprite.spriteName     = type.spriteName;
     UpdateBuildingStatus();
 }
	/**
	 * Initialise the building with the given type and position.
	 */ 
	override public void Init(BuildingTypeData type, GridPosition pos){
		data = new UpgradableBuildingData();
		((UpgradableBuildingData)data).level = 1;
		uid = System.Guid.NewGuid ().ToString ();
		Position = pos;
		this.Type = type;
		State = BuildingState.PLACING;
		CurrentActivity = null;
		CompletedActivity = null;
		view = gameObject;
		view.SendMessage ("UI_Init", this);
		view.SendMessage("SetPosition", data.position);
	}
 /**
  * Initialise the building with the given type and position.
  */
 virtual public void Init(BuildingTypeData type, GridPosition pos)
 {
     data              = new BuildingData();
     uid               = System.Guid.NewGuid().ToString();
     Position          = pos;
     this.Type         = type;
     State             = BuildingState.PLACING;
     CurrentActivity   = null;
     CompletedActivity = null;
     view              = gameObject;
     view.SendMessage("UI_Init", this);
     view.SendMessage("SetPosition", data.position);
 }
Example #8
0
        public List <Action> BuildGas(MacroData macroData, BuildingTypeData unitData, Unit geyser)
        {
            if (unitData.Minerals <= macroData.Minerals && unitData.Gas <= macroData.VespeneGas)
            {
                var worker = GetWorker(new Point2D {
                    X = geyser.Pos.X, Y = geyser.Pos.Y
                });
                if (worker != null)
                {
                    worker.UnitRole = UnitRole.Build;
                    return(worker.Order(macroData.Frame, unitData.Ability, null, geyser.Tag));
                }
            }

            return(null);
        }
	/**
	 * Set up the building with the given type data.
     */
	virtual public void InitialiseWithBuildingType(BuildingTypeData type) {
		this.type = type;
		titleLabel.text = type.name;
		descriptionLabel.text = type.description;
		costLabel.text = type.cost.ToString();
		sprite.spriteName = type.spriteName;
		if (type.additionalCosts != null && type.additionalCosts.Count > 0) {
			extraCostLabel.gameObject.SetActive(true);
			extraCostSprite.gameObject.SetActive(true);
			extraCostLabel.text = "" + type.additionalCosts[0].amount;
			extraCostSprite.spriteName = ResourceManager.Instance.GetCustomResourceType(type.additionalCosts[0].id).spriteName;
		} else {
			extraCostLabel.gameObject.SetActive(false);
			extraCostSprite.gameObject.SetActive(false);
		}
		UpdateBuildingStatus ();
	}
Example #10
0
        public List <Action> BuildBuilding(MacroData macroData, UnitTypes unitType, BuildingTypeData unitData, Point2D generalLocation = null, bool ignoreMineralProximity = false, float maxDistance = 50, List <UnitCommander> workerPool = null, bool requireSameHeight = false, WallOffType wallOffType = WallOffType.None)
        {
            if (unitData.Minerals <= macroData.Minerals && unitData.Gas <= macroData.VespeneGas)
            {
                bool anyBase  = false;
                var  location = generalLocation;
                if (location == null)
                {
                    anyBase  = true;
                    location = GetReferenceLocation(TargetingData.SelfMainBasePoint);
                }
                var placementLocation = BuildingPlacement.FindPlacement(location, unitType, unitData.Size, ignoreMineralProximity, maxDistance, requireSameHeight, wallOffType);

                if (placementLocation == null)
                {
                    placementLocation = BuildingPlacement.FindPlacement(location, unitType, unitData.Size, true, maxDistance, requireSameHeight, wallOffType);
                }
                if (placementLocation == null && anyBase)
                {
                    foreach (var selfBase in BaseData.SelfBases)
                    {
                        placementLocation = BuildingPlacement.FindPlacement(selfBase.Location, unitType, unitData.Size, true, maxDistance, requireSameHeight, wallOffType);
                        if (placementLocation != null)
                        {
                            break;
                        }
                    }
                }
                if (placementLocation != null)
                {
                    var worker = GetWorker(placementLocation, workerPool);
                    if (worker != null)
                    {
                        if (workerPool == null)
                        {
                            worker.UnitRole = UnitRole.Build;
                        }

                        return(worker.Order(macroData.Frame, unitData.Ability, placementLocation));
                    }
                }
            }

            return(null);
        }
Example #11
0
 public void Initialise()
 {
     _terrainData  = new Dictionary <TerrainType, TerrainTypeData>();
     _buildingData = new Dictionary <BuildingType, BuildingTypeData>();
     foreach (TileTypeDataBase data in BaseData)
     {
         if (data.Tile == TileType.Terrain)
         {
             TerrainTypeData tData = (TerrainTypeData)data;
             _terrainData.Add(tData.Type, tData);
         }
         else
         {
             BuildingTypeData bData = (BuildingTypeData)data;
             _buildingData.Add(bData.Type, bData);
         }
     }
 }
Example #12
0
    /**
     * Formats the allows/required identifiers to be nice strings, coloured correctly.
     * Returns the identifiers.
     */
    virtual protected string FormatIds(List <string> allowIds, bool redIfNotPresent)
    {
        BuildingManager manager = BuildingManager.GetInstance();
        string          result  = "";

        foreach (string id in allowIds)
        {
            if (redIfNotPresent && !manager.PlayerHasBuilding(id) && !OccupantManager.GetInstance().PlayerHasOccupant(id))
            {
                result += "[ff0000]";
            }
            else
            {
                result += "[000000]";
            }
            BuildingTypeData type  = manager.GetBuildingTypeData(id);
            OccupantTypeData otype = OccupantManager.GetInstance().GetOccupantTypeData(id);
            if (type != null)
            {
                result += type.name + ", ";
            }
            else if (otype != null)
            {
                result += otype.name + ", ";
            }
            else
            {
                Debug.LogWarning("No building or occupant type data found for id:" + id);
                result += id + ", ";
            }
        }
        if (result.Length > 2)
        {
            result = result.Substring(0, result.Length - 2);
        }
        else
        {
            return("Nothing");
        }
        return(result);
    }
    /**
     * Formats the allows/required identifiers to be nice strings, coloured correctly.
     * Returns the identifiers.
     */
    private string FormatIds(List <string> allowIds, bool redIfNotPresent)
    {
        BuildingManager manager = BuildingManager.GetInstance();
        string          result  = "";

        foreach (string id in allowIds)
        {
            if (redIfNotPresent && !manager.PlayerHasBuilding(id))
            {
                result += "[ff0000]";
            }
            else
            {
                result += "[000000]";
            }
            BuildingTypeData type = manager.GetBuildingTypeData(id);
            if (type != null)
            {
                result += manager.GetBuildingTypeData(id).name + ", ";
            }
            else
            {
                Debug.LogWarning("No building type data found for id:" + id);
                result += id + ", ";
            }
        }
        if (result.Length > 2)
        {
            result = result.Substring(0, result.Length - 2);
        }
        else
        {
            return("Nothing");
        }
        return(result);
    }
	private void AddBuildingPanel(BuildingTypeData type) {
		GameObject panelGo = (GameObject) NGUITools.AddChild(buildingScrollPanel, buildingPanelPrefab);
		UIBuildingSelectPanel panel = panelGo.GetComponent<UIBuildingSelectPanel>();
		panel.InitialiseWithBuildingType(type);
		buildingSelectPanels.Add (panel);
	}
	/**
	 * Create a building from data. Uses a coroutine to ensure view can be synced with data.
	 */ 
	protected virtual IEnumerator DoInit(BuildingTypeData type, BuildingData data) {
		this.data = data;
		this.type = type;
		this.Position = data.position;
		// Ensure occupant type references are loaded
		if (data.occupants != null) {
			foreach (OccupantData o in data.occupants) {
				o.Type = OccupantManager.GetInstance().GetOccupantTypeData(o.occupantTypeString);	
				OccupantManager.GetInstance().RecruitedOccupant(o);
			}
		}
		// Update view
		view = gameObject;
		view.SendMessage ("UI_Init", this);
		view.SendMessage ("SetPosition", data.position);
		view.SendMessage ("UI_UpdateState");
		
		// Wait one frame to ensure everything is initialised
		yield return true;
		if (data.state == BuildingState.IN_PROGRESS || data.state == BuildingState.READY) {
			StartCoroutine(BuildActivity(data.startTime));
		} else {
			// Activities
			if (data.completedActivity != null && data.completedActivity.Type != ActivityType.BUILD && data.completedActivity.Type != ActivityType.NONE) {
				StartCoroutine(GenericActivity(data.completedActivity.Type, data.completedActivity.StartTime, data.completedActivity.SupportingId));
			} else if (data.currentActivity != null && data.currentActivity.Type != ActivityType.BUILD && data.currentActivity.Type != ActivityType.NONE) {
				StartCoroutine(GenericActivity(data.currentActivity.Type, data.currentActivity.StartTime, data.currentActivity.SupportingId));
			}
			// Auto activities
			if (!Type.isObstacle) {
				if (data.autoActivity != null) {
					System.TimeSpan span = System.DateTime.Now - data.autoActivity.StartTime;
					int iterations = ((int)span.TotalSeconds) / type.generationTime;
					int generated = iterations * type.generationAmount;
					StoredResources = StoredResources + generated;
					if (StoredResources > type.generationStorage) StoredResources = type.generationStorage;
					// If storage not full resume activity
					if (type.generationAmount > 0 && StoredResources < type.generationStorage) {
						System.DateTime newStartTime = data.autoActivity.StartTime + new System.TimeSpan(0, 0, iterations * type.generationTime);
						StartAutomaticActivity(newStartTime);
					} else {
						AutoActivity = null;	
					}
				} else if (type.generationAmount > 0 && StoredResources < type.generationStorage) {
					// Start auto activty if one not saved
					StartAutomaticActivity(System.DateTime.Now);
				}
				// If more than 50% full and no other activity to show then show a store collect indicator
				if (type.generationAmount > 0 && CompletedActivity == null && CurrentActivity == null && StoredResources >= type.generationStorage * 0.5f) {
					view.SendMessage ("UI_StoreFull");
				}		
			}
		}
	}
	/**
	 * Initialise the building with the given data
	 */ 
	virtual public void Init(BuildingTypeData type, BuildingData data){
		StartCoroutine(DoInit (type, data));
	}
    /**
     * Create a building from data. Uses a coroutine to ensure view can be synced with data.
     */
    protected virtual IEnumerator DoInit(BuildingTypeData type, BuildingData data)
    {
        this.data     = data;
        this.type     = type;
        this.Position = data.position;
        // Ensure occupant type references are loaded
        if (data.occupants != null)
        {
            foreach (OccupantData o in data.occupants)
            {
                o.Type = OccupantManager.GetInstance().GetOccupantTypeData(o.occupantTypeString);
                OccupantManager.GetInstance().RecruitedOccupant(o);
            }
        }
        // Update view
        view = gameObject;
        view.SendMessage("UI_Init", this);
        view.SendMessage("SetPosition", data.position);
        view.SendMessage("UI_UpdateState");

        // Wait one frame to ensure everything is initialised
        yield return(true);

        if (data.state == BuildingState.IN_PROGRESS || data.state == BuildingState.READY)
        {
            StartCoroutine(BuildActivity(data.startTime));
        }
        else
        {
            // Activities
            if (data.completedActivity != null && data.completedActivity.Type != ActivityType.BUILD && data.completedActivity.Type != ActivityType.NONE)
            {
                StartCoroutine(GenericActivity(data.completedActivity.Type, data.completedActivity.StartTime, data.completedActivity.SupportingId));
            }
            else if (data.currentActivity != null && data.currentActivity.Type != ActivityType.BUILD && data.currentActivity.Type != ActivityType.NONE)
            {
                StartCoroutine(GenericActivity(data.currentActivity.Type, data.currentActivity.StartTime, data.currentActivity.SupportingId));
            }
            // Auto activities
            if (!Type.isObstacle)
            {
                if (data.autoActivity != null)
                {
                    System.TimeSpan span       = System.DateTime.Now - data.autoActivity.StartTime;
                    int             iterations = ((int)span.TotalSeconds) / type.generationTime;
                    int             generated  = iterations * type.generationAmount;
                    StoredResources = StoredResources + generated;
                    if (StoredResources > type.generationStorage)
                    {
                        StoredResources = type.generationStorage;
                    }
                    // If storage not full resume activity
                    if (type.generationAmount > 0 && StoredResources < type.generationStorage)
                    {
                        System.DateTime newStartTime = data.autoActivity.StartTime + new System.TimeSpan(0, 0, iterations * type.generationTime);
                        StartAutomaticActivity(newStartTime);
                    }
                    else
                    {
                        AutoActivity = null;
                    }
                }
                else if (type.generationAmount > 0 && StoredResources < type.generationStorage)
                {
                    // Start auto activty if one not saved
                    StartAutomaticActivity(System.DateTime.Now);
                }
                // If more than 50% full and no other activity to show then show a store collect indicator
                if (type.generationAmount > 0 && CompletedActivity == null && CurrentActivity == null && StoredResources >= type.generationStorage * 0.5f)
                {
                    view.SendMessage("UI_StoreFull");
                }
            }
        }
    }
 /**
  * Initialise the building with the given data
  */
 virtual public void Init(BuildingTypeData type, BuildingData data)
 {
     StartCoroutine(DoInit(type, data));
 }
	/**
	 * Return true if there are enough resources to build the given building.
	 */ 
	public bool CanBuild(BuildingTypeData building) {
		if (Resources >= building.cost) 
		{
			if (building.additionalCosts != null) {
				foreach (CustomResource c in building.additionalCosts) {
					if (GetCustomResource(c.id) < c.amount) {
						Debug.Log ("Not enough " + c.id);
						return false;
					}
				}
			}
			return true;
		}
		return false;
	}