so, I'm making a terraria-like game in unity 2019 on windws 10 using c# which has procedurally generated tilemaps and i have this script attached to a grid:
using UnityEngine;
using AccidentalNoise;
using System.Collections.Generic;
using UnityEngine.Tilemaps;
using System;
public class CompileTerrain : MonoBehaviour
{
public TileBase dirtTile;
public TileBase grassTile;
public TileBase stoneTile;
public List<GameObject> fractalLayers = new List<GameObject>();
public Tilemap grid;
public int width;
public int height;
public float seed;
public int caveSmoothness = 2;
void Start()
{
grid.ClearAllTiles();
int touchCount = 0;
Vector3Int newPos;
double nx, ny;
ModuleBase combinedTerrain = CavesAndMountains((uint)seed);
List<Vector3Int> terrainCoords = new List<Vector3Int>();
SMappingRanges ranges = new SMappingRanges();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
nx = (ranges.mapx0 + ((double)x / (double)width) * (ranges.mapx1 - ranges.mapx0)) * 3;
ny = (ranges.mapy0 + ((double)y / (double)height) * (ranges.mapy1 - ranges.mapy0)) * 3;
if (combinedTerrain.Get(nx, ny) > 0f)
{
terrainCoords.Add(new Vector3Int(x, height - y, 0));
}
}
}
List<Tuple<int, int>> neighbors = new List<Tuple<int, int>>() {Tuple.Create(1, 1), Tuple.Create(-1, -1),
Tuple.Create(0, 1), Tuple.Create(1, 0),
Tuple.Create(0, -1), Tuple.Create(-1, 0),
Tuple.Create(-1, 1), Tuple.Create(1, -1)};
for (int index = 0; index < terrainCoords.Count; index++)
{
if (index == terrainCoords.Count)
{
break;
}
touchCount = 0;
for (int posAdd = 0; posAdd < neighbors.Count; posAdd++)
{
newPos = new Vector3Int(terrainCoords[index].x + neighbors[posAdd].Item1, terrainCoords[index].y + neighbors[posAdd].Item2, 0);
touchCount += terrainCoords.Contains(newPos) ? 1 : 0;
}
if (touchCount < 2)
{
terrainCoords.Remove(terrainCoords[index]);
}
}
for (int j = 0; j < caveSmoothness; j++)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
if (!terrainCoords.Contains(new Vector3Int(x, y, 0)))
{
touchCount = 0;
for (int posAdd = 0; posAdd < neighbors.Count; posAdd++)
{
newPos = new Vector3Int(x + neighbors[posAdd].Item1, y + neighbors[posAdd].Item2, 0);
touchCount += terrainCoords.Contains(newPos) ? 1 : -1;
}
if (touchCount > 1)
{
terrainCoords.Add(new Vector3Int(x, y, 0));
}
}
}
}
}
foreach (Vector3Int blck in terrainCoords)
{
grid.SetTile(blck, stoneTile);
}
terrainCoords.Sort((x, y) => x.x == y.x ? x.y.CompareTo(y.y) : x.x.CompareTo(y.x));
terrainCoords.Reverse();
TileBase selectedTile;
int depth = 0;
int lastx = 0;
int lasty = terrainCoords[0].y + 1;
foreach (Vector3Int blck in terrainCoords)
{
depth = blck.x != lastx ? 0 : depth;
lasty = blck.x != lastx ? blck.y + 1 : lasty;
selectedTile = depth < 4 ? grassTile : stoneTile;
selectedTile = 3 < depth && depth < 30 ? dirtTile : selectedTile;
grid.SetTile(blck, selectedTile);
lastx = blck.x;
depth += lasty - blck.y;
lasty = blck.y;
}
int layerNum = 1;
List<Vector3Int> posList = new List<Vector3Int>();
foreach (GameObject layer in fractalLayers)
{
GetPerlinLayer component = layer.GetComponent<GetPerlinLayer>();
for (int k = 0; k < component.populateCount; k++)
{
layerNum++;
foreach (Vector3Int pos in component.GetFractalCoords(width, height, (uint)(seed * layerNum)))
if (grid.GetTile(pos) != null && grid.GetTile(pos) != grassTile)
{
grid.SetTile(pos, component.defaultTile);
}
}
}
}
public static ModuleBase CavesAndMountains(uint seed)
{
AccidentalNoise.Gradient ground_gradient = new AccidentalNoise.Gradient(0, 0, 0, 1);
// lowlands
Fractal lowland_shape_fractal = new Fractal(FractalType.BILLOW, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 2, 0.25, seed);
AutoCorrect lowland_autocorrect = new AutoCorrect(lowland_shape_fractal, 0, 1);
ScaleOffset lowland_scale = new ScaleOffset(0.125, -0.45, lowland_autocorrect);
ScaleDomain lowland_y_scale = new ScaleDomain(lowland_scale, null, 0);
TranslatedDomain lowland_terrain = new TranslatedDomain(ground_gradient, null, lowland_y_scale);
// highlands
Fractal highland_shape_fractal = new Fractal(FractalType.FBM, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 4, 2, seed);
AutoCorrect highland_autocorrect = new AutoCorrect(highland_shape_fractal, -1, 1);
ScaleOffset highland_scale = new ScaleOffset(0.25, 0, highland_autocorrect);
ScaleDomain highland_y_scale = new ScaleDomain(highland_scale, null, 0);
TranslatedDomain highland_terrain = new TranslatedDomain(ground_gradient, null, highland_y_scale);
// mountains
Fractal mountain_shape_fractal = new Fractal(FractalType.RIDGEDMULTI, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 8, 1, seed);
AutoCorrect mountain_autocorrect = new AutoCorrect(mountain_shape_fractal, -1, 1);
ScaleOffset mountain_scale = new ScaleOffset(0.3, 0.15, mountain_autocorrect);
ScaleDomain mountain_y_scale = new ScaleDomain(mountain_scale, null, 0.15);
TranslatedDomain mountain_terrain = new TranslatedDomain(ground_gradient, null, mountain_y_scale);
// terrain
Fractal terrain_type_fractal = new Fractal(FractalType.FBM, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 3, 0.125, seed);
AutoCorrect terrain_autocorrect = new AutoCorrect(terrain_type_fractal, 0, 1);
ScaleDomain terrain_type_y_scale = new ScaleDomain(terrain_autocorrect, null, 0);
AccidentalNoise.Cache terrain_type_cache = new AccidentalNoise.Cache(terrain_type_y_scale);
Select highland_mountain_select = new Select(terrain_type_cache, highland_terrain, mountain_terrain, 0.55, 0.2);
Select highland_lowland_select = new Select(terrain_type_cache, lowland_terrain, highland_mountain_select, 0.25, 0.15);
AccidentalNoise.Cache highland_lowland_select_cache = new AccidentalNoise.Cache(highland_lowland_select);
Select ground_select = new Select(highland_lowland_select_cache, 0, 1, 0.5, null);
// caves
Fractal cave_shape = new Fractal(FractalType.RIDGEDMULTI, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 1, 4, seed);
Bias cave_attenuate_bias = new Bias(highland_lowland_select_cache, 0.65);
Combiner cave_shape_attenuate = new Combiner(CombinerTypes.MULT, cave_shape, cave_attenuate_bias);
Fractal cave_perturb_fractal = new Fractal(FractalType.FBM, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 6, 3, seed);
ScaleOffset cave_perturb_scale = new ScaleOffset(0.5, 0, cave_perturb_fractal);
TranslatedDomain cave_perturb = new TranslatedDomain(cave_shape_attenuate, cave_perturb_scale, null);
Select cave_select = new Select(cave_perturb, 1, 0, 0.75, 0);
return new Combiner(CombinerTypes.MULT, cave_select, ground_select) as ModuleBase;
}
}
which i have so graciously borrowed and modified from the fine folks at accidental noise, and i made an empty gameobject which i attached this script to:
using UnityEngine;
using AccidentalNoise;
using System.Collections.Generic;
using UnityEngine.Tilemaps;
public class GetPerlinLayer : MonoBehaviour
{
public TileBase defaultTile;
public float threshold = 0.5f;
public int populateCount = 5;
public List<Vector3Int> GetFractalCoords(int width, int height, uint seed)
{
double nx, ny;
ModuleBase combinedTerrain = new Fractal(FractalType.FBM, BasisTypes.GRADIENT, InterpTypes.QUINTIC, 6, 2, seed);
List<Vector3Int> fractalCoords = new List<Vector3Int>();
SMappingRanges ranges = new SMappingRanges();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
nx = (ranges.mapx0 + ((double)x / (double)width) * (ranges.mapx1 - ranges.mapx0)) * 3;
ny = (ranges.mapy0 + ((double)y / (double)height) * (ranges.mapy1 - ranges.mapy0)) * 3;
if (combinedTerrain.Get(nx, ny) > threshold)
{
fractalCoords.Add(new Vector3Int(x, height - y, 0));
}
}
}
return fractalCoords;
}
}
and i attached different colored square sprites for each of those gameobjects, and saved them as a prefab. Once i had that prefab, i attached that to the fractalLayers list in my previous script to generate ores. And although it runs fine on a lower scale, I cant run it on a larger scale. And since there's no cure-all for making code run faster (aside from refactoring, which i don't know how to do), and i probably could've made parts of my code more efficient since I'm a novice, i would really like some insight from the eyes of a professional on how to make my code run better. I know i didn't explain everything about my project but its really just a bare-bones project those are the only scripts and unique parts about it, you can just infer what i did and fill in the blanks. Any help is appreciated. And if you could give me the extra push along with some information on the subject, I would love to have some videos recommended along with your insight to guide me along this process, since i am more of a visual learner. Thank you! =)
(For reference, it took me about 4 minutes to build this, with the settings shown here.)
So apparently my word generation method was fine, just the loop I was using to smoothen the terrain took too long.
Related
Codes below:
using System;
using UnityEngine;
using UnityEngine.Serialization;
public class Chessboard : MonoBehaviour
{
[Header("Art")]
[SerializeField] private Material tileMaterial;
//Logic
private const int TILE_COUNT_X = 8;
private const int TILE_COUNT_Y = 8;
private GameObject[,] tiles;
private Camera currentCamera;
private Vector2Int currentHover = -Vector2Int.one;
private void Awake()
{
GenerateAllTiles(1, TILE_COUNT_X, TILE_COUNT_Y);
}
private void Update()
{
if (!currentCamera)
{
currentCamera = Camera.current;
return;
}
RaycastHit info;
Ray ray = currentCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out info, 100, LayerMask.GetMask("Tile")))
{
// get the indexes of Tile i've hit
Vector2Int hitPosition = LookupTileIndex(info.transform.gameObject);
//If we are hovering a tile after not hovering any
if (currentHover == -Vector2Int.one)
{
tiles[hitPosition.x, hitPosition.y].layer = LayerMask.NameToLayer("Hover");
currentHover = hitPosition;
}
//If we were already hovering a tile, change the previous one
else if (currentHover != hitPosition)
{
tiles[currentHover.x, currentHover.y].layer = LayerMask.NameToLayer("Tile");
currentHover = hitPosition;
tiles[hitPosition.x, hitPosition.y].layer = LayerMask.NameToLayer("Hover");
}
}
else
{
if (currentHover != -Vector2Int.one)
{
tiles[currentHover.x, currentHover.y].layer = LayerMask.NameToLayer("Tile");
currentHover = -Vector2Int.one;
}
}
}
//Generate the board (useful)
private void GenerateAllTiles(float tileSize, int tileCountX, int tileCountY)
{
tiles = new GameObject[tileCountX, tileCountY];
for (int x = 0; x < tileCountX; x++)
for (int y = 0; y < tileCountY; y++)
tiles[x, y] = generate1tile(tileSize, x, y);
}
private GameObject generate1tile(float tileSize, int x, int y)
{
GameObject tileObject = new GameObject(string.Format("Tile X:{0}, Y:{1}", x, y));
tileObject.transform.parent = transform;
Mesh mesh = new Mesh();
tileObject.AddComponent<MeshFilter>().mesh = mesh;
tileObject.AddComponent<MeshRenderer>().material = tileMaterial;
Vector3[] vertices = new Vector3[4];
vertices[0] = new Vector3(x * tileSize, 0, y * tileSize);
vertices[1] = new Vector3(x * tileSize, 0, (y + 1) * tileSize);
vertices[2] = new Vector3((x + 1) * tileSize, 0, y * tileSize);
vertices[3] = new Vector3((x + 1) * tileSize, 0, (y + 1) * tileSize);
int[] tris = new int[]{0, 2, 1, 1, 2, 3};
mesh.vertices = vertices;
mesh.triangles = tris;
mesh.RecalculateNormals();
tileObject.layer = LayerMask.NameToLayer("Tile");
tileObject.AddComponent<BoxCollider>();
return tileObject;
}
private Vector2Int LookupTileIndex(GameObject hitInfo)
{
for(int x = 0; x < TILE_COUNT_X; x++)
for(int y = 0; y < TILE_COUNT_Y; y++)
if(tiles[x, y] == hitInfo)
return new Vector2Int(x, y);
return new Vector2Int(-1, -1); //Invalid
}
}
Error Message:
A game object can only be in one layer. The layer needs to be in the range [0...31]
UnityEngine.StackTraceUtility:ExtractStackTrace ()
Chessboard:generate1tile (single,int,int) (at Assets/scripts/Chessboard.cs:93)
Chessboard:GenerateAllTiles (single,int,int) (at Assets/scripts/Chessboard.cs:68)
Chessboard:Awake () (at Assets/scripts/Chessboard.cs:20)
I just get into Unity, so I might be missed some parts of it.
I tired using AI to config my problem, but it doesn't work. I am expecting i put my mouse on a tile, the tile change its color.
Here is the link to the tutorial I watched: https://www.youtube.com/watch?v=FtGy7J8XD90&list=PLmcbjnHce7SeAUFouc3X9zqXxiPbCz8Zp&index=2&ab_channel=Epitome
Here is my code :
BetBoard_Test.cs
//Scoreboard
[SerializeField] protected GameObject prefab_big_road = null;
[SerializeField] Transform pos_big_road = null;
string jsonString = "[1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]"; //sample data
private void Start()
{
ExampleClass dataParser = new ExampleClass();
dataParser.dataToParse = jsonString;
//Convert to Json
string exampleClassToJson = JsonUtility.ToJson(dataParser);
Debug.Log(exampleClassToJson);
ExampleClass obj = JsonUtility.FromJson<ExampleClass>(exampleClassToJson);
//Loop over it
for (int i = 1; i < obj.dataToParse.Length - 1; i += 3)
{
char indivisualChar = obj.dataToParse[i];
Debug.Log(indivisualChar);
}
WinLog();
}
IEnumerator WinLog_big_road()
{
DeleteChildrens(pos_big_road);
yield return new WaitForEndOfFrame();
int[] array_big_road = tzPlayInfo.Instance._BIG_ROAD_;
for (int i = 0; i < rh.Const._HISTORY_COUNT_ * rh.Const._HISTORY_HEIGHT_; i++)
{
if (array_big_road[i] == 0) continue;
int x = i % rh.Const._HISTORY_COUNT_;
int y = i / rh.Const._HISTORY_COUNT_;
float xl = 9.0f;
float yl = -8.0f;
GameObject o = Instantiate(prefab_big_road) as GameObject;
o.transform.SetParent(pos_big_road);
o.transform.localScale = Vector3.one; //(1,1,1)
o.transform.localPosition = new Vector3(x * xl, y * yl, 0f);
o.GetComponent<UISprite>().spriteName = array_big_road[i] == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";
NGUITools.SetActive(o, true);
yield return new WaitForEndOfFrame();
}
yield break;
}
void DeleteChildrens(Transform t)
{
NGUITools.DestroyChildren(t);
}
public void WinLog()
{
StopCoroutine("WinLog_big_road");
StartCoroutine("WinLog_big_road");
}
}
[Serializable]
public class ExampleClass
{
public string dataToParse;
}
ConstantValue.cs
public const int _HISTORY_COUNT_ = 70;
public const int _HISTORY_HEIGHT_ = 6;
PlayInfo.cs
public int[] _BIG_ROAD_ = new int[Const._HISTORY_COUNT_ * Const._HISTORY_HEIGHT_ ];
What i am trying to achieve here is that see image
my jsonString="[1, 1, 2, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]"; that is converted into json format needs to do it something like this for example
1 = blue circle
2 = red circle
just like that in the picture every value on my json data needs to be instantiated with the sprite that is equivalent to 1 and 2 which is i have this condition o.GetComponent<UISprite>().spriteName = array_big_road[i] == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";
PS: I am very sorry if i couldn't explain it very well because english is not my native so i provided a picture. I am very sorry.
EDIT: I did it like this but the problem is that it's not getting what i want all the red which is 2 is the only appearing on the board
for (int i = 1; i < obj.dataToParse.Length - 1; i += 3)
{
char indivisualChar = obj.dataToParse[i];
int j = 0;
if(j < rh.Const._HISTORY_COUNT_ * rh.Const._HISTORY_HEIGHT_)
{
//lets increment it
j++;
//instantiate the sprite
GameObject o = Instantiate(prefab_big_road) as GameObject;
o.transform.SetParent(pos_big_road);
o.transform.localScale = Vector3.one; //(1,1,1)
int x = j % rh.Const._HISTORY_COUNT_;
int y = j / rh.Const._HISTORY_COUNT_;
float xl = 9.0f;
float yl = -8.0f;
o.transform.localPosition = new Vector3(x * xl, y * yl, 0f);
//o.GetComponent<UISprite>().spriteName = indivisualChar == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";
if (indivisualChar == 1)
{
o.GetComponent<UISprite>().spriteName = "layout_player_bigline-01";
NGUITools.SetActive(o, true);
}
else
{
o.GetComponent<UISprite>().spriteName = "layout_banker_bigline-01";
NGUITools.SetActive(o, true);
}
}
//Debug.Log(indivisualChar);
}
EDITED: More information.
It just give me this
All of the sprites are in one place and the second problem of that is all the prefab that is cloned is always red (2)
I solve the issue about all game prefab that are instantiated is all red so what at i did here was like this.
char indivisualChar = obj.dataToParse[i];
int j = 0;
if (j < rh.Const._HISTORY_COUNT_ * rh.Const._HISTORY_HEIGHT_)
{
//lets increment it
j++;
//instantiate the sprite
GameObject o = Instantiate(prefab_big_road) as GameObject;
o.transform.SetParent(pos_big_road);
o.transform.localScale = Vector3.one; //(1,1,1)
int x = j % rh.Const._HISTORY_COUNT_;
int y = j / rh.Const._HISTORY_COUNT_;
float xl = 2.0f;
float yl = -22.0f;
o.transform.localPosition = new Vector3(x * xl, y * yl, 0f);
o.GetComponent<UISprite>().spriteName = indivisualChar == '1' ? "layout_player_bigline-01" : "layout_banker_bigline-01";
NGUITools.SetActive(o, true);
}
Thanks.
I'm building a tile engine for my first game ever and i've been following a guide step by step (can't link it because i'm limited to 2). I have however, made a few modifications to the tilemap so that i can continue with my project.
And while testing my program a noticed a bug that i was hoping you guys could help me resolve...
...The tile engine is drawing the wrong tiles!
This is my TileMap (with numbers assigned to each tile) and this is what my engine draws.
While it should follow the order as seen below (the code is self-explanatory)
rows[0].columns[3].tileID = 0;
rows[0].columns[4].tileID = 1;
rows[0].columns[5].tileID = 2;
rows[0].columns[6].tileID = 3;
rows[0].columns[7].tileID = 4;
rows[1].columns[3].tileID = 5;
rows[1].columns[4].tileID = 6;
rows[1].columns[5].tileID = 7;
rows[1].columns[6].tileID = 8;
rows[1].columns[7].tileID = 9;
rows[2].columns[3].tileID = 10;
rows[2].columns[4].tileID = 11;
rows[2].columns[5].tileID = 12;
rows[2].columns[6].tileID = 13;
rows[2].columns[7].tileID = 14;
rows[3].columns[3].tileID = 15;
rows[3].columns[4].tileID = 16;
rows[3].columns[5].tileID = 17;
rows[3].columns[6].tileID = 18;
rows[3].columns[7].tileID = 19;
rows[4].columns[3].tileID = 20;
rows[4].columns[4].tileID = 21;
rows[4].columns[5].tileID = 22;
rows[4].columns[6].tileID = 23;
rows[4].columns[7].tileID = 24;
rows[5].columns[3].tileID = 25;
rows[5].columns[4].tileID = 26;
rows[5].columns[5].tileID = 27;
rows[5].columns[6].tileID = 28;
rows[5].columns[7].tileID = 29;
Code Essentials
The Important parts of my code
Tile.class
Where i define the tile size and have a function which navigates the tilemap to find my deiserd tile.
namespace TileEngine
{
static class Tile
{
static public Texture2D tileSetTexture;
static public int tileWidth = 48;
static public int tileHeight = 48;
static public Rectangle getSourceRectangle(int tileIndex)
{
int tileY = tileIndex / (tileSetTexture.Width / tileWidth);
int tileX = tileIndex % (tileSetTexture.Height / tileHeight);
return new Rectangle(tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight);
}
}
}
MapCell
Where i define the tileID
class MapCell
{
public List<int> baseTiles = new List<int>();
public int tileID
{
get { return baseTiles.Count > 0 ? baseTiles[0] : 0; }
set
{
if (baseTiles.Count > 0)
baseTiles[0] = value;
else
addBaseTile(value);
}
}
public void addBaseTile(int tileID)
{
baseTiles.Add(tileID);
}
public MapCell(int tileID)
{
this.tileID = tileID;
}
}
TileMap
Where i build the map
class MapRow
{
public List<MapCell> columns = new List<MapCell>();
}
class TileMap
{
public List<MapRow> rows = new List<MapRow>();
public int mapWidth = 50;
public int mapHeight = 50;
public TileMap()
{
for (int y = 0; y < mapHeight; y++)
{
MapRow thisRow = new MapRow();
for (int x = 0; x < mapWidth; x++)
{
thisRow.columns.Add(new MapCell(0));
}
rows.Add(thisRow);
}
}
}
Draw
And lastly my draw code.
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
Vector2 firstSquare = new Vector2(Camera.location.X / Tile.tileWidth, Camera.location.Y / Tile.tileHeight);
int firstX = (int)firstSquare.X;
int firstY = (int)firstSquare.Y;
Vector2 squareOffset = new Vector2(Camera.location.X % Tile.tileWidth, Camera.location.Y % Tile.tileHeight);
int offsetX = (int)squareOffset.X;
int offsetY = (int)squareOffset.Y;
for (int y = 0; y < squaresDown; y++)
{
for (int x = 0; x < squaresAcross; x++)
{
foreach (int tileID in myMap.rows[y + firstY].columns[x + firstX].baseTiles)
{
spriteBatch.Draw(
Tile.tileSetTexture,
new Rectangle(
(x * Tile.tileWidth) - offsetX, (y * Tile.tileHeight) - offsetY,
Tile.tileWidth, Tile.tileHeight),
Tile.getSourceRectangle(tileID),
Color.White);
}
}
}
spriteBatch.End();
base.Draw(gameTime);
}
For me this bug is game breaking, I am using four different Lists that control rockets that shoot out of many launchers. The launchers are also held in a list:
Launchers = new List<Launcher>();
RocketsUp = new List<Rectangle>();
RocketsDown = new List<Rectangle>();
RocketsLeft = new List<Rectangle>();
RocketsRight = new List<Rectangle>();
If you just want to see the full thing Here is the project. (Please Understand that it's a compleat mess, I am trying to make it work, if I had more time then 3 to 4 weeks to finish this I would try and make it more cleaned up)
Questions
The Rockets will shoot out but the right rockets are stopping at random intervals, up, down, and left are work fine, how?
If I let the game play a little it starts to slow down after a while, I had thought that I had to many rockets in game firing but they're firing and being destroyed so shouldn't the game work fine since the rockets are being cleared as fast as there being spawned?
End Questions
Note: (I have multiple levels and when I jump to the next the lag goes away since I have a clear all in a method):
Launchers.Clear();
RocketsUp.Clear();
RocketsDown.Clear();
RocketsRight.Clear();
RocketsLeft.Clear();
What these launchers will do is fire a projectile every set amount of seconds in a strait line until they make contact with a block or player.
I have a foreach loop go through each launcher position and spawn a rocket based on what direction the launcher is facing, 1 = Up, 2 = Down, 3 = Right, 4 = Left:
foreach (Launcher l in Launchers)
{
l.Updata(gameTime);
if (rocketTime > rocketMax)
{
if (l.Direction == 1)
RocketsUp.Add(new Rectangle((int)l.Position.X + 11, (int)l.Position.Y, 9, 18));
if (l.Direction == 2)
RocketsDown.Add(new Rectangle((int)l.Position.X + 11, (int)l.Position.Y, 9, 18));
if (l.Direction == 3)
RocketsRight.Add(new Rectangle((int)l.Position.X, (int)l.Position.Y + 11, 18, 9));
if (l.Direction == 4)
RocketsLeft.Add(new Rectangle((int)l.Position.X, (int)l.Position.Y + 11, 18, 9));
}
}
Then a for loop is called for each rocket list and define their size since the texture is not the correct size:
if (rocketTime > rocketMax)
rocketTime = 0;
for (int i = 0; i < RocketsUp.Count; i++)
{
RocketsUp[i] = new Rectangle(RocketsUp[i].X, RocketsUp[i].Y - 3, RocketsUp[i].Width, RocketsUp[i].Height);
}
for (int i = 0; i < RocketsDown.Count; i++)
{
RocketsDown[i] = new Rectangle(RocketsDown[i].X, RocketsDown[i].Y + 3, RocketsDown[i].Width, RocketsDown[i].Height);
}
for (int i = 0; i < RocketsLeft.Count; i++)
{
RocketsRight[i] = new Rectangle(RocketsRight[i].X + 3, RocketsRight[i].Y, RocketsRight[i].Width, RocketsRight[i].Height);
}
for (int i = 0; i < RocketsLeft.Count; i++)
{
RocketsLeft[i] = new Rectangle(RocketsLeft[i].X - 3, RocketsLeft[i].Y, RocketsLeft[i].Width, RocketsLeft[i].Height);
}
Then the rockets are drawn in the Draw method:
for (int i = 0; i < RocketsUp.Count; i++)
{
spriteBatch.Draw(rocketUp, new Rectangle(RocketsUp[i].X,
RocketsUp[i].Y,
RocketsUp[i].Width,
RocketsUp[i].Height),
Color.White);
}
for (int i = 0; i < RocketsDown.Count; i++)
{
spriteBatch.Draw(rocketDown, new Rectangle(RocketsDown[i].X,
RocketsDown[i].Y,
RocketsDown[i].Width,
RocketsDown[i].Height),
Color.White);
}
for (int i = 0; i < RocketsRight.Count; i++)
{
spriteBatch.Draw(rocketRight, new Rectangle(RocketsRight[i].X, RocketsRight[i].Y, RocketsRight[i].Width, RocketsRight[i].Height), Color.White);
}
for (int i = 0; i < RocketsLeft.Count; i++)
{
spriteBatch.Draw(rocketLeft, new Rectangle(RocketsLeft[i].X, RocketsLeft[i].Y, RocketsLeft[i].Width, RocketsLeft[i].Height), Color.White);
}
Finally the Rockets are called in another class called Block:
public Player BlockCollision(Player player, GameTime gameTime, Game1 game1)
{
this.game1 = game1;
kbState = Keyboard.GetState();
Rectangle BlockRectangle = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);
Rectangle top = new Rectangle((int)Position.X + 5, (int)Position.Y - 10, Texture.Width - 10, 10);
Rectangle bottom = new Rectangle((int)Position.X + 5, (int)Position.Y + Texture.Height, Texture.Width - 10, 10);
Rectangle left = new Rectangle((int)Position.X - 10, (int)Position.Y + 5, 10, Texture.Height - 10);
Rectangle right = new Rectangle((int)Position.X + Texture.Width, (int)Position.Y + 5, 10, Texture.Height - 10);
Rectangle personRectangle = new Rectangle((int)player.Position.X, (int)player.Position.Y, player.Texture.Width, player.Texture.Height);
for (int i = 0; i < game1.RocketsDown.Count; i++)
{
Vector2 Rocket = new Vector2(game1.RocketsDown[i].X, game1.RocketsDown[i].Y);
if (personRectangle.Intersects(new Rectangle((int)Rocket.X, (int)Rocket.Y, game1.rocketDown.Width, game1.rocketDown.Height)))
{
player.Dead = true;
}
}
for (int i = 0; i < game1.RocketsUp.Count; i++)
{
Vector2 Rocket = new Vector2(game1.RocketsUp[i].X, game1.RocketsUp[i].Y);
if (personRectangle.Intersects(new Rectangle((int)Rocket.X, (int)Rocket.Y, game1.rocketUp.Width, game1.rocketUp.Height)))
{
player.Dead = true;
}
}
for (int i = 0; i < game1.RocketsLeft.Count; i++)
{
Vector2 Rocket = new Vector2(game1.RocketsLeft[i].X, game1.RocketsLeft[i].Y);
if (personRectangle.Intersects(new Rectangle((int)Rocket.X, (int)Rocket.Y, game1.rocketLeft.Width, game1.rocketLeft.Height)))
{
player.Dead = true;
}
}
for (int i = 0; i < game1.RocketsRight.Count; i++)
{
Vector2 Rocket = new Vector2(game1.RocketsRight[i].X, game1.RocketsRight[i].Y);
if (personRectangle.Intersects(new Rectangle((int)Rocket.X, (int)Rocket.Y, game1.rocketRight.Width, game1.rocketRight.Height)))
{
player.Dead = true;
}
}
if (BlockState > 0)
{
for (int i = 0; i < game1.RocketsDown.Count; i++)
{
Vector2 Rocket = new Vector2(game1.RocketsDown[i].X, game1.RocketsDown[i].Y);
if (top.Intersects(new Rectangle((int)Rocket.X, (int)Rocket.Y, game1.rocketDown.Width, game1.rocketDown.Height)))
{
game1.RocketsDown.RemoveAt(i);
}
}
for (int i = 0; i < game1.RocketsUp.Count; i++)
{
Vector2 Rocket = new Vector2(game1.RocketsUp[i].X, game1.RocketsUp[i].Y);
if (bottom.Intersects(new Rectangle((int)Rocket.X, (int)Rocket.Y, game1.rocketUp.Width, game1.rocketUp.Height)))
{
game1.RocketsUp.RemoveAt(i);
}
}
for (int i = 0; i < game1.RocketsLeft.Count; i++)
{
Vector2 Rocket = new Vector2(game1.RocketsLeft[i].X, game1.RocketsLeft[i].Y);
if (right.Intersects(new Rectangle((int)Rocket.X, (int)Rocket.Y, game1.rocketLeft.Width, game1.rocketLeft.Height)))
{
game1.RocketsLeft.RemoveAt(i);
}
}
for (int i = 0; i < game1.RocketsRight.Count; i++)
{
Vector2 Rocket = new Vector2(game1.RocketsRight[i].X, game1.RocketsRight[i].Y);
if (left.Intersects(new Rectangle((int)Rocket.X, (int)Rocket.Y, game1.rocketRight.Width, game1.rocketRight.Height)))
{
game1.RocketsRight.RemoveAt(i);
}
}
}
Check this piece of code:
for (int i = 0; i < RocketsLeft.Count; i++)
{
RocketsRight[i] = new Rectangle(RocketsRight[i].X + 3, RocketsRight[i].Y, RocketsRight[i].Width, RocketsRight[i].Height);
}
for (int i = 0; i < RocketsLeft.Count; i++)
{
RocketsLeft[i] = new Rectangle(RocketsLeft[i].X - 3, RocketsLeft[i].Y, RocketsLeft[i].Width, RocketsLeft[i].Height);
}
You have used RocketLeft.Count where you should have used RocketRight.
Edit: Also, try disposing your 'destroyed' rockets using Rectangle.Dispose(). It may help to free up some resources.
I am creating a game after working through a XNA 4.0 book. It will be 3D, but I am already stuck in creating the terrain...
UPDATE: Everything starting from here is an update...
Terrain Update:
public void Update(Matrix view, Matrix projection)
{
View = view;
Projection = projection;
World = Matrix.CreateTranslation(-Width / 2f, 0, Height / 2f);
}
Terrain Draw:
public void Draw(GraphicsDevice g)
{
effect.CurrentTechnique = effect.Techniques["ColoredNoShading"];
effect.Parameters["xView"].SetValue(View);
effect.Parameters["xProjection"].SetValue(Projection);
effect.Parameters["xWorld"].SetValue(World);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
//g.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3, VertexPositionColorNormal.VertexDeclaration);
g.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);
}
}
The commented line is working, in the both cases I am able to see the terrain...
The following code is to initialize Vertex and Index Buffer:
private void SetUpVertices(GraphicsDevice g)
{
float currentH;
int currentI;
vertices = new VertexPositionColorNormal[Width * Height];
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
currentH = heightData[x,y];
currentI = x + y * Width;
vertices[currentI].Position = new Vector3(x, currentH , -y);
if (currentH < minH + (maxH - minH) / 3)
vertices[currentI].Color = Color.ForestGreen;
else if (currentH < maxH - (maxH - minH) / 3)
vertices[currentI].Color = Color.LawnGreen;
else
vertices[currentI].Color = Color.White;
}
}
SetUpIndices(g);
}
private void SetUpIndices(GraphicsDevice g)
{
indices = new int[(Width - 1) * (Height - 1) * 6];
int counter = 0;
for (int y = 0; y < Height - 1; y++)
{
for (int x = 0; x < Width - 1; x++)
{
int lowerLeft = x + y * Width;
int lowerRight = (x + 1) + y * Width;
int topLeft = x + (y + 1) * Width;
int topRight = (x + 1) + (y + 1) * Width;
indices[counter++] = topLeft;
indices[counter++] = lowerRight;
indices[counter++] = lowerLeft;
indices[counter++] = topLeft;
indices[counter++] = topRight;
indices[counter++] = lowerRight;
}
}
SetUpNormals(g);
}
private void SetUpNormals(GraphicsDevice g)
{
for (int i = 0; i < vertices.Length; i++)
{
vertices[i].Normal = Vector3.Zero;
}
int[] index = new int[3];
Vector3 s1, s2, n;
for (int i = 0; i < vertices.Length / 3; i++)
{
for (int y = 0; y < 3; y++)
index[y] = indices[i * 3 + y];
s1 = vertices[index[0]].Position - vertices[index[2]].Position;
s2 = vertices[index[0]].Position - vertices[index[1]].Position;
n = Vector3.Cross(s1, s2);
for (int y = 0; y < 3; y++)
{
vertices[index[y]].Normal += n;
vertices[index[y]].Normal.Normalize();
}
}
FillBuffers(g);
}
private void FillBuffers(GraphicsDevice g)
{
VertexBuffer = new VertexBuffer(g, VertexPositionColorNormal.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
VertexBuffer.SetData(vertices);
IndexBuffer = new IndexBuffer(g, typeof(int), indices.Length, BufferUsage.WriteOnly);
IndexBuffer.SetData(indices);
g.Indices = IndexBuffer;
g.SetVertexBuffer(VertexBuffer);
}
I don't think, that there is a mistake, because it is working with the other line. Might there be an error with the .fx file I am using. If you think so, I am going to switch to BasicEffects...
(You might notice, that the code is from http://www.riemers.net/eng/Tutorials/XNA/Csharp/series1.php )
Thanks for your help...
Yours,
Florian
(Answer to original revision of the question.)
You're not setting your vertex buffer and index buffer onto the graphics device. These two lines of code (untested) should do what you need:
g.GraphicsDevice.Indices = indexBuffer;
g.GraphicsDevice.SetVertexBuffer(vertexBuffer);
Place them just after you set the parameters on your effect (ef), before the loop.
The vertex buffer provides the vertex declaration that the exception message is asking for.
Edit after question update: In your new version you're setting the vertex and index buffers - but it's in the wrong place. You need to set them onto the graphics device each frame. Your code would only work if nothing changes them after you set them in FillBuffers. But I'm guessing that stuff is being drawn outside your class's Draw method?
If that something else is a SpriteBatch, even it works using vertex buffers and index buffers. So it will reset your settings. (It's worth adding that it also sets render states - in which case you might need to see this article.)