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

@@ -3,6 +3,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp2.2</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@@ -15,7 +16,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="LegacyJava" /> <Folder Include="OLD" />
<Folder Include="src" />
</ItemGroup> </ItemGroup>
</Project> </Project>

22
Collector/Restrictions.cs Normal file
View File

@@ -0,0 +1,22 @@
using System;
using static Collector.IRestrictions;
namespace Collector
{
public interface IRestrictions
{
public const int Seed = 1;
public const float ViewportHeight = 9/2f;
public const float ViewportWidth = 16/2f;
public const int MovementSpeed = 16;
public const int FreeSpeed = 2;
public const int KeyDelay = 0;
public const int TileSize = 16;
public const int ChunkSize = 8;
public const int SuperChunkSize = 1;
public const int TileShift = 5;
public const int ChunkShift = 3;
public const int RenderDistance = 3;
public const float RenderTime = 1/60f;
}
}

View File

@@ -0,0 +1,113 @@
package Collector.Character;
import Collector.Main;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector3;
import Collector.Dimension.Chunks;
import Collector.Restrictions;
import java.util.HashMap;
public class InputController implements Restrictions {
private Player player;
private HashMap<String, Animation<TextureAtlas.AtlasRegion>> animations;
private Mouse mouse;
int i = 0;
public InputController(Player player, Mouse mouse) {
this.player = player;
this.mouse = mouse;
animations = new HashMap<>();
animations.put("Up", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "Up")));
animations.put("UpRight", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "UpRight")));
animations.put("UpLeft", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "UpLeft")));
animations.put("Down", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "Down")));
animations.put("DownRight", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "DownRight")));
animations.put("DownLeft", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "DownLeft")));
animations.put("Left", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "Left")));
animations.put("Right", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "Right")));
}
public void handleInput() {
i++;
if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
Main.cam.zoom += 5;
}
if (Gdx.input.isKeyPressed(Input.Keys.E)) {
Main.cam.zoom -= 5;
}
if ((Gdx.input.isButtonJustPressed(Input.Buttons.LEFT) || Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT) )&& i > KEY_DELAY) {
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
Main.cam.unproject(mousePos);
int x = mouse.getSelectedX(mousePos) >> TILE_SHIFT;
int y = mouse.getSelectedY(mousePos) >> TILE_SHIFT;
if(Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {
i = 0;
Chunks.placeBlock(x, y, "wood");
}
if (Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT)) {
i = 0;
Chunks.removeBlock(x,y);
}
}
if (Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
i = 0;
player.setAnimation(animations.get("UpLeft"));
Player.addX(-MOVEMENT_SPEED);
Player.addY(MOVEMENT_SPEED);
Main.cam.translate(-MOVEMENT_SPEED, MOVEMENT_SPEED);
}
else if (Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
i = 0;
player.setAnimation(animations.get("UpRight"));
Player.addX(MOVEMENT_SPEED);
Player.addY(MOVEMENT_SPEED);
Main.cam.translate(MOVEMENT_SPEED, MOVEMENT_SPEED);
}
else if (Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
i = 0;
player.setAnimation(animations.get("DownLeft"));
Player.addX(-MOVEMENT_SPEED);
Player.addY(-MOVEMENT_SPEED);
Main.cam.translate(-MOVEMENT_SPEED, -MOVEMENT_SPEED);
}
else if (Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
i = 0;
player.setAnimation(animations.get("DownRight"));
Player.addX(MOVEMENT_SPEED);
Player.addY(-MOVEMENT_SPEED);
Main.cam.translate(MOVEMENT_SPEED, -MOVEMENT_SPEED);
}
else if (Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
i = 0;
player.setAnimation(animations.get("Left"));
Player.addX(-MOVEMENT_SPEED);
Main.cam.translate(-MOVEMENT_SPEED, 0);
}
else if (Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
i = 0;
player.setAnimation(animations.get("Right"));
Player.addX(MOVEMENT_SPEED);
Main.cam.translate(MOVEMENT_SPEED, 0);
}
else if (Gdx.input.isKeyPressed(Input.Keys.S) && i > KEY_DELAY) {
i = 0;
player.setAnimation(animations.get("Down"));
Player.addY(-MOVEMENT_SPEED);
Main.cam.translate(0, -MOVEMENT_SPEED);
}
if (Gdx.input.isKeyPressed(Input.Keys.W) && i > KEY_DELAY) {
i = 0;
player.setAnimation(animations.get("Up"));
Player.addY(MOVEMENT_SPEED);
Main.cam.translate(0, MOVEMENT_SPEED);
}
Main.cam.update();
}
}

View File

@@ -0,0 +1,25 @@
package Collector.Character;
import Collector.Restrictions;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector3;
public class Mouse implements Restrictions {
Texture crosshair;
public Mouse() {
this.crosshair = new Texture("assets/crosshair.png");
}
public Texture getCrosshair() {
return crosshair;
}
public int getSelectedX(Vector3 mousePos) {
return ((int)(mousePos.x)>>4)<<4;
}
public int getSelectedY(Vector3 mousePos) {
return ((int)(mousePos.y)>>4)<<4;
}
}

View File

@@ -0,0 +1,64 @@
using Collector;
public class Player : IRestrictions {
private static int X;
private static int Y;
//private string SpriteName;
//private TextureAtlas textureAtlas;
//private Animation<TextureAtlas.AtlasRegion> animation;
private Inventory playerInventory;
public Player(int x, int y) {
//Player location
Player.X = x<<IRestrictions.TileShift;
Player.Y = y<<IRestrictions.TileShift;
//Player Inventory
playerInventory = new Inventory();
//Player animation
//spriteName = "man";
//textureAtlas = new TextureAtlas("man.atlas");
//Array<TextureAtlas.AtlasRegion> keyFrames = textureAtlas.findRegions(spriteName + "_Down");
//float frameDuration = 1 / 4f;
//animation = new Animation<>(frameDuration, keyFrames);
}
public static int getX() {
return X;
}
public static int getY() {
return Y;
}
public static void addX(int x){
Player.X += x;
}
public static void addY(int y){
Player.Y += y;
}
/*
public String getSpriteName() {
return spriteName;
}
public Animation<TextureAtlas.AtlasRegion> getAnimation() {
return animation;
}
public void setAnimation(Animation<TextureAtlas.AtlasRegion> animationTemp) {
animation = animationTemp;
}
public void dispose(){
//textureAtlas.dispose();
}
public TextureAtlas getTextureAtlas() {
return textureAtlas;
}
*/
}

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);
}
*/
}
}

File diff suppressed because one or more lines are too long

7
Collector/src/UI/GUI.cs Normal file
View File

@@ -0,0 +1,7 @@
using Collector;
public class GUI: IRestrictions {
}