public virtual void Update() { if (!MainCharacter.Dead && GameGlobals.paused == false) { //_enemyTimer.UpdateTimer(); //if (_enemyTimer.Test()) //{ // int randomX = Engine.Globals.RandomNumber(0, 1000); // int randomY = Engine.Globals.RandomNumber(0, 1000); // Console.WriteLine(randomX + " , " + randomY); // GameGlobals.PassNpc(new FirstEnemy(new Vector2(randomX,randomY)));; // _enemyTimer.Reset(); //} MainCharacter.Update(_offset, _grid); for (int idx = 0; idx < _projectiles.Count(); idx++) { _projectiles[idx].Update(_offset, _npcs.ToList <Unit>()); if (_projectiles[idx].Done) { _projectiles.RemoveAt(idx); idx--; } } List <Unit> mainCharList = new List <Unit>(); mainCharList.Add(MainCharacter); for (int idx = 0; idx < _enemyProjectiles.Count(); idx++) { _enemyProjectiles[idx].Update(_offset, mainCharList); if (_enemyProjectiles[idx].Done) { _enemyProjectiles.RemoveAt(idx); idx--; } } for (int idx = 0; idx < _npcs.Count(); idx++) { _npcs[idx].Update(_offset, MainCharacter, _grid); if (_npcs[idx].Dead) { GridLocation location = _grid.GetSlotFromPixel(_npcs[idx].Position, Vector2.Zero); location.Filled = false; location.Impassible = false; _nKilled++; _npcs.RemoveAt(idx); idx--; } } for (int idx = 0; idx < _effects.Count(); idx++) { _effects[idx].Update(_offset); if (_effects[idx].Done) { _effects.RemoveAt(idx); idx--; } } } else { if (Globals.MyKeyboard.GetNewPress("Enter")) { ResetWorld(null); } } if (_grid != null) { _grid.Update(_offset); } if (Globals.MyKeyboard.GetNewPress("Escape") && !MainCharacter.Dead) { GameGlobals.paused = !GameGlobals.paused; } if (Globals.MyKeyboard.GetNewPress("G")) { _grid.ShowGrid = !_grid.ShowGrid; } _ui.Update(this); }
IEnumerator ThreadMesh(int[,] map, int startX, int startY, int chunkSize, float squareSize, bool forceClear) { int width = map.GetLength(0); int height = map.GetLength(1); int borderSeemX = startX < width - chunkSize ? 1 : 0; int borderSeemY = startY < height - chunkSize ? 1 : 0; int chunksX = width / chunkSize; int chunksY = height / chunkSize; int chunkIndexX = startX / chunkSize; int chunkIndexY = startY / chunkSize; if (chunks == null) { chunks = new Chunk[chunksX, chunksY]; } else if (forceClear) { foreach (Chunk chunkItem in chunks) { if (chunkItem.caveMesh != null) { Destroy(chunkItem.caveMesh.gameObject); } if (chunkItem.wallMesh != null) { Destroy(chunkItem.wallMesh.gameObject); } if (chunkItem.polygonColliders != null) { foreach (PolygonCollider2D collider in chunkItem.polygonColliders) { if (collider != null) { Destroy(collider); } } } } } Chunk chunk = chunks[chunkIndexX, chunkIndexY]; if (chunk == null || forceClear) { chunk = new Chunk(); chunks[chunkIndexX, chunkIndexY] = chunk; } while (chunk.isRendering) { yield return(null); } chunk.isRendering = true; chunk.Clear(); if (chunk.squareGrid == null) { chunk.squareGrid = new SquareGrid(map, startX, startY, chunkSize, borderSeemX, borderSeemY, squareSize); } SquareGrid squareGrid = chunk.squareGrid; //squareGrid = new SquareGrid(map, startX, startY, chunkSize, borderSeemX, borderSeemY, squareSize); squareGrid.Update(map, startX, startY, chunkSize, borderSeemX, borderSeemY, squareSize, filterOnMaterialType, filterMaterialType); if (!squareGrid.dirty) { chunk.isRendering = false; yield break; } chunk.vertices = new List <Vector3>(); chunk.triangles = new List <int> [materials.Length]; List <int>[] triangles = chunk.triangles; List <Vector3> vertices = chunk.vertices; int len = triangles.Length; for (int i = 0; i < len; i++) { triangles[i] = new List <int>(); } int endX = startX + chunkSize + borderSeemX - 1; int endY = startY + chunkSize + borderSeemY - 1; chunk.StartTriangulate(startX, startY, endX, endY); //print("Start Triangulate"); yield return(StartCoroutine(chunk.WaitFor())); MeshFilter cave = chunk.caveMesh; if (cave == null) { GameObject caveGO = Instantiate(cavePrefab); cave = caveGO.GetComponent <MeshFilter>(); cave.transform.SetParent(this.transform, false); chunk.caveMesh = cave; cave.mesh = new Mesh(); } Mesh mesh = cave.sharedMesh; mesh.Clear(); mesh.SetVertices(vertices); int subMeshCount = 0; len = triangles.Length; for (int i = 0; i < len; i++) { if (triangles[i].Count > 0) { subMeshCount += 1; } } mesh.subMeshCount = subMeshCount; MeshRenderer meshRenderer = cave.GetComponent <MeshRenderer>(); Material[] newMaterials = new Material[subMeshCount]; subMeshCount = 0; for (int i = 0; i < len; i++) { List <int> triangleList = triangles[i]; if (triangles[i].Count > 0) { mesh.SetTriangles(triangleList, subMeshCount); newMaterials[subMeshCount] = materials[i]; subMeshCount++; } } meshRenderer.materials = newMaterials; int tileAmount = textureTileSize; Vector2[] uvs = new Vector2[vertices.Count]; for (int i = 0; i < uvs.Length; i++) { uvs[i] = new Vector2(vertices[i].x, vertices[i].z); } /* * len = vertices.Count; * float size = width > height ? width : height; * float sizeFactor = size/2*squareSize; * for (int i =0; i < len; i ++) * { * float percentX = Mathf.InverseLerp(-sizeFactor,sizeFactor,vertices[i].x); * float percentY = Mathf.InverseLerp(-sizeFactor,sizeFactor,vertices[i].z); * uvs[i] = new Vector2(percentX, percentY); * * }*/ mesh.uv = uvs; mesh.RecalculateBounds(); mesh.RecalculateNormals(); ; CreateWallMesh(chunk, startX, startY, width, height, chunkSize); chunk.isRendering = false; }