private void Start() { spriteRenderer = GetComponent <SpriteRenderer>(); Vector3 extents = spriteRenderer.sprite.bounds.extents; //leftNode.position = new Vector3(gameObject.transform.position.x - extents.x, // gameObject.transform.position.y + extents.y, // gameObject.transform.position.z); //rightNode.position = new Vector3(gameObject.transform.position.x + extents.x, // gameObject.transform.position.y + extents.y, // gameObject.transform.position.z); if (gameObject.tag == "tag_ladder") { leftNode.position = new Vector3(verticalAdjustment, -extents.y / leftNodeSpacingVariable, 0); rightNode.position = new Vector3(verticalAdjustment, extents.y / rightNodesSpacingVariable, 0); } else { leftNode.position = new Vector3(-extents.x / leftNodeSpacingVariable, extents.y + verticalAdjustment, 0); rightNode.position = new Vector3(extents.x / rightNodesSpacingVariable, extents.y + verticalAdjustment, 0); } //Matrix4x4 parentTransform = Matrix4x4.TRS(transform.parent.position, transform.parent.rotation, transform.parent.localScale); Matrix4x4 platformMatrix = Matrix4x4.TRS(gameObject.transform.position, gameObject.transform.rotation, gameObject.transform.lossyScale); //platformMatrix = platformMatrix * parentTransform; leftNode.position = platformMatrix.MultiplyPoint3x4(leftNode.position); rightNode.position = platformMatrix.MultiplyPoint3x4(rightNode.position); leftNode.platform = this; rightNode.platform = this; width = extents.x * gameObject.transform.localScale.x * 2; height = extents.y * gameObject.transform.localScale.y * 2; //Adding grid cells for both the nodes so that they register themselves to a certain part of the world GridIndex index; index = WorldGrid.GetGridIndex(leftNode.position); leftNode.gridCell = WorldGrid.GetTheWorldGridCell(index); WorldGrid.AddToCell(leftNode, leftNode.gridCell); index = WorldGrid.GetGridIndex(rightNode.position); rightNode.gridCell = WorldGrid.GetTheWorldGridCell(index); WorldGrid.AddToCell(rightNode, rightNode.gridCell); }
//Call this function to register player to the world grid protected void UpdatePositionInWorld()//Vector3 position, out GridCell gridCell) { GridIndex index = WorldGrid.GetGridIndex(gameObject.transform.position); if (gridCell == null) { gridCell = WorldGrid.GetTheWorldGridCell(index); } else if (index.x != gridCell.index.x || index.y != gridCell.index.y) { gridCell = WorldGrid.GetTheWorldGridCell(index); } if (gridCell != previousGridCell) { if (gridCell != null) { WorldGrid.AddToCell(this, gridCell); if (previousGridCell != null) { WorldGrid.RemoveFromCell(this, previousGridCell); } previousGridCell = gridCell; } else { Debug.Log("Something went wrong. Player doesn't have a grid cell"); } } //Debug.DrawLine(Vector3.zero, gridCell.worldPosition); //Debug.Log("Character: " + gameObject.name); //Debug.Log("Grid cell indices: " + gridCell.index.x + " " + gridCell.index.y); //Debug.Log("Grid cell indices: " + gridCell.index.x + " " + gridCell.index.y); //Debug.Log("Grid character count: " + gridCell.character.Count); //Debug.Log("Grid node count: " + gridCell.node.Count); //List<GridCell> cells = WorldGrid.Instance.gridArray[gridIndex.x, gridIndex.y]; }