public bool TryPlaceObject(GridObject gridObject, Vector3 worldPosition, bool addToGrid, bool moveOnPlace) { Vector2 cellPos = WorldPointToCell(worldPosition); gridObject.cellPosition = cellPos; Vector2[] objGridCells = new Vector2[gridObject.cells.Count]; for (int i = 0; i < gridObject.cells.Count; i++) { objGridCells[i] = gridObject.cells[i] + cellPos; if (objectsIn.ContainsKey(objGridCells[i])) { if (moveOnPlace) { gridObject.transform.position = WorldPointToWorldCellPoint(worldPosition) - gridObject.basePosition; } return(false); } } if (addToGrid) { foreach (var cell in objGridCells) { objectsIn.Add(cell, gridObject.gameObject); } gridObject.gridMap = this; gridObject.gameObject.layer = LayerMask.NameToLayer("Default"); } if (moveOnPlace) { gridObject.transform.position = WorldPointToWorldCellPoint(worldPosition) - gridObject.basePosition; } return(true); }
private void Update() { if (Input.touchSupported) { if (Input.touchCount > 0 && _currentInputDelay <= 0) { var touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { var obj = GetSelectableObject(touch); if (obj != null) { obj.Select(); } _selectedObj = obj; } else if (touch.phase == TouchPhase.Moved) { if (_selectedObj) { MoveSelectedObject(touch); } } _currentInputDelay = inputDelay; } _currentInputDelay -= Time.deltaTime; } }
internal Tile GetTileInDirection(GridObject obj, Vector2 direction) { //current Tile t = _tiles[obj.x, obj.y]; //horizontal if (direction.y == 0) { if (direction.x > 0) { t = _tiles[width - 1, t.y]; } else { t = _tiles[0, t.y]; } } //vertical else { if (direction.y > 0) { t = _tiles[t.x, height - 1]; } else { t = _tiles[t.x, 0]; } } return(t); }
// Use this for initialization void Start() { goal = GameObject.FindGameObjectWithTag("Goal").GetComponent <GridObject>(); if (goal == null) { Debug.LogError("Goal is either missing or not properly tagged."); } DeathManager.PlayerKilled += ResetPositions; if (bringInCurrentPositions) { possibleSpots[0] = (Vector2)goal.GetPosition(); possibleSpots[1] = (Vector2)blueGoal.GetPosition(); if (possibleSpots.Length > 2) { possibleSpots[2] = (Vector2)redGoal.GetPosition(); } } if (possibleSpots.Length >= 1) { MovePositions(); } }
// Update is called once per frame void GetGrid() { grid = gridCombatSystem.GetGrid(); lastActiveCell = grid.GetGridObject(0, 0); lastPlayerCell = grid.GetGridObject(player.transform.position); rangeCombatPlayer = player.rangeCombat; }
/// <summary> /// Creates a line for a tile. /// </summary> private string TileToString(Point position, GridObject obj) { string result; string typeName = string.Empty; switch (obj.Type) { case GridObjectType.Tile: typeName = "tile"; break; case GridObjectType.Ghost: typeName = "ghost"; break; case GridObjectType.Patrol: typeName = "patrol"; break; case GridObjectType.GravityBoots: typeName = "gravityboots"; break; case GridObjectType.JumpPlatform: typeName = "jumpplatform"; break; } result = string.Format("{0},{1}={2}", position.X, position.Y, typeName); if (obj.Type == GridObjectType.Patrol) { result += "\r\n"; // Add a newline. result += "100"; // Add range for patrol. } return result; }
private IEnumerator DropLootCoroutine(GridObject gridObject, Vector3 dropLocation, float delayAppear = 1.0f) { LootHandler lh = gridObject.gameObject.GetComponent <LootHandler>(); if (lh != null) { GameObject lootObjectToDrop = lh.RequestLootDrop(dropLocation, forced: true); if (lootObjectToDrop != null) { MeshRenderer renderer = lootObjectToDrop.GetComponentInChildren <MeshRenderer>(); renderer.enabled = false; gm.AddObjectToGrid(lootObjectToDrop, gm.WorldToGrid(dropLocation)); gridObjectsInPlay.Add(lootObjectToDrop.GetComponent <Loot>()); Rotator lootRotator = lootObjectToDrop.GetComponent <Rotator>(); lootRotator.enabled = true; lootRotator.ApplyRotation("Left"); yield return(new WaitForSeconds(delayAppear)); renderer.enabled = true; } } }
// Use this for initialization void Start() { GridObject gridObject = GetComponent <GridObject>(); Color setColor; if (randomColor) { setColor = new Color(Random.Range(0.2f, 1.0f), Random.Range(0.2f, 1.0f), Random.Range(0.2f, 1.0f)); } else { setColor = Color.white; } for (int i = 0; i < gridObject.height; i++) { for (int j = 0; j < gridObject.width; j++) { GameObject gameObject = Instantiate(spriteObject); gameObject.transform.SetParent(this.transform); gameObject.transform.localScale = new Vector3(1.0f / gridObject.width, 1.0f / gridObject.height, 1); gameObject.transform.position = gridObject.transform.position + new Vector3(j + 0.5f - (float)gridObject.width / 2, i + 0.5f - (float)gridObject.height / 2, 0); gameObject.GetComponent <SpriteRenderer>().color = setColor; } } }
public bool CanBuildGridObject(GridObject grid_object) { if (grid_object is AreaBuildingFactory || grid_object is AreaBuilding) { if (areaBuilding == null) return true; else return false; } else if (grid_object is Building) { if (areaBuilding != null) { Building compare_building = (Building) grid_object; if (compare_building.AreaBuildingTypeMatch(areaBuilding.GetBuildingType()) && building == null) { return true; } //Debug.Log("fail 2"); } //Debug.Log("fail 3"); return false; } else if (grid_object is Road) { if (areaBuilding == null && gridObject == null) { return true; } else { return false; } } else if (grid_object is GridObject) { if (areaBuilding == null && gridObject == null) { return true; } else { return false; } } return false; }
protected virtual void Start() { if (m_Attack) { _gridObject = m_Attack.m_GridObject; } }
public void Move(GridObject tile) { x = tile.x; y = tile.y; OnMoved(tile); }
public void Export() { var path = Application.streamingAssetsPath + "/Grid_Answer.json"; string str = "no parsng happened"; GridObject export = new GridObject(); export.Grid = new List <int[]>(); foreach (var item in boxes) { export.Grid.Add(item.selectedOptions); } str = JsonConvert.SerializeObject(export.Grid); using (StreamWriter file = File.CreateText(path)) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(file, export.Grid); } //using (FileStream fs = new FileStream(path, FileMode.Create)) //{ // using (StreamWriter writer = new StreamWriter(fs)) // { // writer.Write(str); // } //} Instruction.text = " file is stored in streamingAsset folder."; #if UNITY_EDITOR UnityEditor.AssetDatabase.Refresh(); #endif }
public void OnHealthChanged(GridObject attacker, int healthChangedAmount) { if (onHealthChangedEvent != null) { onHealthChangedEvent(this, attacker, healthChangedAmount); } }
public void OnDeath(GridObject attacker) { if (onDeathEvent != null) { onDeathEvent(this, attacker); } }
public void Interact(Vector3 position, params GameObject[] objectToPlace) { if (objectToPlace.Length == 0 || objectToPlace[0] == null || objectToPlace[0].GetComponent <GridObject>() == null) { return; } GridObject gridObject = objectToPlace[0].GetComponent <GridObject>(); if (objectToPlace.Length > 1) // Mouse holding { var isPossible = TryPlaceObject(gridObject, position + gridObject.basePosition, false, true); gridObject.renderer.material = isPossible ? possible : impossible; } else // Mouse released { gridObject.renderer.material = gridObject.originalMat; if (!TryPlaceObject(gridObject, position + gridObject.basePosition, true, true)) { Destroy(gridObject.gameObject); } else { userInteractor.selectedGridObject = null; } userInteractor.placeObject = null; } }
private void Jump() { GridPos targetPos = this.selfGridObject.pos + this.directions[(int)this.facing]; GridObject targetGridObject = Root.instance.grid.GetObjectAt(this.selfGridObject.pos + this.directions[(int)this.facing]); if (targetGridObject == null) { return; } Tower targetTower = targetGridObject.GetComponent <Tower>(); MovementRules.Modalities modality; if (this.movementRules.IsValidMove(this.selfGridObject.pos, targetPos, out modality)) { targetTower.RegisterCharacter(this); this.tower.DeregisterCharacter(); this.tower = targetTower; //FIXME! int newHeight = targetTower.gridObject.GetHeight(); if (modality == MovementRules.Modalities.JUMP_UP_HACK) { newHeight = 3; } this.selfGridObject.SetHeight(newHeight); switch (modality) { case MovementRules.Modalities.JUMP_UP: JumpUp(targetTower); break; case MovementRules.Modalities.JUMP_DOWN: JumpDown(targetTower); break; case MovementRules.Modalities.HOP: Hop(targetTower); break; case MovementRules.Modalities.JUMP_UP_HACK: JumpUp(targetTower, true); break; } } else { switch (modality) { case MovementRules.Modalities.BLOCKED: DoBump(); break; case MovementRules.Modalities.FALL: DoFear(); break; } } }
public void SelectTower(GameObject obj) { if (obj.GetComponent <GridObject>()) { selectedGridObject = obj.GetComponent <GridObject>(); } }
private bool SFilter(GridObject targetObject) { Vector2 selfPos = character.GetComponent <GridObject>().pos; Vector2 pos = (targetObject.pos - selfPos); if (Mathf.Abs(pos.x) <= MaxRange && Mathf.Abs(pos.y) <= MaxRange && Mathf.Abs(pos.x) >= MinRange && Mathf.Abs(pos.y) >= MinRange) { return(false); } foreach (Vector2Int extPos in targetObject.extra_pos) { pos = (extPos - selfPos); if (Mathf.Abs(pos.x) <= MaxRange && Mathf.Abs(pos.y) <= MaxRange && Mathf.Abs(pos.x) >= MinRange && Mathf.Abs(pos.y) >= MinRange) { return(false); } } return(true); }
protected virtual void HitObstacle(GridObject obstacle) { //Obstacle takes damage switch (obstacle.GetObjectType()) { case ObjectType.Unit: { FaceTarget(gridSpace.transform.position, 0.25f); break; } } obstacle.TakeDamage(KnockbackDamage); //Invoke On Obstacle Hit OnObstacleHit?.Invoke(this); //If the object hasn't died then deal damage to itself if (gameObject.activeSelf) { switch (GetObjectType()) { case ObjectType.Unit: { FaceTarget(obstacle.transform.position, 0.25f); break; } } TakeDamage(KnockbackDamage); } }
private void TryCommit() { CellCoordinates _end = m_passedThrough.Peek(); GridObject end = m_gridManager.GetCell(_end); if (end == null) { Log("WireManager: Commit attempted on an empty cell " + _end.ToString() + ", ending mode."); ResetMode(); return; } if (m_passedThrough.Count < 3) { Log("WireManager: Commit attempted with too few cells passed through."); ResetMode(); return; } // Generate wire objects List <CellCoordinates> coordinates = new List <CellCoordinates>(m_passedThrough); coordinates.Reverse(); // Probably? Wire toCreate = new Wire(coordinates.GetRange(1, coordinates.Count - 2).ToArray(), coordinates[0], coordinates[coordinates.Count - 1]); // Insert wire into grid m_gridManager.InsertObject(toCreate); m_gameplayManager.UpdateGiblets(toCreate.Entry); m_gameplayManager.UpdateGiblets(toCreate.Exit); Log("WireManager: Successfully committed at " + _end.ToString()); ResetMode(); }
public void RemoveGridObject(GridObject gridObject) { var message = new BattleSceneRemoveGridObjectMessage(); message.gridObj = StreamableFactory.CreateBattleSceneObject(gridObject); _connection.SendMessage(message); }
private void OnClick(int x, int y, System.Windows.Forms.MouseButtons button) { if (button == System.Windows.Forms.MouseButtons.Left) { Point p = GraphicsEngine.TranslateToGrid(x, y); GridObject go = _grid[p]; if (go == null) { if (InputHandler.InputType != null) { go = GridObject.InstantiateObject(InputHandler.InputType, p.X, p.Y, 1, 1, InputHandler.SelectedRotation); if (go != null) { _grid.AddObject(go); } } } else { IClickableObject clicakble = go as IClickableObject; if (clicakble == null) { return; } clicakble.OnClick(button); } } }
// manage selected grid objects public void AddSelected(GridObject newlySelected) { if (selectedGridObject == null) { audio.PlayOneShot(selectSound); selectedGridObject = newlySelected; } else { if ((grid.AdjacentGridObjects(newlySelected).Contains(selectedGridObject))) { grid.Switch(selectedGridObject, newlySelected); switchingObjects.Add(selectedGridObject); switchingObjects.Add(newlySelected); newlySelected.BeNotSelected(); selectedGridObject.BeNotSelected(); selectedGridObject = null; } else { audio.PlayOneShot(selectSound); selectedGridObject.BeNotSelected(); selectedGridObject = newlySelected; } } }
public GridObject CreateBuilding(string templateID) { GridObject template = TemplateManager.Instance.Get <PurchasableObject>(templateID); if (template != null) { PurchasableObject buildingData = (PurchasableObject)template; if (buildingData != null) { if (HasResource(buildingData.BuildingCost.ResourceCost.Type, buildingData.BuildingCost.ResourceCost.Amount)) { GridObject gridObject = TemplateManager.Instance.Spawn <GridObject>(template); gridObject.UID = GetNewUID(gridObject); return(gridObject); } ShowMessage("Not enough Cash!"); } else { Debug.LogError("Can not create building '" + templateID + "' because it is not of type 'BaseBuilding'."); } } return(null); }
private Recipe GetRecipeWithInputs(GridObject dragged, GridObject target) { List <Recipe> validRecipes = new List <Recipe>(); foreach (Recipe recipe in Recipes) { bool aFound = false; bool bFound = false; foreach (RecipeInput input in recipe.Inputs) { if (input.Object == dragged.Data && !aFound) { aFound = true; } else if (input.Object == target.Data && input.CanBeTargetObject) { bFound = true; } } if (aFound && bFound) { return(recipe); } } return(null); }
public GameObject FindNearestSafeZone(Vector3 position) { GameObject best = null; GameObject closest = null; float dist = float.MaxValue; float minDist = float.MaxValue; for (int i = 0; i < _safeZones.Length; i++) { float newDist = (_safeZones[i].transform.position - position).magnitude; if (newDist < dist) { GridObject exit = _safeZones[i].GetComponent <GridObject>(); if (newDist < minDist) { minDist = dist; closest = _safeZones[i]; } if (!exit.IsFlooded) { best = _safeZones[i]; dist = newDist; } } } if (best == null) { best = closest; } return(best); }
public IEnumerator Move(TileHolder tile, float jumpPower, float duration) { TileHolder = tile; yield return(transform.DOJump(GetPlacementPosition(tile), jumpPower, 1, duration).WaitForCompletion()); GridObject.Move(tile.Tile); }
public static Vector2Int BlockAtMouse() { Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue()); RaycastHit hit; if (Physics.Raycast(ray, out hit, 100)) { GridObject blockHit = hit.transform.gameObject.GetComponent <GridObject>(); if (blockHit != null) { if (blockHit.pos.x >= mapShape.x) { blockHit.pos.x = mapShape.x - 1; } if (blockHit.pos.x < 0) { blockHit.pos.x = 0; } if (blockHit.pos.y >= mapShape.y) { blockHit.pos.y = mapShape.y - 1; } if (blockHit.pos.y < 0) { blockHit.pos.y = 0; } return(blockHit.pos); } } return(new Vector2Int(0, 0)); }
void Start() { state = State.InGame; movableList = new List <GridObject>(); steppingList = new List <StepController>(); gridTable = new Dictionary <Vector3, GridObject>(); foreach (MovableController obj in FindObjectsOfType <MovableController>()) { if (obj is PlayerContoller) { playerObject = new GridObject(player, player.transform.position); } else if (obj is SecretController) { secretObject = new GridObject(secret, secret.transform.position); } else { movableList.Add(new GridObject(obj, obj.transform.position)); gridTable.Add(obj.transform.position, new GridObject(obj, obj.transform.position)); } } gridTable.Add(player.transform.position, playerObject); gridTable.Add(secret.transform.position, secretObject); }
// Callback to invoke the OnEnterTile events when a gridobject with the target layer exits the same gridtile as this protected virtual void OnGridObjectExitedTileCallBack(GridObject gridObject, GridTile gridTile) { if (0 != (m_Layers.value & 1 << gridObject.gameObject.layer)) { OnExitTileMethod(gridObject, gridTile); } }
public void ForcePlaceObject(GridObject gridObject, Vector3 worldPosition, bool addToGrid, bool moveOnPlace) { Vector2 cellPos = WorldPointToCell(worldPosition); gridObject.cellPosition = cellPos; List <Vector2> objGridCells = new List <Vector2>(); for (int i = 0; i < gridObject.cells.Count; i++) { if (objectsIn.ContainsKey(gridObject.cells[i] + cellPos)) { continue; } objGridCells.Add(gridObject.cells[i] + cellPos); } if (addToGrid) { foreach (var cell in objGridCells) { objectsIn.Add(cell, gridObject.gameObject); } } if (moveOnPlace) { gridObject.transform.position = WorldPointToWorldCellPoint(worldPosition) - gridObject.basePosition; } }
private GridObject GetSelectableObject(Touch touch) { var touchPosRay = _mainCam.ScreenPointToRay(touch.position); bool hasHit = Physics.Raycast(touchPosRay, out var hit, selectableLayer); if (hasHit) { if (_selectedObj != null) { if (hit.transform.CompareTag("LeftRotator")) { _selectedObj.RotateLeft(); return(_selectedObj); } if (hit.transform.CompareTag("RightRotator")) { _selectedObj.RotateRight(); return(_selectedObj); } _selectedObj.Deselect(); _selectedObj = null; } var moveObj = hit.transform.parent.GetComponent <GridObject>(); if (moveObj.isSelectable) { return(moveObj); } } return(null); }
/* * Get all the possible new snap positions */ public List<Vector3> GetSnapSpots(Vector3 mousePos) { List<Vector3> returnList = new List<Vector3>(); // first get closest snapToObject, because we just have a type GridObject foundSnapToObject = BuildingList.GetClosestBuildingOfType(snapToObjectType, mousePos); // if there are no buildings of that type, return null; if (foundSnapToObject == null) { return returnList; } // if the object closest is same we found before, return saved list else if (foundSnapToObject == snapToObject) { return savedSnapSpots; } // otherwise it's a new building, or first time using this one else { snapToObject = foundSnapToObject; } // entire search area // <-------------------> // // ------------ // | snapTo | // | Object | // | ------------ // | // | // <------> // = // searchCellRange for (int bottomLeftX = (int) snapToObject.GetGridPositionMin().x - searchCellRange; bottomLeftX <= snapToObject.GetGridPositionMax().x + searchCellRange + 1; bottomLeftX++) { for (int bottomLeftY = (int) snapToObject.GetGridPositionMin().y - searchCellRange; bottomLeftY <= snapToObject.GetGridPositionMax().y + searchCellRange + 1; bottomLeftY++) { currentObject.UpdatePositionAndSizeManual(bottomLeftX, bottomLeftY); if (GridHelper.CanBuildGridObject(currentObject)) { Vector2 arrayPos = new Vector2(bottomLeftX + (currentObject.GetSize().x / 2f), bottomLeftY + (currentObject.GetSize().y / 2f)); if (Vector3.Distance(GridHelper.GetWorldPosition3(arrayPos), snapToObject.GetPositionCenter()) < searchCellRange * GridHelper.GetGridCellSize()) { returnList.Add(GridHelper.GetGridCell(arrayPos).worldPosition3); //print("can build at = " + bottomLeftX + ", " + bottomLeftY); } else { //print("too far away " + bottomLeftX + ", " + bottomLeftY + "!"); } } else { //print("can't build at " + bottomLeftX + ", " + bottomLeftY + "!"); } } } // save list so we don't have to make another list when we're still closest to same building savedSnapSpots = returnList; return returnList; }
protected override void UpdateLabels(GridObject gridObject) { property_one_title.text = "building panel"; //EventDelegate.Add (single_button.onClick, Purchase); //single_button.isEnabled = true; base.UpdateLabels (gridObject); }
public void UpdateOn(GridObject grid_object) { gridObject = grid_object; buildPanel.Setup (gridObject); base.UpdateOn (); // turn off other interfaces this.gameObject.GetComponent<SelectEntity>().Pause(); }
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value) { //if (value.GetType() != typeof(ScriptCollection)) // return null; GridObject designer = new GridObject(value); designer.ShowDialog(); designer.Dispose(); return value; }
public static void BuildGridObject(GridObject gridObject, bool ignoreBuildings) { // get arrayPos of bottomLeft int loopX = (int) gridObject.GetGridPositionMin().x; int loopY = (int) gridObject.GetGridPositionMin().y; for (int x = loopX; x < loopX + gridObject.GetSize ().x; x++) { for (int y = loopY; y < loopY + gridObject.GetSize ().y; y++) { GetGridCell (x, y).BuildGridObject(gridObject, ignoreBuildings); } } }
/* * Give an override option when replacing one road with another */ public void BuildGridObject(GridObject grid_object, bool ignoreBuildings) { if (!CanBuildGridObject(grid_object) && !ignoreBuildings) Debug.Log ("CRITICAL ERROR - should not be getting to this point"); else if (grid_object is AreaBuilding) areaBuilding = (AreaBuilding) grid_object; else if (grid_object is Building) building = (Building) grid_object; else if (grid_object is GridObject) // for example road gridObject = grid_object; else Debug.Log ("Object not recognized"); }
public void MoveGridObject(GridObject tempGrid) { mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePos.z = transform.position.z; Vector3 mouseDir = tempGrid.gameObject.transform.position - mousePos; int direction = (int)Mathf.Ceil(mouseDir.magnitude); if (direction > gridSpacing) { int x = tempGrid.xPos; int y = tempGrid.yPos; //if the new place we want to move doesn't have a grid object already if (tempGrid.isVertical) { if (mouseDir.y < 0 && tempGrid.yPos < gridSize-1) { if(grid[x,y+1].gridObject == null || grid[x,y+1].gridObject == tempGrid){ UpdatePosition (tempGrid, 0, 1); GridFill (tempGrid); Debug.Log("Onbeat drag"); } } else if (mouseDir.y > 0 && tempGrid.yPos - tempGrid.sizeY > -1) { if(grid[x,(y-tempGrid.sizeY)].gridObject == null || grid[x,(y-tempGrid.sizeY)].gridObject == tempGrid){ UpdatePosition (tempGrid, 0, -1); GridFill (tempGrid); Debug.Log("Onbeat drag"); } } } else if (!tempGrid.isVertical) { if(mouseDir.x < 0 && tempGrid.xPos < gridSize-1){ if(grid[x+1,y].gridObject == null || grid[x+1,y].gridObject == tempGrid){ UpdatePosition(tempGrid,1,0); GridFill(tempGrid); Debug.Log("Onbeat drag"); } } else if(mouseDir.x > 0 && tempGrid.xPos - tempGrid.sizeX > -1){ if(grid[x-tempGrid.sizeX,y].gridObject == null || grid[x-tempGrid.sizeX,y].gridObject == tempGrid){ UpdatePosition(tempGrid,-1,0); GridFill(tempGrid); Debug.Log("Onbeat drag"); } } } UpdateGrid(); } }
public void Grid(float tileSize, byte maxValue, GridObject<byte> paint, GridObject<byte> canvas) { if (paint == null || canvas == null) return; float dx = Math.Abs(normal.X); float dy = Math.Abs(normal.Y); int x = (int)(point.X / tileSize); int y = (int)(point.Y / tileSize); float error = dx - dy; int x_inc = (normal.X > 0) ? 1 : -1; int y_inc = (normal.Y > 0) ? 1 : -1; dx *= 2; dy *= 2; float distance = 0; byte value = 0; while(true) { value = Math.Max(value, paint.Get(x, y, maxValue)); canvas.Set(x, y, value); if (value >= maxValue) { return; } if (error > 0) { x += x_inc; error -= dy; distance += dy * tileSize; } else { y += y_inc; error += dx; distance += dx * tileSize; } } }
public static bool CanBuildGridObject(GridObject grid_object, Vector2 wallSnap) { // get arrayPos of bottomLeft int loopX = (int) grid_object.GetGridPositionMin().x; int loopY = (int) grid_object.GetGridPositionMin().y; if (grid_object.GetGridPositionMin().x < -(Grid.GroundPlaneSizeX / 2) || grid_object.GetGridPositionMin().y < -(Grid.GroundPlaneSizeY / 2)) return false; //print("bottomLeft arrayPos = (" + loopX + ", " + loopY + ")"); GridCell gridCell; for (int x = loopX; x < loopX + grid_object.GetSize().x; x++) { for (int y = loopY; y < loopY + grid_object.GetSize().y; y++) { gridCell = GetGridCell(x, y); if (gridCell != null) { if (!gridCell.CanBuildGridObject(grid_object)) { //Debug.Log("Fails on: " + gridCell.ToString()); return false; } // now also test for wall snap, incase building is supposed to attach to wall if (wallSnap != Vector2.zero) { // test if this snap is happening along the x axis (so y is stable) if (wallSnap.y != 0 && y == grid_object.GetWallSnapY() ) { // if so, make sure this is wall, otherwise return false; if(!gridCell.IsWall()) return false; } if (wallSnap.x != 0 && x == grid_object.GetWallSnapX()) { // if so, make sure this is wall, otherwise return false; if(!gridCell.IsWall()) return false; } } } else // invalid cell so don't build return false; } } return true; }
public GridTrunk(AtlasGlobal atlas) : base(atlas) { _debugVpct = new VertexPositionColorTexture[256]; _debugCounter = 0; _heightMap = new GridObject<byte>(64, 64, 0); _tileMap = new GridObject<short>(64, 64, 0); _visitedMap = new bool[64, 64]; _width = _heightMap.Width; _height = _heightMap.Height; _tileSize = 16; Version = 0; }
public byte[,] CanSee(int x, int y, byte height) { if (_sightMap == null || _width != _sightMap.GetLength(0) || _height != _sightMap.GetLength(1)) _sightMap = new byte[_width, _height][][,]; Vector2 v = new Vector2(x + 0.5f, y + 0.5f); if (_sightMap[x, y] == null) { _sightMap[x, y] = new byte[MAX_HEIGHT + 1][,]; } if (_sightMap[x, y][height] == null) { var tmpSight = new GridObject<byte>(_width, _height, height); for (int i = 0; i < _width; i++) { for (int j = 0; j < _height; j++) { var tmp = Math.Min(Math.Min( Raytrace(1, v, new Vector2(i + 0.3f, j + 0.3f)), Raytrace(1, v, new Vector2(i + 0.7f, j + 0.3f))), Math.Min( Raytrace(1, v, new Vector2(i + 0.3f, j + 0.7f)), Raytrace(1, v, new Vector2(i + 0.7f, j + 0.7f)))); tmpSight.Set(i, j, tmp); } } _sightMap[x, y][height] = tmpSight.GetGrid(); } return _sightMap[x, y][height]; }
public void StartSmoke(GridObject grid_object) { StartSmoke(grid_object.size); }
public override void Highlight(GridObject gridObject) { building = (Building) gridObject; base.Highlight (building); }
public void BuildGridObject(GridObject grid_object) { BuildGridObject (grid_object, false); }
// Use this for initialization void Start() { var realPlayer = (GameObject)Instantiate(player,GetLocation(playerStart),Quaternion.identity); playerGrid = realPlayer.GetComponent<GridObject>(); gridObjects = new GridObject[25]; playerGrid.SetLocation(playerStart,GetLocation(playerStart)); grid = new GridObject[5][]; for( int i = 0; i < 5; i ++ ) { grid[i] = new GridObject[5]; } grid[(int)playerStart.x][(int)playerStart.y] = playerGrid; }
public bool TryFindPath(Vector2 v1, Vector2 v2, float raduis, GridObject<float> weightMap, out List<Vector2> path) { for (int i = 0; i < _width; i++) for (int j = 0; j < _height; j++) _visitedMap[i, j] = false; int startX = (int)(v1.X / _tileSize); int startY = (int)(v1.Y / _tileSize); int goalX = (int)(v2.X / _tileSize); int goalY = (int)(v2.Y / _tileSize); //int tileSize = (int)((raduis * 2 - 1) / _tileSize) + 1; float r = raduis / _tileSize; //int d = (int)((raduis * 2 * TwoSqrt - 1) / _tileSize) + 1; bool success = false; path = null; if (startX < 0 || startY < 0 || startX >= _width || startY >= _height || goalX < 0 || goalY < 0 || goalX >= _width || goalY >= _height || _heightMap.Get(startX, startY, 0) != 0 || _heightMap.Get(goalX, goalY, 0) != 0) { return false; } List<GridNode> inList = new List<GridNode>(); inList.Add(new GridNode(-1, startX, startY, 0, GridNode.EstimateDistanceTo(startX, startY, goalX, goalY))); List<GridNode> outList = new List<GridNode>(); _visitedMap[startX, startY] = true; float offset = 0.5f * ((int)(r * 2 + 1) % 2); while (inList.Count > 0 && !success) { float distance = float.MaxValue; int current = -1; for (int i = 0; i < inList.Count; i++) { if (distance > inList[i].GetDistance()) { current = i; distance = inList[i].GetDistance(); } } GridNode n = inList[current]; if (n.x == goalX && n.y == goalY) success = true; if (!success) { for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { if (i + n.x >= 0 && i + n.x < _width && j + n.y >= 0 && j + n.y < _height) { if (_canFit(i + n.x + offset, j + n.y + offset, r)) { bool diagonal = Math.Abs(i - j) != 1; if (!diagonal || _canFit(i * 0.5f + n.x + offset, j * 0.5f + n.y + offset, r)) { var tmpNode = new GridNode(outList.Count, i + n.x, j + n.y, n.steps + (diagonal ? TwoSqrt : 1) * weightMap.Get(i + n.x, j + n.y, 1), GridNode.EstimateDistanceTo(i + n.x, j + n.y, goalX, goalY)); if (!_visitedMap[i + n.x, j + n.y]) { inList.Add(tmpNode); _visitedMap[i + n.x, j + n.y] = true; } else { for (int k = 0; k < inList.Count; k++) { if (inList[k].x == i + n.x && inList[k].y == j + n.y) { if (inList[k].steps > tmpNode.steps) inList[k] = tmpNode; break; } } } } } } } } } inList.RemoveAt(current); outList.Add(n); } if (!success) return false; GridNode node = outList.Last(); path = new List<Vector2>(); path.Add(new Vector2((node.x + offset) * _tileSize, (node.y + offset) * _tileSize)); while (node.parent != -1) { node = outList[node.parent]; path.Add(new Vector2((node.x + offset) * _tileSize, (node.y + offset) * _tileSize)); } path.Reverse(); return true; }
public void FromJson(GridJson json) { Version++; _tileSize = json.size; _width = json.heightMap.GetLength(0); _height = json.heightMap.GetLength(1); if (_width != json.tileMap.GetLength(0) || _height != json.tileMap.GetLength(1)) { } _heightMap = new GridObject<byte>(json.heightMap); _tileMap = new GridObject<short>(json.tileMap); _visitedMap = new bool[_width, _height]; _sightMap = null; }
public static bool CanBuildGridObject(GridObject grid_object) { return CanBuildGridObject(grid_object, Vector2.zero); }
public void Setup(GridObject grid_object) { gridObject = grid_object; cost = gridObject.gameObject.GetComponent<Cost>(); }
//fills in the corresponding grid spaces void GridFill(GridObject tempGrid) { if(!tempGrid.isVertical){ for(int k = 0; k < tempGrid.sizeX; k++){ grid[tempGrid.xPos - k,tempGrid.yPos].gridObject = tempGrid; if(grid[tempGrid.xPos - k,tempGrid.yPos].isGoal && tempGrid.isPlayer) Debug.Log("You are Winner"); } }else if(tempGrid.isVertical){ for(int k = 0; k < tempGrid.sizeY; k++){ grid[tempGrid.xPos,tempGrid.yPos-k].gridObject = tempGrid; if(grid[tempGrid.xPos,tempGrid.yPos-k].isGoal && tempGrid.isPlayer) Debug.Log("You are Winner"); } } }
void UpdatePosition(GridObject tempGrid, int x, int y) { tempGrid.yPos += y; tempGrid.xPos += x; if(tempGrid.yPos == gridSize) tempGrid.yPos = gridSize-1; if(tempGrid.xPos == gridSize) tempGrid.xPos = gridSize-1; if(tempGrid.yPos == -1) tempGrid.yPos = 0; if(tempGrid.xPos == -1) tempGrid.xPos = 0; GridObjectPackage tempPackage = gridObjectRef[tempGrid]; tempPackage.gO.transform.position = grid[tempGrid.xPos,tempGrid.yPos].pos + tempPackage.offset; }
public float Dist(GridObject o) { return Geo.Dist(this, o as Stop); }
public void DrawHeightGrid(GridObject<byte> grid, Color min, Color max) { var cm = Atlas.GetManager<CameraManager>(); var t = Atlas.Content.GetContent<Texture2D>("blop"); var rec = new Rectangle(0, 0, _tileSize, _tileSize); for (int i = Math.Max(0, (int)((cm.Position.X - cm.Width * 0.6f) / _tileSize)); i < Math.Min(_width, (int)((cm.Position.X + cm.Width * 0.6f) / _tileSize)); i++) { for (int j = Math.Max(0, (int)((cm.Position.Y - cm.Height * 0.6f) / _tileSize)); j < Math.Min(_height, (int)((cm.Position.Y + cm.Height * 0.6f) / _tileSize)); j++) { Atlas.Graphics.DrawSprite(t, new Vector2(i * _tileSize, j * _tileSize), rec, Color.Lerp(min, max, grid.Get(i, j, MAX_HEIGHT) / ((MAX_HEIGHT + 1) * 1f))); } } }
public static void BuildGridObject(GridObject gridObject) { // get arrayPos of bottomLeft BuildGridObject(gridObject, false); }
public override void Dehighlight(GridObject GridObject) { building = (Building) GridObject; base.Dehighlight (GridObject); }