void OnTriggerEnter2D(Collider2D col) { Tds_Tile vTile = col.GetComponent <Tds_Tile>(); Tds_Loot vLoot = col.GetComponent <Tds_Loot>(); if (vTile != null) { //ONLY refresh variable current level vListCollider.Add(vTile); //make the player refresh it's pixel tiles variables if (vCharacter != null) { vCharacter.RefreshVariables(vListCollider); } } if (vLoot != null) { //make the player refresh it's pixel tiles variables if (vCharacter != null) { vCharacter.RefreshLoot(vLoot); } } }
//check if the same Texture2D has been found List<GameObject> GridHaveThisTile (Texture2D vTexture, Vector3 aligned){ List<GameObject> vTileObject = new List<GameObject>(); for (int vNbrSizeX = 0; vNbrSizeX < TileSize; vNbrSizeX++) { for (int vNbrSizeY = 0; vNbrSizeY < TileSize; vNbrSizeY++) { int i = 0; while (i < vTilesObjects.transform.childCount) { Transform transform = vTilesObjects.transform.GetChild (i); //if have the same position, check if we have the same Texture2D if (transform.position == aligned + new Vector3(grid.width * vNbrSizeX, grid.height * vNbrSizeY, 0f)) { Sprite vsprite = transform.GetComponent<SpriteRenderer> ().sprite; Tds_Tile vTds_Tile = transform.GetComponent<Tds_Tile> (); vTileObject.Add (transform.gameObject); } i++; } } } //return a result return vTileObject; }
void OnTriggerEnter2D(Collider2D other) { Tds_Tile vTile = other.GetComponent <Tds_Tile> (); if (vTile != null) { if (vTile.vTileType == Tds_Tile.cTileType.Destructible) { vTile.TileDie(); } if (vTile.vTileType == Tds_Tile.cTileType.Wall && vRebounce <= 0) { ProjDie(); } else if (vTile.vTileType == Tds_Tile.cTileType.Wall) { CalculateRebounce(other); } } else if (other.tag == "Character") { Tds_Character vChar = other.GetComponent <Tds_Character> (); //make sure the projectile is for the other faction and the target is alive. if (vChar.vFactionType != vProjFactionType && vChar.IsAlive) { vChar.ApplyDamage(vDmg); ProjDie(); } } }
void RefreshEditor() { //reset the top menu TopMenuPickerIndex = -1; vSelectedTds_Tile = null; //switch to the Selected Tile manually TabIndex = 0; Repaint(); }
//get the right Sorting Order by level + pixeltile type public int GetSortingOrderByTile() { //initialise variable int vNewOrder = 0; Tds_Tile vCurTile = this; //redraw the tiles correctly when we play if (vCurTile.vTileType == Tds_Tile.cTileType.Above) //above is always at top! { vNewOrder = vCurTile.LayerOrder + 300; } else { vNewOrder = vCurTile.LayerOrder + 100; } //return new order return(vNewOrder); }
void OnTriggerExit2D(Collider2D col) { Tds_Tile vTile = col.GetComponent <Tds_Tile>(); Tds_Loot vLoot = col.GetComponent <Tds_Loot>(); //check if we have it on the list so we can remove it if (vTile != null) { if (vListCollider.Contains(vTile)) { vListCollider.Remove(vTile); //make the player refresh it's pixel tiles variables vCharacter.RefreshVariables(vListCollider); } } else if (vLoot != null) { vLoot = null; vCharacter.RefreshLoot(vLoot); } }
IEnumerator OnTriggerEnter2D(Collider2D other) { Tds_Tile vTile = other.GetComponent <Tds_Tile>(); if (vTile != null || other.tag == "Ball") { if (vTile != null && vTile.vTileType == Tds_Tile.cTileType.Destructible) { vTile.TileDie(); } if (((vTile != null && vTile.vTileType == Tds_Tile.cTileType.Wall) || other.tag == "Ball") && vRebounce <= 0) { this.vCollider.isTrigger = false; yield return(new WaitForSeconds(0.02f)); ProjDie(); } else if ((vTile != null && vTile.vTileType == Tds_Tile.cTileType.Wall) || other.tag == "Ball") { this.vCollider.isTrigger = false; yield return(new WaitForSeconds(0.02f)); CalculateRebounce(other); } } else if (other.tag == "Character") { Tds_Character vChar = other.GetComponent <Tds_Character>(); //make sure the projectile is for the other faction and the target is alive. if (vChar.vFactionType != vProjFactionType && vChar.IsAlive) { vChar.ApplyDamage(vDmg); ProjDie(); } } }
public void ChangeWallColor() { ShowFunctionUsed ("ChangeWallColor"); int i = 0; while (i < vTilesObjects.transform.childCount) { Transform vTile = vTilesObjects.transform.GetChild(i); Tds_Tile vTds_Tile = vTile.GetComponent<Tds_Tile> (); SpriteRenderer vRenderer = vTile.GetComponent<SpriteRenderer> (); //check if the Tds_Tile is a wall if (vTds_Tile.vTileType == Tds_Tile.cTileType.Wall && IsLookingWall) vRenderer.color = grid.WallColor; //if wall, put it in green else { Color vNewColor = vRenderer.color; //change the color to white but do not touch the alpha vNewColor.b = 255; vNewColor.g = 255; vNewColor.r = 255; vRenderer.color = vNewColor; //show them in white! } i++; } }
void OnSceneGUI() { /////////////TOP MENU///////////// //go back to normal skin GUI.skin = vNONESkin; ////////////////tile sie///////////// if (IsSizingTile) { GUILayout.BeginHorizontal (); //go back to normal skin GUI.skin = vNONESkin; //get the number of item to be shown vNbrButton = 3; vBoxSize = 65; //create a windows GUILayout.Window (3, new Rect (Screen.width - 50 - (vBoxSize * vNbrButton), 0, vNbrButton * 85, 100), (id) => { //by default it's -1 TileSize = -1; List<Texture2D> vSizeList = new List<Texture2D> (); vSizeList.Add (v1x1); vSizeList.Add (v2x2); vSizeList.Add (v3x3); //get the right size on the list TileSize = GUILayout.SelectionGrid (TileSize, vSizeList.ToArray (), 20);GUILayout.Height (vBoxSize); //check if we selected something if (TileSize > -1) { //increase that number by 1 TileSize++; //switch back to CreateTile SaveVariables ("CreateTile"); IsSizingTile = false; } }, ""); //go back to normal skin GUI.skin = null; GUILayout.EndHorizontal (); } ////////////////////////////////// /////////////COLOR PICKER/////////////// //check if we have something so we show them, the user will select which of them we want! //may have many many tile on the same grid! if (vTilesFound != null) if (vTilesFound.Count > 0) { //remove teleport tile before clicking on it grid.tilePrefab = null; GUILayout.BeginHorizontal (); //go back to normal skin GUI.skin = vCustSkin; //get the number of item to be shown vNbrButton = vTilesFound.Count; int vBoxSizeY = 96; int vBoxSizeX = 40; int vLargestX = 96; //make sure we got enought room to show the current texture foreach (Texture2D vCurText in vTilesFound) { //incease width for every tiles vBoxSizeX += vCurText.width; //the main bos is equal to the biggest tiles in the list if (vBoxSizeY < vCurText.height) vBoxSizeY = vCurText.height; //keep the longest so we can draw the button correctly if (vLargestX < vCurText.width) vLargestX = vCurText.width; } //create a windows GUILayout.Window (3, new Rect (Screen.width - (vLargestX*vNbrButton)-10, 20, (vLargestX*vNbrButton), vBoxSizeY), (id) => { ColorPickerIndex = -1; //get the right color picker on the list + ALSO get the same tiles specs EX : Ground, Order layer 40, levels, isdoor...) ColorPickerIndex = GUILayout.SelectionGrid (ColorPickerIndex, vTilesFound.ToArray (), vTilesFound.Count, GUILayout.Height(vBoxSizeY-20)); //check if we selected something if (ColorPickerIndex > -1) { Tds_Texture vTextureFound = new Tds_Texture (); //get the right texture to use! foreach (Tds_Folder vFolder in grid.tileSet.prefabs) foreach (Tds_Texture vTexture in vFolder.vPixelTextureList) if (vTexture.vTexture == vTilesFound [ColorPickerIndex]) vTextureFound = vTexture; //if we found it, we make our default pixeltexture this one if (vTextureFound != null) { //check if we SELECT the tiles or we ONLY GET THE TILES TO REDRAW if (vAction == "SelectTile") { GameObject vGameObjectSel = null; //get the right texture to use! foreach (GameObject vCurObject in vGameobjectsFound) { if (vCurObject.GetComponent<SpriteRenderer>().sprite.texture == vTextureFound.vTexture) vGameObjectSel = vCurObject; } //try to see if it's the teleport gameobject we want to get if (vGameObjectSel == null) foreach (GameObject vCurObject in vGameobjectsFound) { //get the first teleport in this if (vCurObject.GetComponent<Tds_Tile>().vTileType == Tds_Tile.cTileType.Teleport) vGameObjectSel = vCurObject; } //check if we found it, so we select it to see the inspector! if (vGameObjectSel != null) { //get the pixel tile vSelectedTds_Tile = vGameObjectSel.GetComponent<Tds_Tile>(); //switch to the Selected Tile manually TabIndex = 2; //reset both list vTilesFound = new List<Texture2D> (); vGameobjectsFound = new List<GameObject>(); //refresh editor Repaint(); } } else { //switch to Create Tile SaveVariables ("CreateTile"); //get the new texture! grid.tilePrefab = vTextureFound; //try to get the foreach (GameObject vCurObject in vGameobjectsFound) { if (vCurObject.GetComponent<SpriteRenderer>().sprite.texture == vTextureFound.vTexture) { //get the pixel tile! Tds_Tile vCurTds_Tile = vCurObject.GetComponent<Tds_Tile>(); //now get the right info about this tile on the grid //grid.IsDoor = vCurTds_Tile.IsDoor; grid.OrderLayer = vCurTds_Tile.LayerOrder; grid.vTileType = vCurTds_Tile.vTileType; grid.ShowParticle = vCurTds_Tile.ShowParticles; } } //remove color picker vTilesFound = new List<Texture2D> (); } } } }, ""); //go back to normal skin GUI.skin = null; GUILayout.EndHorizontal (); } ////////////////Level Selector/////////////// Handles.BeginGUI (); //stretch with the number of buttons vNbrButton = 5; vBoxSize = 50; //go back to normal skin GUI.skin = null; //GUILayout.EndHorizontal (); Handles.EndGUI (); //////////////END of the Level Selector/////// ////////////////TILE EDITOR/////////////// Handles.BeginGUI (); //GUILayout.BeginHorizontal (); //stretch with the number of buttons vNbrButton = 7; vBoxSize = 50; //go back to normal skin GUI.skin = vNONESkin; //create a windows GUILayout.Window (2, new Rect (Screen.width - 65, Screen.height - (57 * vNbrButton), 70, vNbrButton * 85), (id) => { //check if we selected create tile on the GUI Texture2D vCreateTileToggle = vCreateTileText; if (vAction == "CreateTile") vCreateTileToggle = vCreateTileTextSel; //check if we have select tile selected Texture2D vSelectTileToggle = vSelectTileText; if (vAction == "SelectTile") vSelectTileToggle = vSelectTileTextSel; //check if we selected create tile on the GUI Texture2D vDeleteTileTextToggle = vDeleteTileText; if (vAction == "DeleteTile") vDeleteTileTextToggle = vDeleteTileTextSel; //check if we selected create tile on the GUI Texture2D vColorPickerToggle = vColorPickerText; if (vAction == "ColorPicker") { vColorPickerToggle = vColorPickerTextSel; } //get the right WallIcon Texture2D vWallIcon = vCanSeeWall; if (!IsLookingWall) vWallIcon = vCannotSeeWall; //by default it's 1x1 Texture2D vSelSizeText = v1x1; if (TileSize == 2) vSelSizeText = v2x2; else if (TileSize == 3) vSelSizeText = v3x3; //Tile Size if (GUILayout.Button (vSelSizeText, GUILayout.Width (vBoxSize), GUILayout.Height (vBoxSize))) { SaveVariables ("SetSize"); //remove color picker vTilesFound = new List<Texture2D> (); //show the size tile IsSizingTile = true; //refresh editor RefreshEditor(); } //Selection Tile if (GUILayout.Button (vSelectTileToggle, GUILayout.Width (vBoxSize), GUILayout.Height (vBoxSize))) { //remove old preview RefreshPreview(); //remove the current prefab to select a new one grid.tilePrefab = null; //switch action SaveVariables ("SelectTile"); //when selecting, we have a tile size of 1 TileSize = 1; //remove color picker vTilesFound = new List<Texture2D> (); //refresh editor RefreshEditor(); } //Create Tile if (GUILayout.Button (vCreateTileToggle, GUILayout.Width (vBoxSize), GUILayout.Height (vBoxSize))) { SaveVariables ("CreateTile"); //remove color picker vTilesFound = new List<Texture2D> (); //make sure a user doesn't waste his time being on a teleport if (grid.vTileType == Tds_Tile.cTileType.Teleport) grid.vTileType = Tds_Tile.cTileType.Ground; //refresh editor RefreshEditor(); } //Delete Tile if (GUILayout.Button (vDeleteTileTextToggle, GUILayout.Width (vBoxSize), GUILayout.Height (vBoxSize))) { SaveVariables ("DeleteTile"); //remove color picker vTilesFound = new List<Texture2D> (); //refresh editor RefreshEditor(); } //Color Picker if (GUILayout.Button (vColorPickerToggle, GUILayout.Width (vBoxSize), GUILayout.Height (vBoxSize))) { SaveVariables ("ColorPicker"); RefreshEditor(); } }, ""); //go back to normal skin GUI.skin = null; //GUILayout.EndHorizontal (); Handles.EndGUI (); //////////////END of the TILE EDITOR/////// int controlId = GUIUtility.GetControlID (FocusType.Passive); Event e = Event.current; Ray ray = Camera.current.ScreenPointToRay (new Vector3 (e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight)); Vector3 mousePos = ray.origin; Texture2D prefab = null; if (grid.tilePrefab != null) prefab = grid.tilePrefab.vTexture; //calculate where is the mouse cursor Vector3 aligned = new Vector3 (Mathf.Floor (mousePos.x / grid.width) * grid.width, Mathf.Floor (mousePos.y / grid.height) * grid.height, 0.0f); //check if the texture has been found before going further if (prefab) { //check if we have a prefab already bruilt if (TilePreview == null) { //initialize it before TilePreview = new List<GameObject> (); //create a preview tile by default TilePreview = CreateTile (1500, Tds_Tile.cTileType.Ground, aligned, true); //positionnate the tilepreview to be shown at the mouse location foreach (GameObject vObject in TilePreview) { Undo.IncrementCurrentGroup (); //vObject.transform.position = aligned; vObject.transform.parent = vAutoTilesObjects.transform; Undo.RegisterCreatedObjectUndo (vObject, "Create" + vObject.name); } } else if (aligned != lastaligned){ //only refresh preview if we change square lastaligned = aligned; RefreshPreview (); } } if (e.isMouse && e.button == 0 && (e.type == EventType.MouseDown || e.type == EventType.MouseDrag)) { if (vLastEventType == EventType.Ignore) { GUIUtility.hotControl = controlId; e.Use (); vLastEventType = e.type; } List<GameObject> gameObject = new List<GameObject> (); //check if the texture has been found before going further if (prefab && vAction == "CreateTile") { gameObject = CreateTile (grid.OrderLayer, grid.vTileType, aligned); //positionnate the tilepreview to be shown at the mouse location foreach (GameObject vObject in gameObject) { Undo.IncrementCurrentGroup (); //vObject.transform.position = aligned; vObject.transform.parent = vTilesObjects.transform; Undo.RegisterCreatedObjectUndo (vObject, "Create" + vObject.name); } } //destroy the same tiles! else if (vAction == "DeleteTile") { //delete the same tile that we got we found! List<GameObject> vFoundTile = GridHaveThisTile (prefab, aligned); //if we have found the same tiles, we can now delete it! if (vFoundTile != null) if (vFoundTile.Count > 0) foreach (GameObject vTileToDestroy in vFoundTile) GameObject.DestroyImmediate (vTileToDestroy); } }//allow right mouse else if (e.type == EventType.MouseUp && vLastEventType != null && e.button == 0) { GUIUtility.hotControl = 0; vLastEventType = EventType.Ignore; //use ignore EventType to skip it } }
//create a list of object to be created List<GameObject> CreateTile(int vOrder, Tds_Tile.cTileType vcTile, Vector3 valigned, bool IsPreview = false) { ShowFunctionUsed ("CreateTile"); //intialize the new list List<GameObject> ListObject = new List<GameObject> (); //every sprite will be for example 8x8 = 64pixels by 64pixels for (int vNbrSizeX = 0; vNbrSizeX < TileSize; vNbrSizeX++) { for (int vNbrSizeY = 0; vNbrSizeY < TileSize; vNbrSizeY++) { //by default, get the dimension needed int vDimX = grid.dimension; int vDimY = grid.dimension; if (grid.tilePrefab == grid.selectionPrefab) { vDimX = 64; //use default dimension for the teleport prefab vDimY = 64; } int vXcpt = 0; int vXLeft = grid.tilePrefab.vTexture.width; while (vXLeft > 0) { //reinitalize the Y variable int vYLeft = grid.tilePrefab.vTexture.height; int vYcpt = 0; while (vYLeft > 0) { //get the new position of this tile Vector3 vNewPosition = valigned + new Vector3 (grid.width * vXcpt, grid.height * vYcpt, 0f) + new Vector3 (grid.width * vNbrSizeX, grid.height * vNbrSizeY, 0f); //Check if the grid already has that tile on the same very position. if yes, we delete it to have the new info! Just like a replace if (!IsPreview) GridHaveThisTile2 (grid.tilePrefab.vTexture, vNewPosition, IsPreview); //create a different tile for 2D Walls GameObject vNewObj = vTileObject; if (vcTile == Tds_Tile.cTileType.Wall) vNewObj = vWallObject; //before going further, we check if it already exist GameObject gameObject = (GameObject)PrefabUtility.InstantiatePrefab (vNewObj); //change its tag for Tile gameObject.tag = "Tile"; gameObject.name = grid.tilePrefab.vFilename.Substring (0, grid.tilePrefab.vFilename.Length - 4); //rename the gameobject + remove file extension gameObject.transform.localScale = new Vector3 (vFactorScale,vFactorScale, 1f); //handle the different size if (grid.tilePrefab == grid.selectionPrefab) gameObject.transform.localScale = new Vector3 (16f,16f, 1f); SpriteRenderer vRenderer = gameObject.GetComponent<SpriteRenderer> (); //make sure we get the last pixel even if it's not standard int vDimTakenX = vDimX; if (vDimTakenX > vXLeft) vDimTakenX = vXLeft; //make sure we get the last pixel even if it's not standard int vDimTakenY = vDimX; if (vDimTakenY > vYLeft) vDimTakenY = vYLeft; Sprite vsprite = Sprite.Create (grid.tilePrefab.vTexture, new Rect (vXcpt * vDimX, vYcpt * vDimY, vDimTakenX, vDimTakenY), new Vector2 (0f, 0f), 128f); vRenderer.sprite = vsprite; //show the tile preview in RED when deleting if (vAction == "DeleteTile") vRenderer.color = Color.red; //if teleport, get the teleport color by deault //put the teleport tile above all of the others if (grid.vTileType == Tds_Tile.cTileType.Teleport) { vRenderer.color = grid.TeleportColor; gameObject.GetComponent<SpriteRenderer> ().sortingOrder = 1500; } //change the tile wall Tds_Tile vTiles = gameObject.GetComponent<Tds_Tile> (); vTiles.vTileType = vcTile; vTiles.AnimationSpeed = grid.AnimationSpeed; //send the animated tiles if (grid.tilePrefab.vAnimationList.Count > 0) { foreach (Tds_Texture vAPixelT in grid.tilePrefab.vAnimationList) vAPixelT.vOrder = int.Parse(Regex.Replace (vAPixelT.vTexture.name, "[^0-9]", "")); //sort grid.tilePrefab.vAnimationList = grid.tilePrefab.vAnimationList.OrderBy(x=>x.vOrder).ToList(); foreach (Tds_Texture vAPixelT in grid.tilePrefab.vAnimationList) if (!vTiles.vAnimationList.Contains (vAPixelT.vTexture)) //don't add texture already in it vTiles.vAnimationList.Add (vAPixelT.vTexture); } //keep a trace of the layerorder for this very tile so we can draw them correctly between levels vTiles.LayerOrder = vOrder; //make sure there is a spriterenderer on the prefab to apply the orderlayer if (gameObject.GetComponent<SpriteRenderer> () != null) gameObject.GetComponent<SpriteRenderer> ().sortingOrder = vTiles.GetSortingOrderByTile(); //return the right drawing order for this new tile //change position gameObject.transform.position = vNewPosition; if (vcTile == Tds_Tile.cTileType.Wall || vcTile == Tds_Tile.cTileType.Destructible) gameObject.AddComponent<PolygonCollider2D> (); //check if teleport, create 2 gameobject wich will be linked together if (grid.vTileType == Tds_Tile.cTileType.Teleport && vOrder != 1500) { //create a copy of it at the same location GameObject gameObject2 = Instantiate (gameObject); //link them together gameObject.GetComponent<Tds_Tile> ().vLinkedObject = gameObject2; gameObject2.GetComponent<Tds_Tile> ().vLinkedObject = gameObject; //add them ListObject.Add (gameObject2); } //add it ListObject.Add (gameObject); //remove pixels Y vYLeft -= vDimY; vYcpt++; } //remove pixels X vXLeft -= vDimX; vXcpt++; } } } return ListObject; }