Updated a large amount of the code to C#

This commit is contained in:
2020-04-19 03:37:18 -06:00
parent 7b68de9526
commit b20710501f
30 changed files with 1046 additions and 1 deletions

View File

@@ -0,0 +1,12 @@
public class Block {
private string name = null;
public Block(string name) {
this.name = name;
}
public string getName() {
return name;
}
}

View File

@@ -0,0 +1,28 @@
using System.Collections.Generic;
namespace Collector.Dimension
{
public class BlockMaterials {
public static Dictionary<string,Block> Materials = new Dictionary<string, Block>();
//public static Dictionary<String,Texture> Textures = new Dictionary<>();
//Private so the singleton can't be instantiated
private BlockMaterials() {}
public static void create(){
Materials.Add("grass",new Block("grass"));
Materials.Add("wood",new Block("wood"));
Materials.Add("water",new Block("water"));
Materials.Add("stone",new Block("stone"));
Materials.Add("snow",new Block("snow"));
Materials.Add("sand",new Block("sand"));
Materials.Add("air",new Block("air"));
Materials.Add("roof",new Block("roof"));
Materials.Add("wall",new Block("wall"));
/*
for (String s:BlockMaterials.Materials.keySet()) {
Textures.Add(s,new Texture("assets/" + s + ".png"));
}
*/
}
}
}

View File

@@ -0,0 +1,180 @@
//https://www.redblobgames.com/maps/terrain-from-noise/
using System;
using System.Collections.Generic;
using Collector.ThirdPartyCode;
namespace Collector.Dimension
{
public class Chunks: IRestrictions {
public static Dictionary<Tuple<int, int, int>, Block> LoadedChunks = new Dictionary<Tuple<int, int, int>, Block>();
public static Dictionary<Tuple<int, int, int>, Block> SavedChunks = new Dictionary<Tuple<int, int, int>, Block>();
public static OpenSimplexNoise Gen1 = new OpenSimplexNoise(IRestrictions.Seed+ 1);
public static OpenSimplexNoise Gen2 = new OpenSimplexNoise(IRestrictions.Seed);
public static void CreateStructures() {
var i = 10;
SetBlock(3 + i, 2,1,"wall");
SetBlock(4 + i, 2,1,"wall");
SetBlock(3 + i, 3,1, "roof");
SetBlock(4 + i, 3,1,"roof");
}
public static void SetBlock(int x, int y,int z, String name) {
LoadedChunks.Add(new Tuple<int,int,int>(x, y, z), BlockMaterials.Materials[name]);
SavedChunks.Add(new Tuple<int,int,int>(x, y, z), BlockMaterials.Materials[name]);
}
public static void PlaceBlock(int x, int y, String name){
var triple = new Tuple<int, int, int>(x, y, 1);
if (!Chunks.LoadedChunks[triple].getName().Equals("air")) return;
Chunks.LoadedChunks[triple] = BlockMaterials.Materials[name];
Chunks.SavedChunks[triple] = BlockMaterials.Materials[name];
}
public static void RemoveBlock(int x, int y){
var triple = new Tuple<int, int, int>(x, y, 1);
if (Chunks.LoadedChunks[triple].getName().Equals("air")) return;
Chunks.LoadedChunks[triple] = BlockMaterials.Materials["air"];
Chunks.SavedChunks[triple] = BlockMaterials.Materials["air"];
}
public static void UngenerateChunk(int x, int y) {
var startX = x << IRestrictions.ChunkShift;
var startY = y << IRestrictions.ChunkShift;
var endX = startX + IRestrictions.ChunkSize;
var endY = startY + IRestrictions.ChunkSize;
//Going from start of selected chunk to end of selected chunk in x and y
for (var i = startX; i != endX; i++) {
for (var j = startY; j != endY; j++) {
LoadedChunks.Remove(new Tuple<int,int,int>(i, j, 0));
}
}
//Second Layer
for (var i = startX; i != endX; i++){
for (var j = startY; j != endY; j++) {
var triple = new Tuple<int, int, int>(i, j,1);
LoadedChunks.Remove(triple);
}
}
}
public static void GenerateChunk(int x, int y) {
var startX = x << IRestrictions.ChunkShift;
var startY = y << IRestrictions.ChunkShift;
var endX = startX + IRestrictions.ChunkSize;
var endY = startY + IRestrictions.ChunkSize;
//Going from start of selected chunk to end of selected chunk in x and y
for (var i = startX; i != endX; i++) {
for (var j = startY; j != endY; j++) {
var triple = new Tuple<int, int, int>(i, j, 0);
if(SavedChunks[triple] != null){
LoadedChunks.Add(triple,SavedChunks[triple]);
}
else {
LoadedChunks.Add(triple, GetTerrain(i, j));
SavedChunks.Add(triple, GetTerrain(i,j));
}
}
}
//Second Layer
for (var i = startX; i != endX; i++){
for (var j = startY; j != endY; j++) {
var triple = new Tuple<int, int, int>(i, j,1);
LoadedChunks.Add(triple, GetBlocks(i,j,1));
SavedChunks.Add(triple, GetBlocks(i,j,1));
}
}
}
private static Block GetBlocks(int x, int y, int z) {
return SavedChunks.GetValueOrDefault(new Tuple<int, int, int>(x, y, z), new Block("air"));
}
private static double Noise1(double nx, double ny) {
return Gen1.Evaluate(nx, ny) / 2 + 0.5;
}
private static double Noise2(double nx, double ny) {
return Gen2.Evaluate(nx, ny) / 2 + 0.5;
}
private static Block GetTerrain(int x, int y) {
const double scale = 0.01;
var nx = x * scale;
var ny = y * scale;
var elevation = (0.17 * Noise1(1 * nx, 1 * ny)
+ 0.42 * Noise1(2 * nx, 2 * ny)
+ 0.16 * Noise1(4 * nx, 4 * ny)
+ 0.00 * Noise1(8 * nx, 8 * ny)
+ 0.00 * Noise1(16 * nx, 16 * ny)
+ 0.03 * Noise1(32 * nx, 32 * ny));
elevation /= (0.17+0.42+0.16+0.00+0.00+0.03);
elevation = Math.Pow(elevation, 3.00);
var moisture = (1.00 * Noise2( 1 * nx, 1 * ny)
+ 0.00 * Noise2( 2 * nx, 2 * ny)
+ 1.00 * Noise2( 4 * nx, 4 * ny)
+ 0.00 * Noise2( 8 * nx, 8 * ny)
+ 0.00 * Noise2(16 * nx, 16 * ny)
+ 0.00 * Noise2(32 * nx, 32 * ny));
moisture /= (0.00+1.00+0.00+0.00+0.00+0.00);
return GetBiomeBlock(moisture, elevation);
}
private static Block GetBiomeBlock(double moisture, double elevation) {
var biome = GetBiome(moisture, elevation);
if (biome.Equals("Tundra")) return BlockMaterials.Materials["snow"];
if (biome.Equals("Taiga")) return BlockMaterials.Materials["snow"];
if (biome.Equals("Snow")) return BlockMaterials.Materials["snow"];
if (biome.Equals("Grassland")) return BlockMaterials.Materials["grass"];
if (biome.Equals("Shrubland")) return BlockMaterials.Materials["grass"];
if (biome.Equals("TemperateDeciduousForest")) return BlockMaterials.Materials["grass"];
if (biome.Equals("TemperateRainForest")) return BlockMaterials.Materials["grass"];
if (biome.Equals("TropicalSeasonalForest")) return BlockMaterials.Materials["grass"];
if (biome.Equals("TropicalForest")) return BlockMaterials.Materials["grass"];
if (biome.Equals("TropicalRainForest")) return BlockMaterials.Materials["grass"];
if (biome.Equals("Beach")) return BlockMaterials.Materials["sand"];
if (biome.Equals("Bare")) return BlockMaterials.Materials["sand"];
if (biome.Equals("Scorched")) return BlockMaterials.Materials["sand"];
if (biome.Equals("TemperateDesert")) return BlockMaterials.Materials["sand"];
return biome.Equals("SubtropicalDesert") ? BlockMaterials.Materials["sand"] : BlockMaterials.Materials["water"];
}
private static string GetBiome(double moisture, double elevation) {
if (elevation < 0.1) return "Ocean";
if (elevation < 0.12) return "Beach";
if (elevation > 0.8) {
if (moisture < 0.1) return "Scorched";
if (moisture < 0.2) return "Bare";
return moisture < 0.5 ? "Tundra" : "Snow";
}
if (elevation > 0.6) {
if (moisture < 0.33) return "TemperateDesert";
return moisture < 0.66 ? "Shrubland" : "Taiga";
}
if (elevation > 0.3) {
if (moisture < 0.16) return "TemperateDesert";
if (moisture < 0.50) return "Grassland";
return moisture < 0.83 ? "TemperateDeciduousForest" : "TemperateRainForest";
}
if (moisture < 0.16) return "SubtropicalDesert";
if (moisture < 0.33) return "Grassland";
return moisture < 0.66 ? "TropicalSeasonalForest" : "TropicalRainForest";
}
public static bool IsEmpty(int x, int y){
return LoadedChunks[new Tuple<int,int,int>(x * 8, y * 8,0)] == null;
}
}
}

View File

@@ -0,0 +1,22 @@
using System.Collections.Generic;
using Collector.Dimension;
public class Inventory {
private LinkedList<ItemStack> inventory = new LinkedList<ItemStack>();
public LinkedList<ItemStack> GetInventory() {
return inventory;
}
public void SetInventory(LinkedList<ItemStack> inventory) {
this.inventory = inventory;
}
public void AddItem(Block block){
inventory.AddFirst((ItemStack) block);
}
public void RemoveItem(ItemStack itemStack){
inventory.Remove(itemStack);
}
}

View File

@@ -0,0 +1,14 @@
namespace Collector.Dimension
{
public class ItemStack: Block {
int value;
int quantity;
public ItemStack(string name, int value, int quantity) : base(name)
{
this.value = value;
this.quantity = quantity;
}
}
}

View File

@@ -0,0 +1,53 @@
using Collector;
using Collector.Dimension;
public class World {
public static void generateWorld(int x, int y) {
if (Chunks.IsEmpty(x, y)) {
Chunks.GenerateChunk(x, y);
}
}
public static void ungenerateWorld(int x, int y) {
if (!Chunks.IsEmpty(x, y)) {
Chunks.UngenerateChunk(x, y);
}
}
public static void loadChunks() {
for (var i = -(IRestrictions.RenderDistance); i < IRestrictions.RenderDistance; i++) {
for (var j = -(IRestrictions.RenderDistance); j < IRestrictions.RenderDistance; j++) {
generateWorld(
i + Player.getX() / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize),
j + Player.getY() / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)
);
}
}
}
public static void unloadChunks() {
for (var i = -IRestrictions.RenderDistance; i < IRestrictions.RenderDistance+1; i++) {
//Down
ungenerateWorld(
Player.getX() / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i,
(Player.getY() / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize))+3
);
//Up
ungenerateWorld(
Player.getX() / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i-1,
(Player.getY() / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize))-4
);
//Right
ungenerateWorld(
Player.getX() / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)-4,
(Player.getY() / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize))+i
);
//Left
ungenerateWorld(
Player.getX() / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+3,
(Player.getY() / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize))+i
);
}
}
}

View File

@@ -0,0 +1,51 @@
using Microsoft.Xna.Framework.Graphics;
namespace Collector.Dimension
{
public class WorldRenderer : IRestrictions {
//private Mouse mouse;
//private InputController inputController;
private Player _player;
/*
public WorldRenderer(InputController inputController, Mouse mouse, Player player) {
this.inputController = inputController;
this.mouse = mouse;
this.player = player;
}
*/
public void DrawWorld(SpriteBatch batch, int layer)
{
/*
foreach (var chunkpair in Chunks.LoadedChunks.Keys.Where(chunkpair => chunkpair.Item3 == layer))
{
batch.draw(
//BlockMaterials.Textures[Chunks.LoadedChunks[chunkpair].getName()],
chunkpair.Item1 << IRestrictions.TileShift,
chunkpair.Item2 << IRestrictions.TileShift
);
}
*/
}
/*
private void mouseCrosshair(SpriteBatch batch) {
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
Main.cam.unproject(mousePos);
int x = mouse.getSelectedX(mousePos);
int y = mouse.getSelectedY(mousePos);
batch.draw(mouse.getCrosshair(), x, y);
}
public void render(SpriteBatch batch, float timeSinceLastUpdate) {
//Higher means draws in a lower layer
drawWorld(batch,0);
batch.draw(player.getAnimation().getKeyFrame(timeSinceLastUpdate, true), Player.x, Player.y,TILE_SIZE,TILE_SIZE);
drawWorld(batch,1);
mouseCrosshair(batch);
inputController.handleInput();
batch.setProjectionMatrix(Main.cam.combined);
}
*/
}
}