public static VertVT[] GetElementVertices(Vector4 elementBounds, SpriteBounds texCoordBounds) { var verts = new Vector2[] { new Vector2(elementBounds.X, elementBounds.Y), new Vector2(elementBounds.X, elementBounds.Y + elementBounds.W), new Vector2(elementBounds.X + elementBounds.Z, elementBounds.Y + elementBounds.W), new Vector2(elementBounds.X + elementBounds.Z, elementBounds.Y) }; var coords = new Vector2[] { new Vector2(texCoordBounds.X, texCoordBounds.Y), new Vector2(texCoordBounds.X, texCoordBounds.Y + texCoordBounds.Height), new Vector2(texCoordBounds.X + texCoordBounds.Width, texCoordBounds.Y + texCoordBounds.Height), new Vector2(texCoordBounds.X + texCoordBounds.Width, texCoordBounds.Y) }; return(new VertVT[] { new VertVT(new Vector3(verts[0]), coords[0]), new VertVT(new Vector3(verts[1]), coords[1]), new VertVT(new Vector3(verts[2]), coords[2]), new VertVT(new Vector3(verts[3]), coords[3]) }); }
public static void DrawSprite(Texture2D texture, Vector4 destination, SpriteBounds spriteBounds) { var verts = GetElementVertices(destination, spriteBounds); SpritesToRender.Add(new SpriteElement() { Texture = texture, Offset = VertexList.Count, ElemCount = verts.Length }); VertexList.AddRange(verts); }
private IEnumerator GetSpriteBounds(string path) { tempBounds = null; byte[] content = { }; string filePath = Application.streamingAssetsPath + path + ".dwb"; if (filePath.Contains("://") || filePath.Contains(":///")) { WWW www = new WWW(filePath); yield return(www); while (!www.isDone) { yield return(new WaitForEndOfFrame()); } content = www.bytes; } else { content = File.ReadAllBytes(filePath); } SpriteBounds bounds = new SpriteBounds(); BinaryReader reader = new BinaryReader(new MemoryStream(content)); bounds.from_origin_north = reader.ReadInt32(); bounds.from_origin_east = reader.ReadInt32(); bounds.from_origin_south = reader.ReadInt32(); bounds.from_origin_west = reader.ReadInt32(); //Older config files don't have this, so skip if they don't! if (reader.BaseStream.Length == 32) { bounds.tile_excess_north = reader.ReadSingle(); bounds.tile_excess_east = reader.ReadSingle(); bounds.tile_excess_south = reader.ReadSingle(); bounds.tile_excess_west = reader.ReadSingle(); } reader.Close(); tempBounds = bounds; }
/* Set specific rotation */ public void SetRotation(PropRotation rotation) { SpriteBounds propBounds = null; if (ourCollider) { ourCollider.enabled = false; } //Set rotation (if invalid try next) propRotation = rotation; switch (propRotation) { case PropRotation.FACING_LEFT: if (spriteSet.leftFacing.sprite == null) { RotateMe(); return; } ourSprite.sprite = spriteSet.leftFacing.sprite; propBounds = spriteSet.leftFacing.bounds; break; case PropRotation.FACING_FRONT: if (spriteSet.frontFacing.sprite == null) { RotateMe(); return; } ourSprite.sprite = spriteSet.frontFacing.sprite; propBounds = spriteSet.frontFacing.bounds; break; case PropRotation.FACING_RIGHT: if (spriteSet.rightFacing.sprite == null) { RotateMe(); return; } ourSprite.sprite = spriteSet.rightFacing.sprite; propBounds = spriteSet.rightFacing.bounds; break; case PropRotation.FACING_BACK: if (spriteSet.backFacing.sprite == null) { RotateMe(); return; } ourSprite.sprite = spriteSet.backFacing.sprite; propBounds = spriteSet.backFacing.bounds; break; } //Set collider if (!ourCollider) { return; } List <Vector2> colliderPoints = new List <Vector2>(); //All values should be /200 because of sprite pixels to unity units conversion colliderPoints.Add(new Vector2((float)propBounds.from_origin_west / 200.0f, (float)propBounds.from_origin_north / 200.0f)); colliderPoints.Add(new Vector2(-((float)propBounds.from_origin_east / 200.0f), (float)propBounds.from_origin_north / 200.0f)); colliderPoints.Add(new Vector2(-((float)propBounds.from_origin_east / 200.0f), -((float)propBounds.from_origin_south / 200.0f))); colliderPoints.Add(new Vector2((float)propBounds.from_origin_west / 200.0f, -((float)propBounds.from_origin_south / 200.0f))); ourCollider.pathCount = 1; ourCollider.SetPath(0, colliderPoints.ToArray()); ourCollider.enabled = true; }
/* Get the tiles that neighbour a given tile given bounds (if the neighbour would be off-grid, it is null) - **List contains original tile in index 0!! */ public List <Tile> GetNeighboursFromBounds(Tile tile, SpriteBounds bounds) { int northBounds = (int)Math.Ceiling(bounds.tile_excess_north) + 1; int eastBounds = (int)Math.Ceiling(bounds.tile_excess_east) + 1; int southBounds = (int)Math.Ceiling(bounds.tile_excess_south) + 1; int westBounds = (int)Math.Ceiling(bounds.tile_excess_west) + 1; //Debug.Log("North: " + northBounds + ", South: " + southBounds + ", East: " + eastBounds + ", West: " + westBounds); /* There is definitely a more optimal way to do this, but as this function is called rarely, I'm using this method as it's easier to debug for my tiny brain */ List <Vector2> posList = new List <Vector2>(); for (int x = 0; x < eastBounds; x++) { for (int y = 0; y < northBounds; y++) { Vector2 newPos = tile.GridPos + new Vector2(x, y); if (!posList.Contains(newPos)) { posList.Add(newPos); } } } for (int x = 0; x < eastBounds; x++) { for (int y = 0; y < southBounds; y++) { Vector2 newPos = tile.GridPos + new Vector2(x, -y); if (!posList.Contains(newPos)) { posList.Add(newPos); } } } for (int x = 0; x < westBounds; x++) { for (int y = 0; y < northBounds; y++) { Vector2 newPos = tile.GridPos + new Vector2(-x, y); if (!posList.Contains(newPos)) { posList.Add(newPos); } } } for (int x = 0; x < westBounds; x++) { for (int y = 0; y < southBounds; y++) { Vector2 newPos = tile.GridPos + new Vector2(-x, -y); if (!posList.Contains(newPos)) { posList.Add(newPos); } } } List <Tile> neighbourList = new List <Tile>(); foreach (Vector2 pos in posList) { neighbourList.Add(GetTileAtGridPos(pos)); //Debug.Log(pos); } return(neighbourList); }
public bool CollidesWith(SpriteGameObject obj) { return(SpriteBounds.Intersects(obj.SpriteBounds)); }
private IEnumerator LoadPropConfigAsync(BinaryReader reader) { List <PropData> propData = new List <PropData>(); int versionNum = reader.ReadInt32(); int entryCount = reader.ReadInt32(); for (int i = 0; i < entryCount; i++) { PropData thisData = new PropData(); bool loadedOK = true; thisData.propID = reader.ReadInt32(); thisData.propName = (PropTypes)thisData.propID; thisData.propNameString = reader.ReadString(); if (thisData.propName.ToString() != thisData.propNameString) { loadedOK = false; } thisData.propDesc = reader.ReadString(); thisData.isWaypoint = reader.ReadBoolean(); if (thisData.isWaypoint) { thisData.waypointFor = (PropWaypointUser)reader.ReadInt16(); thisData.waypointType = (PropWaypointType)reader.ReadInt16(); } thisData.isScripted = reader.ReadBoolean(); if (thisData.isScripted) { thisData.scriptName = reader.ReadString(); } thisData.isPOI = reader.ReadBoolean(); if (thisData.isPOI) { thisData.poiType = (PoiType)reader.ReadInt16(); thisData.poiGoonCount = reader.ReadInt32(); } thisData.placement = (reader.ReadBoolean()) ? PropPlacement.INTERIOR : PropPlacement.EXTERIOR; thisData.isUnpathable = reader.ReadBoolean(); thisData.hideInEditor = reader.ReadBoolean(); string folderPath = "PROPS/" + thisData.propName.ToString() + "/"; string streamingPath = "/PROPCONFIGS/" + thisData.propName.ToString() + "/"; thisData.spriteSet.frontFacing.sprite = Resources.Load <Sprite>(folderPath + "FRONT_FACING"); if (thisData.spriteSet.frontFacing.sprite != null) { tempBounds = null; StartCoroutine(GetSpriteBounds(streamingPath + "FRONT_FACING")); while (tempBounds == null) { yield return(new WaitForEndOfFrame()); } thisData.spriteSet.frontFacing.bounds = tempBounds; } thisData.spriteSet.leftFacing.sprite = Resources.Load <Sprite>(folderPath + "LEFT_FACING"); if (thisData.spriteSet.leftFacing.sprite != null) { tempBounds = null; StartCoroutine(GetSpriteBounds(streamingPath + "LEFT_FACING")); while (tempBounds == null) { yield return(new WaitForEndOfFrame()); } thisData.spriteSet.leftFacing.bounds = tempBounds; } thisData.spriteSet.rightFacing.sprite = Resources.Load <Sprite>(folderPath + "RIGHT_FACING"); if (thisData.spriteSet.rightFacing.sprite != null) { tempBounds = null; StartCoroutine(GetSpriteBounds(streamingPath + "RIGHT_FACING")); while (tempBounds == null) { yield return(new WaitForEndOfFrame()); } thisData.spriteSet.rightFacing.bounds = tempBounds; } thisData.spriteSet.backFacing.sprite = Resources.Load <Sprite>(folderPath + "BACK_FACING"); if (thisData.spriteSet.backFacing.sprite != null) { tempBounds = null; StartCoroutine(GetSpriteBounds(streamingPath + "BACK_FACING")); while (tempBounds == null) { yield return(new WaitForEndOfFrame()); } thisData.spriteSet.backFacing.bounds = tempBounds; } thisData.spriteSet.editorUISprite = Resources.Load <Sprite>(folderPath + "EDITOR_UI"); tempBounds = null; StartCoroutine(GetSpriteBounds(streamingPath + thisData.propName.ToString())); while (tempBounds == null) { yield return(new WaitForEndOfFrame()); } thisData.spriteSet.completeSpriteBounds = tempBounds; thisData.zOffset = reader.ReadInt16(); if (loadedOK) { propData.Add(thisData); } } reader.Close(); PropProperties.SetData(propData); }
void UpdateVisual() { Visual.DistanceFromBottom = SpriteBounds.DistanceFrom(SpriteBounds.Direction.Bottom); }