Fixed the discrepancy between the screen coords and the game coords and also fixed mouse problem when zoomed out too far.
@@ -8,18 +8,32 @@
|
||||
|
||||
<ItemGroup>
|
||||
<MonoGameContentReference Include="**\*.mgcb" />
|
||||
<MonoGameContentReference Remove="OLD\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Humper" Version="0.5.8" />
|
||||
<PackageReference Include="MGCB.Linux" Version="3.5.1" />
|
||||
<PackageReference Include="MonoGame.Content.Builder" Version="3.7.0.9" />
|
||||
<PackageReference Include="MonoGame.Extended" Version="3.7.0" />
|
||||
<PackageReference Include="MonoGame.Extended.Collisions" Version="3.7.0" />
|
||||
<PackageReference Include="MonoGame.Framework.DesktopGL.Core" Version="3.8.0.13" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="OLD" />
|
||||
<Folder Include="src" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="OLD\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Remove="OLD\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="OLD\**" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -152,7 +152,7 @@
|
||||
/processorParam:ColorKeyEnabled=True
|
||||
/processorParam:GenerateMipmaps=False
|
||||
/processorParam:PremultiplyAlpha=True
|
||||
/processorParam:ResizeToPowerOfTwo=False
|
||||
/processorParam:ResizeToPowerOfTwo=True
|
||||
/processorParam:MakeSquare=False
|
||||
/processorParam:TextureFormat=Color
|
||||
/build:man.png
|
||||
|
||||
@@ -18,7 +18,9 @@ namespace Collector
|
||||
private InputController _inputController;
|
||||
private Player _player;
|
||||
private PlayerMouse _playerMouse;
|
||||
private OrthographicCamera _cam;
|
||||
private static OrthographicCamera _cam;
|
||||
public static int VirtualWidth;
|
||||
public static int VirtualHeight;
|
||||
private WorldRenderer WorldRenderer { get; set; }
|
||||
public static Dictionary<Blocks, Texture2D> Materials { get; } = new Dictionary<Blocks, Texture2D>();
|
||||
|
||||
@@ -41,10 +43,14 @@ namespace Collector
|
||||
Materials.Add(name,Content.Load<Texture2D>(name.ToString()));
|
||||
}
|
||||
|
||||
var viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, 800, 480);
|
||||
VirtualWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
|
||||
VirtualHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
|
||||
|
||||
var viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, VirtualWidth, VirtualHeight);
|
||||
_player = new Player(0, 0);
|
||||
_cam = new OrthographicCamera(viewportAdapter);
|
||||
_cam.LookAt(new Vector2(Player.X, Player.Y));
|
||||
_cam.LookAt(new Vector2(Player.X+IRestrictions.TileSize/2, Player.Y+IRestrictions.TileSize/2));
|
||||
_cam.Zoom = IRestrictions.Zoom;
|
||||
|
||||
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
||||
_playerMouse = new PlayerMouse(Content, _spriteBatch, _cam);
|
||||
|
||||
|
Before Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 1.6 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 2.1 KiB |
|
Before Width: | Height: | Size: 2.1 KiB |
|
Before Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
@@ -1,113 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package Collector.Character;
|
||||
|
||||
import Collector.Restrictions;
|
||||
import Collector.Dimension.Inventory;
|
||||
import com.badlogic.gdx.graphics.g2d.Animation;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
|
||||
public class Player implements Restrictions {
|
||||
public static int x;
|
||||
public 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<<TILE_SHIFT;
|
||||
Player.y = y<<TILE_SHIFT;
|
||||
|
||||
//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;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package Collector.Dimension;
|
||||
|
||||
public class Block {
|
||||
private final String name;
|
||||
|
||||
public Block(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package Collector.Dimension;
|
||||
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class BlockMaterials {
|
||||
public static HashMap<String,Block> materials = new HashMap<>();
|
||||
public static HashMap<String,Texture> textures = new HashMap<>();
|
||||
|
||||
//Private so the singleton can't be instantiated
|
||||
private BlockMaterials() {}
|
||||
|
||||
public static void create(){
|
||||
materials.put("grass",new Block("grass"));
|
||||
materials.put("wood",new Block("wood"));
|
||||
materials.put("water",new Block("water"));
|
||||
materials.put("stone",new Block("stone"));
|
||||
materials.put("snow",new Block("snow"));
|
||||
materials.put("sand",new Block("sand"));
|
||||
materials.put("air",new Block("air"));
|
||||
materials.put("roof",new Block("roof"));
|
||||
materials.put("wall",new Block("wall"));
|
||||
|
||||
for (String s:BlockMaterials.materials.keySet()) {
|
||||
textures.put(s,new Texture("assets/" + s + ".png"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
//https://www.redblobgames.com/maps/terrain-from-noise/
|
||||
package Collector.Dimension;
|
||||
|
||||
import com.github.czyzby.kiwi.util.tuple.immutable.Triple;
|
||||
import Collector.ThirdPartyCode.OpenSimplexNoise;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static Collector.Dimension.BlockMaterials.materials;
|
||||
import static Collector.Restrictions.*;
|
||||
|
||||
public class Chunks {
|
||||
public static HashMap<Triple<Integer, Integer, Integer>, Block> loadedChunks = new HashMap<>();
|
||||
public static HashMap<Triple<Integer, Integer,Integer>, Block> savedChunks = new HashMap<>();
|
||||
public static OpenSimplexNoise gen1 = new OpenSimplexNoise(SEED + 1);
|
||||
public static OpenSimplexNoise gen2 = new OpenSimplexNoise(SEED);
|
||||
|
||||
public static void createStructures() {
|
||||
int 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.put(new Triple<>(x, y, z), materials.get(name));
|
||||
savedChunks.put(new Triple<>(x, y, z), materials.get(name));
|
||||
}
|
||||
|
||||
public static void placeBlock(int x, int y, String name){
|
||||
Triple<Integer, Integer, Integer> triple = new Triple<>(x, y, 1);
|
||||
if(Chunks.loadedChunks.get(triple).getName().equals("air")) {
|
||||
Chunks.loadedChunks.replace(triple, Chunks.loadedChunks.get(triple), materials.get(name));
|
||||
Chunks.savedChunks.replace(triple, Chunks.savedChunks.get(triple), materials.get(name));
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeBlock(int x, int y){
|
||||
Triple<Integer, Integer, Integer> triple = new Triple<>(x, y, 1);
|
||||
if(!Chunks.loadedChunks.get(triple).getName().equals("air")) {
|
||||
Chunks.loadedChunks.replace(triple, Chunks.loadedChunks.get(triple), materials.get("air"));
|
||||
Chunks.savedChunks.replace(triple, Chunks.savedChunks.get(triple), materials.get("air"));
|
||||
}
|
||||
}
|
||||
|
||||
public static void ungenerateChunk(int x, int y) {
|
||||
int startX = x << CHUNK_SHIFT;
|
||||
int startY = y << CHUNK_SHIFT;
|
||||
int endX = startX + CHUNK_SIZE;
|
||||
int endY = startY + CHUNK_SIZE;
|
||||
|
||||
//Going from start of selected chunk to end of selected chunk in x and y
|
||||
for (int i = startX; i != endX; i++) {
|
||||
for (int j = startY; j != endY; j++) {
|
||||
loadedChunks.remove(new Triple<>(i, j, 0), getTerrain(i, j));
|
||||
}
|
||||
}
|
||||
//Second Layer
|
||||
for (int i = startX; i != endX; i++){
|
||||
for (int j = startY; j != endY; j++) {
|
||||
Triple<Integer,Integer,Integer> triple = new Triple<>(i, j,1);
|
||||
loadedChunks.remove(triple, loadedChunks.get(triple));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void generateChunk(int x, int y) {
|
||||
int startX = x << CHUNK_SHIFT;
|
||||
int startY = y << CHUNK_SHIFT;
|
||||
int endX = startX + CHUNK_SIZE;
|
||||
int endY = startY + CHUNK_SIZE;
|
||||
|
||||
//Going from start of selected chunk to end of selected chunk in x and y
|
||||
for (int i = startX; i != endX; i++) {
|
||||
for (int j = startY; j != endY; j++) {
|
||||
Triple<Integer, Integer, Integer> triple = new Triple<>(i, j, 0);
|
||||
if(savedChunks.get(triple) != null){
|
||||
loadedChunks.put(triple,savedChunks.get(triple));
|
||||
}
|
||||
else {
|
||||
loadedChunks.put(triple, getTerrain(i, j));
|
||||
savedChunks.put(triple, getTerrain(i,j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Second Layer
|
||||
for (int i = startX; i != endX; i++){
|
||||
for (int j = startY; j != endY; j++) {
|
||||
Triple<Integer,Integer,Integer> triple = new Triple<>(i, j,1);
|
||||
loadedChunks.put(triple, getBlocks(i,j,1));
|
||||
savedChunks.put(triple, getBlocks(i,j,1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Block getBlocks(int x, int y, int z) {
|
||||
return savedChunks.getOrDefault(new Triple<>(x, y, z), new Block("air"));
|
||||
}
|
||||
|
||||
public static double noise1(double nx, double ny) {
|
||||
return gen1.eval(nx, ny) / 2 + 0.5;
|
||||
}
|
||||
|
||||
public static double noise2(double nx, double ny) {
|
||||
return gen2.eval(nx, ny) / 2 + 0.5;
|
||||
}
|
||||
|
||||
public static Block getTerrain(int x, int y) {
|
||||
double moisture, elevation, nx, ny;
|
||||
double scale = 0.01;
|
||||
nx = x * scale;
|
||||
ny = y * scale;
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
public static Block getBiomeBlock(double moisture, double elevation) {
|
||||
String biome = getBiome(moisture, elevation);
|
||||
if (biome.equals("Tundra")) return materials.get("snow");
|
||||
if (biome.equals("Taiga")) return materials.get("snow");
|
||||
if (biome.equals("Snow")) return materials.get("snow");
|
||||
|
||||
if (biome.equals("Grassland")) return materials.get("grass");
|
||||
if (biome.equals("Shrubland")) return materials.get("grass");
|
||||
if (biome.equals("TemperateDeciduousForest")) return materials.get("grass");
|
||||
|
||||
if (biome.equals("TemperateRainForest")) return materials.get("grass");
|
||||
if (biome.equals("TropicalSeasonalForest")) return materials.get("grass");
|
||||
if (biome.equals("TropicalForest")) return materials.get("grass");
|
||||
if (biome.equals("TropicalRainForest")) return materials.get("grass");
|
||||
|
||||
if (biome.equals("Beach")) return materials.get("sand");
|
||||
if (biome.equals("Bare")) return materials.get("sand");
|
||||
if (biome.equals("Scorched")) return materials.get("sand");
|
||||
if (biome.equals("TemperateDesert")) return materials.get("sand");
|
||||
if (biome.equals("SubtropicalDesert")) return materials.get("sand");
|
||||
|
||||
return materials.get("water");
|
||||
}
|
||||
|
||||
public 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";
|
||||
if (moisture < 0.5) return "Tundra";
|
||||
return "Snow";
|
||||
}
|
||||
|
||||
if (elevation > 0.6) {
|
||||
if (moisture < 0.33) return "TemperateDesert";
|
||||
if (moisture < 0.66) return "Shrubland";
|
||||
return "Taiga";
|
||||
}
|
||||
|
||||
if (elevation > 0.3) {
|
||||
if (moisture < 0.16) return "TemperateDesert";
|
||||
if (moisture < 0.50) return "Grassland";
|
||||
if (moisture < 0.83) return "TemperateDeciduousForest";
|
||||
return "TemperateRainForest";
|
||||
}
|
||||
|
||||
if (moisture < 0.16) return "SubtropicalDesert";
|
||||
if (moisture < 0.33) return "Grassland";
|
||||
if (moisture < 0.66) return "TropicalSeasonalForest";
|
||||
return "TropicalRainForest";
|
||||
}
|
||||
|
||||
public static Boolean isEmpty(int x, int y){
|
||||
return loadedChunks.get(new Triple<>(x * 8, y * 8,0)) == null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package Collector.Dimension;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
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.add((ItemStack) block);
|
||||
}
|
||||
|
||||
public void removeItem(ItemStack itemStack){
|
||||
inventory.remove(itemStack);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package Collector.Dimension;
|
||||
|
||||
import Collector.Dimension.Block;
|
||||
|
||||
public class ItemStack extends Block {
|
||||
int value;
|
||||
int quantity;
|
||||
|
||||
public ItemStack(String name, int value, int quantity) {
|
||||
super(name);
|
||||
this.value = value;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package Collector.Dimension;
|
||||
|
||||
import Collector.Character.Player;
|
||||
|
||||
import static Collector.Restrictions.*;
|
||||
|
||||
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 (int i = -(RENDER_DISTANCE); i < RENDER_DISTANCE; i++) {
|
||||
for (int j = -(RENDER_DISTANCE); j < RENDER_DISTANCE; j++) {
|
||||
generateWorld(
|
||||
i + Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE),
|
||||
j + Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void unloadChunks() {
|
||||
for (int i = -RENDER_DISTANCE; i < RENDER_DISTANCE+1; i++) {
|
||||
//Down
|
||||
ungenerateWorld(
|
||||
Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)+i,
|
||||
(Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE))+3
|
||||
);
|
||||
//Up
|
||||
ungenerateWorld(
|
||||
Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)+i-1,
|
||||
(Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE))-4
|
||||
);
|
||||
//Right
|
||||
ungenerateWorld(
|
||||
Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)-4,
|
||||
(Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE))+i
|
||||
);
|
||||
//Left
|
||||
ungenerateWorld(
|
||||
Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)+3,
|
||||
(Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE))+i
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package Collector.Dimension;
|
||||
|
||||
import Collector.Character.Player;
|
||||
import Collector.Main;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.scenes.scene2d.Stage;
|
||||
import com.github.czyzby.kiwi.util.tuple.immutable.Triple;
|
||||
import Collector.Character.InputController;
|
||||
import Collector.Character.Mouse;
|
||||
import Collector.Restrictions;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class WorldRenderer implements Restrictions {
|
||||
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) {
|
||||
for (Triple<Integer, Integer, Integer> chunkpair : Chunks.loadedChunks.keySet()) {
|
||||
if (chunkpair.getThird() == layer) {
|
||||
batch.draw(
|
||||
BlockMaterials.textures.get(Chunks.loadedChunks.get(chunkpair).getName()),
|
||||
chunkpair.getFirst() << TILE_SHIFT,
|
||||
chunkpair.getSecond() << TILE_SHIFT
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
package Collector;
|
||||
|
||||
import Collector.Dimension.*;
|
||||
import com.badlogic.gdx.ApplicationAdapter;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.GL30;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.scenes.scene2d.Stage;
|
||||
import com.badlogic.gdx.utils.viewport.ExtendViewport;
|
||||
import Collector.Character.InputController;
|
||||
import Collector.Character.Mouse;
|
||||
import Collector.Character.Player;
|
||||
import Collector.UI.GUI;
|
||||
|
||||
import static Collector.Restrictions.*;
|
||||
|
||||
public class Main extends ApplicationAdapter {
|
||||
private Stage stage;
|
||||
private SpriteBatch batch;
|
||||
private WorldRenderer worldRenderer;
|
||||
private GUI gui;
|
||||
private ExtendViewport extendViewport;
|
||||
public static float chunkLoadingTime = 0;
|
||||
float playerAnimationTime = 0f; //accumulator
|
||||
public static OrthographicCamera cam;
|
||||
private InputController inputController;
|
||||
private Mouse mouse;
|
||||
private Player player;
|
||||
|
||||
|
||||
@Override
|
||||
public void create () {
|
||||
Gdx.graphics.setWindowedMode(1024, 576);
|
||||
|
||||
//Declaring all objects needed for the game
|
||||
cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
|
||||
extendViewport = new ExtendViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, cam);
|
||||
mouse = new Mouse();
|
||||
player = new Player(0, 0);
|
||||
inputController = new InputController(player,mouse);
|
||||
worldRenderer = new WorldRenderer(inputController,mouse,player);
|
||||
batch = new SpriteBatch();
|
||||
stage = new Stage();
|
||||
gui = new GUI(stage);
|
||||
|
||||
Gdx.input.setInputProcessor(stage);
|
||||
|
||||
BlockMaterials.create();
|
||||
Chunks.createStructures();
|
||||
|
||||
//cam.translate(TILE_SIZE, TILE_SIZE);
|
||||
cam.zoom = 25f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(int width, int height) {
|
||||
stage.getViewport().update(width, height, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render () {
|
||||
float deltaTime = Gdx.graphics.getDeltaTime();
|
||||
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
playerAnimationTime += deltaTime;
|
||||
chunkLoadingTime += deltaTime;
|
||||
|
||||
gui.debugInfo(stage,mouse);
|
||||
|
||||
stage.act(deltaTime);
|
||||
batch.begin();
|
||||
worldRenderer.render(batch,playerAnimationTime);
|
||||
|
||||
if (chunkLoadingTime > RENDER_TIME) {
|
||||
chunkLoadingTime -= RENDER_TIME;
|
||||
World.loadChunks();
|
||||
World.unloadChunks();
|
||||
}
|
||||
|
||||
cam.update();
|
||||
batch.end();
|
||||
stage.draw();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void dispose () {
|
||||
gui.dispose();
|
||||
player.dispose();
|
||||
stage.dispose();
|
||||
batch.dispose();
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package Collector;
|
||||
|
||||
public interface Restrictions {
|
||||
int SEED = 1;
|
||||
|
||||
float VIEWPORT_HEIGHT = 9/2f;
|
||||
float VIEWPORT_WIDTH = 16/2f;
|
||||
|
||||
int MOVEMENT_SPEED = 16;
|
||||
int FREE_SPEED = 2;
|
||||
int KEY_DELAY = 0;
|
||||
|
||||
int TILE_SIZE = 32;
|
||||
int CHUNK_SIZE = 8;
|
||||
int SUPER_CHUNK_SIZE = 1;
|
||||
|
||||
int TILE_SHIFT = (int)(Math.log(TILE_SIZE)/Math.log(2));
|
||||
int CHUNK_SHIFT = (int)(Math.log(CHUNK_SIZE)/Math.log(2));
|
||||
//int SUPER_CHUNK_SHIFT = (int)(Math.log(SUPER_CHUNK_SIZE)/Math.log(2));
|
||||
|
||||
int RENDER_DISTANCE = 3;
|
||||
float RENDER_TIME = 1/60f;
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package Collector.UI;
|
||||
|
||||
import Collector.Character.Mouse;
|
||||
import Collector.Main;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.scenes.scene2d.Stage;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Label;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Table;
|
||||
import com.badlogic.gdx.utils.Align;
|
||||
import com.badlogic.gdx.utils.viewport.ExtendViewport;
|
||||
import Collector.Restrictions;
|
||||
|
||||
public class GUI implements Restrictions {
|
||||
private final BitmapFont font;
|
||||
private ExtendViewport guiViewport;
|
||||
private final Table table;
|
||||
private final Label label;
|
||||
|
||||
public GUI(Stage stage) {
|
||||
font = new BitmapFont();
|
||||
table = new Table();
|
||||
table.setDebug(true);
|
||||
guiViewport = new ExtendViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),stage.getCamera());
|
||||
stage.setViewport(guiViewport);
|
||||
|
||||
//Creating block selection debug menu
|
||||
|
||||
table.top().left();
|
||||
label = new Label("x=N/A y=N/A", new Label.LabelStyle(font,Color.WHITE));
|
||||
table.add(label);
|
||||
|
||||
//Creating Minimap
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void debugInfo(Stage stage, Mouse mouse){
|
||||
table.setFillParent(true);
|
||||
stage.addActor(table);
|
||||
|
||||
//Mouse position
|
||||
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;
|
||||
label.setText("x="+ x + " y=" + y);
|
||||
}
|
||||
|
||||
public void dispose(){
|
||||
font.dispose();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package Collector.UI;
|
||||
|
||||
public class InventoryInterface {
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package Collector.UI;
|
||||
|
||||
public class MiniMap {
|
||||
|
||||
}
|
||||
@@ -5,9 +5,9 @@ namespace Collector
|
||||
public static class Program
|
||||
{
|
||||
[STAThread]
|
||||
static void Main()
|
||||
private static void Main()
|
||||
{
|
||||
using (var game = new Main())
|
||||
using var game = new Main();
|
||||
game.Run();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
using System;
|
||||
using Collector.Character;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using static Collector.IRestrictions;
|
||||
|
||||
namespace Collector
|
||||
@@ -6,17 +9,17 @@ 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 TileSize = 1;
|
||||
public const float ViewportHeight = 9f;
|
||||
public const float ViewportWidth = 16f;
|
||||
public const int MovementSpeed = 1;
|
||||
public const int FreeSpeed = 2;
|
||||
public const int KeyDelay = 0;
|
||||
public const int TileSize = 32;
|
||||
public const int KeyDelay = 20;
|
||||
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 int RenderDistance = 5;
|
||||
public const float RenderTime = 1/60f;
|
||||
public const float Zoom = 30;
|
||||
public const float Scale = ViewportWidth/ViewportHeight;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Collector;
|
||||
using Collector.Character;
|
||||
using Collector.Dimension;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
@@ -9,8 +6,10 @@ using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using MonoGame.Extended;
|
||||
|
||||
public class InputController : IRestrictions
|
||||
namespace Collector.Character
|
||||
{
|
||||
public class InputController : IRestrictions
|
||||
{
|
||||
private readonly PlayerMouse _playerMouse;
|
||||
private readonly OrthographicCamera _cam;
|
||||
private readonly Dictionary<string, Rectangle[]> _animations;
|
||||
@@ -29,72 +28,73 @@ public class InputController : IRestrictions
|
||||
_spriteBatch = spriteBatch;
|
||||
_frameNumber = 0;
|
||||
_texture = contentManager.Load<Texture2D>("man");
|
||||
const int tileSize = 32;
|
||||
_animations = new Dictionary<string, Rectangle[]>
|
||||
{
|
||||
["Up"] = new[]
|
||||
{
|
||||
new Rectangle(32 * 0, 0, 32, 32),
|
||||
new Rectangle(32 * 1, 0, 32, 32),
|
||||
new Rectangle(32 * 2, 0, 32, 32),
|
||||
new Rectangle(32 * 3, 0, 32, 32)
|
||||
new Rectangle(0, 0, tileSize, tileSize),
|
||||
new Rectangle(1, 0, tileSize, tileSize),
|
||||
new Rectangle(2, 0, tileSize, tileSize),
|
||||
new Rectangle(3, 0, tileSize, tileSize)
|
||||
},
|
||||
["Right"] = new[]
|
||||
{
|
||||
new Rectangle(32 * 0 + 32 * 4, 32, 32, 32),
|
||||
new Rectangle(32 * 1 + 32 * 4, 32, 32, 32),
|
||||
new Rectangle(32 * 2 + 32 * 4, 32, 32, 32),
|
||||
new Rectangle(32 * 3 + 32 * 4, 32, 32, 32)
|
||||
new Rectangle(0 + 4, 1, tileSize, tileSize),
|
||||
new Rectangle(1 + 4, 1, tileSize, tileSize),
|
||||
new Rectangle(2 + 4, 1, tileSize, tileSize),
|
||||
new Rectangle(3 + 4, 1, tileSize, tileSize)
|
||||
},
|
||||
["UpRight"] = new[]
|
||||
{
|
||||
new Rectangle(32 * 0 + 32 * 4, 32 * 2, 32, 32),
|
||||
new Rectangle(32 * 1 + 32 * 4, 32 * 2, 32, 32),
|
||||
new Rectangle(32 * 2 + 32 * 4, 32 * 2, 32, 32),
|
||||
new Rectangle(32 * 3 + 32 * 4, 32 * 2, 32, 32)
|
||||
new Rectangle(0 + 4, 2, tileSize, tileSize),
|
||||
new Rectangle(1 + 4, 2, tileSize, tileSize),
|
||||
new Rectangle(2 + 4, 2, tileSize, tileSize),
|
||||
new Rectangle(3 + 4, 2, tileSize, tileSize)
|
||||
},
|
||||
["DownRight"] = new[]
|
||||
{
|
||||
new Rectangle(32 * 0 + 32 * 4, 32 * 3, 32, 32),
|
||||
new Rectangle(32 * 1 + 32 * 4, 32 * 3, 32, 32),
|
||||
new Rectangle(32 * 2 + 32 * 4, 32 * 3, 32, 32),
|
||||
new Rectangle(32 * 3 + 32 * 4, 32 * 3, 32, 32)
|
||||
new Rectangle(0 + 4, 3, tileSize, tileSize),
|
||||
new Rectangle(1 + 4, 3, tileSize, tileSize),
|
||||
new Rectangle(2 + 4, 3, tileSize, tileSize),
|
||||
new Rectangle(3 + 4, 3, tileSize, tileSize)
|
||||
},
|
||||
["Left"] = new[]
|
||||
{
|
||||
new Rectangle(32 * 0, 32, 32, 32),
|
||||
new Rectangle(32 * 1, 32, 32, 32),
|
||||
new Rectangle(32 * 2, 32, 32, 32),
|
||||
new Rectangle(32 * 3, 32, 32, 32)
|
||||
new Rectangle(0, 1, tileSize, tileSize),
|
||||
new Rectangle(1, 1, tileSize, tileSize),
|
||||
new Rectangle(2, 1, tileSize, tileSize),
|
||||
new Rectangle(3, 1, tileSize, tileSize)
|
||||
},
|
||||
["UpLeft"] = new[]
|
||||
{
|
||||
new Rectangle(32 * 0, 32 * 2, 32, 32),
|
||||
new Rectangle(32 * 1, 32 * 2, 32, 32),
|
||||
new Rectangle(32 * 2, 32 * 2, 32, 32),
|
||||
new Rectangle(32 * 3, 32 * 2, 32, 32)
|
||||
new Rectangle(0, 2, tileSize, tileSize),
|
||||
new Rectangle(1, 2, tileSize, tileSize),
|
||||
new Rectangle(2, 2, tileSize, tileSize),
|
||||
new Rectangle(3, 2, tileSize, tileSize)
|
||||
},
|
||||
["DownLeft"] = new[]
|
||||
{
|
||||
new Rectangle(32 * 0, 32, 32, 32),
|
||||
new Rectangle(32 * 1, 32, 32, 32),
|
||||
new Rectangle(32 * 2, 32, 32, 32),
|
||||
new Rectangle(32 * 3, 32, 32, 32)
|
||||
new Rectangle(0, 1, tileSize, tileSize),
|
||||
new Rectangle(1, 1, tileSize, tileSize),
|
||||
new Rectangle(2, 1, tileSize, tileSize),
|
||||
new Rectangle(3, 1, tileSize, tileSize)
|
||||
},
|
||||
["Down"] = new[]
|
||||
{
|
||||
new Rectangle(32 * 0 + 32 * 4, 0, 32, 32),
|
||||
new Rectangle(32 * 1 + 32 * 4, 0, 32, 32),
|
||||
new Rectangle(32 * 2 + 32 * 4, 0, 32, 32),
|
||||
new Rectangle(32 * 3 + 32 * 4, 0, 32, 32)
|
||||
new Rectangle(0 + 4, 0, tileSize, tileSize),
|
||||
new Rectangle(1 + 4, 0, tileSize, tileSize),
|
||||
new Rectangle(2 + 4, 0, tileSize, tileSize),
|
||||
new Rectangle(3 + 4, 0, tileSize, tileSize)
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public void PlayerInput(Main main, PlayerMouse playerMouse, GameTime gameTime)
|
||||
{
|
||||
const int movementSpeed = IRestrictions.MovementSpeed;
|
||||
var keyboardState = Keyboard.GetState();
|
||||
var mouseState = Mouse.GetState();
|
||||
const int movementSpeed = IRestrictions.MovementSpeed;
|
||||
if (keyboardState.IsKeyDown(Keys.Escape))
|
||||
{
|
||||
Quit(main);
|
||||
@@ -110,78 +110,66 @@ public class InputController : IRestrictions
|
||||
{
|
||||
UpdateAnimationFrame(gameTime);
|
||||
Input = "UpRight";
|
||||
Player.Y += -movementSpeed;
|
||||
Player.X += movementSpeed;
|
||||
_cam.Move(new Vector2(movementSpeed, -movementSpeed));
|
||||
Player.Move(_cam,movementSpeed,-movementSpeed);
|
||||
}
|
||||
else if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.A))
|
||||
{
|
||||
UpdateAnimationFrame(gameTime);
|
||||
Input = "UpLeft";
|
||||
Player.Y += -movementSpeed;
|
||||
Player.X += -movementSpeed;
|
||||
_cam.Move(new Vector2(-movementSpeed, -movementSpeed));
|
||||
Player.Move(_cam,-movementSpeed,-movementSpeed);
|
||||
}
|
||||
else if (keyboardState.IsKeyDown(Keys.S) && keyboardState.IsKeyDown(Keys.D))
|
||||
{
|
||||
UpdateAnimationFrame(gameTime);
|
||||
Input = "DownRight";
|
||||
Player.Y += movementSpeed;
|
||||
Player.X += movementSpeed;
|
||||
_cam.Move(new Vector2(movementSpeed, movementSpeed));
|
||||
Player.Move(_cam,movementSpeed,movementSpeed);
|
||||
}
|
||||
else if (keyboardState.IsKeyDown(Keys.S) && keyboardState.IsKeyDown(Keys.A))
|
||||
{
|
||||
UpdateAnimationFrame(gameTime);
|
||||
Input = "DownLeft";
|
||||
Player.Y += movementSpeed;
|
||||
Player.X += -movementSpeed;
|
||||
_cam.Move(new Vector2(-movementSpeed, movementSpeed));
|
||||
Player.Move(_cam,-movementSpeed,movementSpeed);
|
||||
}
|
||||
else if (keyboardState.IsKeyDown(Keys.W))
|
||||
{
|
||||
UpdateAnimationFrame(gameTime);
|
||||
Input = "Up";
|
||||
Player.Y += -movementSpeed;
|
||||
_cam.Move(new Vector2(0, -movementSpeed));
|
||||
Player.Move(_cam,0,-movementSpeed);
|
||||
}
|
||||
else if (keyboardState.IsKeyDown(Keys.A))
|
||||
{
|
||||
UpdateAnimationFrame(gameTime);
|
||||
Player.X += -movementSpeed;
|
||||
_cam.Move(new Vector2(-movementSpeed, 0));
|
||||
Player.Move(_cam,-movementSpeed,0);
|
||||
Input = "Left";
|
||||
}
|
||||
else if (keyboardState.IsKeyDown(Keys.S))
|
||||
{
|
||||
UpdateAnimationFrame(gameTime);
|
||||
Player.Y += movementSpeed;
|
||||
_cam.Move(new Vector2(0, movementSpeed));
|
||||
Player.Move(_cam,0,movementSpeed);
|
||||
Input = "Down";
|
||||
}
|
||||
else if (keyboardState.IsKeyDown(Keys.D))
|
||||
{
|
||||
UpdateAnimationFrame(gameTime);
|
||||
Player.X += movementSpeed;
|
||||
_cam.Move(new Vector2(movementSpeed, 0));
|
||||
Player.Move(_cam,movementSpeed,0);
|
||||
Input = "Right";
|
||||
}
|
||||
if (keyboardState.IsKeyDown(Keys.Q))
|
||||
{
|
||||
_cam.ZoomIn(0.01f);
|
||||
_cam.ZoomIn(1f);
|
||||
}
|
||||
if (keyboardState.IsKeyDown(Keys.E))
|
||||
{
|
||||
_cam.ZoomOut(0.01f);
|
||||
_cam.ZoomOut(1f);
|
||||
}
|
||||
|
||||
if (mouseState.LeftButton == ButtonState.Pressed)
|
||||
{
|
||||
Chunks.RemoveBlock(playerMouse.GetSelectedXTile(),playerMouse.GetSelectedYTile());
|
||||
Chunks.RemoveBlock(PlayerMouse.GetSelectedX(),PlayerMouse.GetSelectedY());
|
||||
}
|
||||
if (mouseState.RightButton == ButtonState.Pressed)
|
||||
{
|
||||
Chunks.PlaceBlock(playerMouse.GetSelectedXTile(),playerMouse.GetSelectedYTile(),Blocks.BlockWood);
|
||||
Chunks.PlaceBlock(PlayerMouse.GetSelectedX(),PlayerMouse.GetSelectedY(),Blocks.BlockWood);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,6 +192,12 @@ public class InputController : IRestrictions
|
||||
|
||||
public void Draw()
|
||||
{
|
||||
_spriteBatch.Draw(_texture, new Vector2(Player.X, Player.Y), _animations[Input][_frameNumber], Color.White);
|
||||
_spriteBatch.Draw(
|
||||
_texture
|
||||
,Player.PlayerBounds
|
||||
,_animations[Input][_frameNumber]
|
||||
,Color.White
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Collector.Dimension;
|
||||
using Humper;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Media;
|
||||
using MonoGame.Extended;
|
||||
using MonoGame.Extended.Collections;
|
||||
using Collision = Collector.Dimension.Collision;
|
||||
|
||||
namespace Collector.Character
|
||||
{
|
||||
public class Player : IRestrictions
|
||||
{
|
||||
public static int X { get; set; }
|
||||
public static int Y { get; set; }
|
||||
public Dictionary<string,Rectangle> Animation { get; set; }
|
||||
//private Inventory PlayerInventory { get; }
|
||||
public static int X { get; private set; }
|
||||
public static int Y { get; private set; }
|
||||
private static Dictionary<string, Rectangle> Animation { get; set; }
|
||||
public static Rectangle PlayerBounds;
|
||||
|
||||
public Player(int x, int y) {
|
||||
X = x<<IRestrictions.TileShift;
|
||||
Y = y<<IRestrictions.TileShift;
|
||||
//PlayerInventory = new Inventory();
|
||||
public Player(int x, int y)
|
||||
{
|
||||
PlayerBounds = new Rectangle(X,Y,IRestrictions.TileSize,IRestrictions.TileSize);
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
|
||||
public static void Move(OrthographicCamera cam, int x, int y)
|
||||
{
|
||||
X += x;
|
||||
Y += y;
|
||||
cam.Move(new Vector2(x+IRestrictions.TileSize/2, y+IRestrictions.TileSize/2));
|
||||
PlayerBounds.Location = new Point(X,Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
@@ -9,7 +10,7 @@ namespace Collector.Character
|
||||
public class PlayerMouse : IRestrictions
|
||||
{
|
||||
private readonly SpriteBatch _spriteBatch;
|
||||
private readonly OrthographicCamera _cam;
|
||||
private static OrthographicCamera _cam;
|
||||
private Texture2D Crosshair { get; set; }
|
||||
|
||||
public PlayerMouse(ContentManager contentManager, SpriteBatch spriteBatch, OrthographicCamera cam)
|
||||
@@ -18,30 +19,24 @@ namespace Collector.Character
|
||||
_cam = cam;
|
||||
Crosshair = contentManager.Load<Texture2D>("crosshair");
|
||||
}
|
||||
|
||||
public void Draw()
|
||||
{
|
||||
_spriteBatch.Draw(Crosshair,new Vector2(GetSelectedX(),GetSelectedY()), Color.White);
|
||||
_spriteBatch.Draw(
|
||||
Crosshair,
|
||||
new Rectangle(GetSelectedX(),GetSelectedY(),1,1),
|
||||
new Rectangle(0,0,32,32),
|
||||
Color.White
|
||||
);
|
||||
}
|
||||
|
||||
public int GetSelectedX()
|
||||
public static int GetSelectedX()
|
||||
{
|
||||
return ((int) (Mouse.GetState().X + _cam.Position.X) >> 5) << 5;
|
||||
return (int) Math.Floor(_cam.ScreenToWorld(Mouse.GetState().X,Mouse.GetState().Y).X);
|
||||
}
|
||||
|
||||
public int GetSelectedY()
|
||||
public static int GetSelectedY()
|
||||
{
|
||||
return ((int) (Mouse.GetState().Y + _cam.Position.Y) >> 5) << 5;
|
||||
}
|
||||
|
||||
public int GetSelectedXTile()
|
||||
{
|
||||
return (int) (Mouse.GetState().X + _cam.Position.X) >> 5;
|
||||
}
|
||||
|
||||
public int GetSelectedYTile()
|
||||
{
|
||||
return (int) (Mouse.GetState().Y + _cam.Position.Y) >> 5;
|
||||
return (int) Math.Floor(_cam.ScreenToWorld(Mouse.GetState().X,Mouse.GetState().Y).Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Collector.ThirdPartyCode;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Humper;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Collector.Dimension
|
||||
{
|
||||
@@ -11,6 +12,9 @@ namespace Collector.Dimension
|
||||
{
|
||||
public static Dictionary<Tuple<int, int, int>, Blocks> LoadedChunks { get; } = new Dictionary<Tuple<int, int, int>, Blocks>();
|
||||
private static readonly Dictionary<Tuple<int, int, int>, Blocks> SavedChunks = new Dictionary<Tuple<int, int, int>, Blocks>();
|
||||
private static readonly Dictionary<Tuple<int, int>, Collision> savedCollisions = new Dictionary<Tuple<int, int>, Collision>();
|
||||
public static readonly Dictionary<Tuple<int,int>, Collision> loadedCollisions = new Dictionary<Tuple<int, int>, Collision>();
|
||||
|
||||
|
||||
private static readonly OpenSimplexNoise Gen1 = new OpenSimplexNoise(IRestrictions.Seed+ 1);
|
||||
private static readonly OpenSimplexNoise Gen2 = new OpenSimplexNoise(IRestrictions.Seed);
|
||||
@@ -25,28 +29,37 @@ namespace Collector.Dimension
|
||||
|
||||
private static void SetBlock(int x, int y, int z, Blocks name)
|
||||
{
|
||||
LoadedChunks[new Tuple<int, int, int>(x, y, z)] = name;
|
||||
SavedChunks[new Tuple<int,int,int>(x, y, z)] = name;
|
||||
var tuple = new Tuple<int, int, int>(x, y, z);
|
||||
var pair = new Tuple<int, int>(x, y);
|
||||
|
||||
loadedCollisions[pair] = new Collision(x,y,IRestrictions.TileSize,false);
|
||||
savedCollisions[pair] = new Collision(x,y,IRestrictions.TileSize,false);
|
||||
LoadedChunks[tuple] = name;
|
||||
SavedChunks[tuple] = name;
|
||||
}
|
||||
|
||||
public static void PlaceBlock(int x, int y, Blocks name)
|
||||
{
|
||||
var triple = new Tuple<int, int, int>(x, y, 1);
|
||||
if (!LoadedChunks.ContainsKey(triple)) return;
|
||||
if (LoadedChunks[triple].Equals(Blocks.BlockAir))
|
||||
var tuple = new Tuple<int, int, int>(x, y, 1);
|
||||
if (!LoadedChunks.ContainsKey(tuple)) return;
|
||||
if (LoadedChunks[tuple].Equals(Blocks.BlockAir))
|
||||
{
|
||||
SetBlock(x, y, 1, name);
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveBlock(int x, int y){
|
||||
var triple = new Tuple<int, int, int>(x, y, 1);
|
||||
var tuple = new Tuple<int, int, int>(x, y, 1);
|
||||
var pair = new Tuple<int,int>(x,y);
|
||||
|
||||
if (!LoadedChunks.ContainsKey(triple)) return;
|
||||
if (LoadedChunks[triple].Equals(Blocks.BlockAir)) return;
|
||||
if (!LoadedChunks.ContainsKey(tuple)) return;
|
||||
if (LoadedChunks[tuple].Equals(Blocks.BlockAir)) return;
|
||||
|
||||
LoadedChunks[triple] = Blocks.BlockAir;
|
||||
SavedChunks[triple] = Blocks.BlockAir;
|
||||
loadedCollisions[pair] = null;
|
||||
savedCollisions[pair] = null;
|
||||
|
||||
LoadedChunks[tuple] = Blocks.BlockAir;
|
||||
SavedChunks[tuple] = Blocks.BlockAir;
|
||||
}
|
||||
|
||||
public static void UngenerateChunk(int x, int y) {
|
||||
|
||||
18
Collector/src/Dimension/Collision.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Collector.Dimension
|
||||
{
|
||||
public class Collision
|
||||
{
|
||||
private readonly int _tileWidth;
|
||||
private bool _entity;
|
||||
public Rectangle Rectangle { get; private set; }
|
||||
|
||||
public Collision(int x, int y,int tileWidth, bool entity)
|
||||
{
|
||||
_entity = entity;
|
||||
_tileWidth = tileWidth;
|
||||
Rectangle = new Rectangle(x,y,tileWidth,tileWidth);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,8 @@ using Collector.Character;
|
||||
namespace Collector.Dimension
|
||||
{
|
||||
public static class World {
|
||||
private static void GenerateWorld(int x, int y) {
|
||||
private static void GenerateWorld(int x, int y)
|
||||
{
|
||||
if (Chunks.IsEmpty(x, y)) {
|
||||
Chunks.GenerateChunk(x, y);
|
||||
}
|
||||
@@ -21,8 +22,8 @@ namespace Collector.Dimension
|
||||
for (var i = -(IRestrictions.RenderDistance); i < IRestrictions.RenderDistance; i++) {
|
||||
for (var j = -(IRestrictions.RenderDistance); j < IRestrictions.RenderDistance; j++) {
|
||||
GenerateWorld(
|
||||
i + Player.X / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize),
|
||||
j + Player.Y / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)
|
||||
i + Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize),
|
||||
j + Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -32,23 +33,23 @@ namespace Collector.Dimension
|
||||
for (var i = -IRestrictions.RenderDistance; i < IRestrictions.RenderDistance+1; i++) {
|
||||
//Down
|
||||
UngenerateWorld(
|
||||
Player.X / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i,
|
||||
(Player.Y / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize))+3
|
||||
Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i,
|
||||
(Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize))+IRestrictions.RenderDistance
|
||||
);
|
||||
//Up
|
||||
UngenerateWorld(
|
||||
Player.X / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i-1,
|
||||
(Player.Y / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize))-4
|
||||
Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i,
|
||||
(Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize))-IRestrictions.RenderDistance
|
||||
);
|
||||
//Right
|
||||
UngenerateWorld(
|
||||
Player.X / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)-4,
|
||||
(Player.Y / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize))+i
|
||||
Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)-IRestrictions.RenderDistance,
|
||||
(Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize))+i
|
||||
);
|
||||
//Left
|
||||
UngenerateWorld(
|
||||
Player.X / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+3,
|
||||
(Player.Y / (IRestrictions.TileSize * IRestrictions.SuperChunkSize * IRestrictions.ChunkSize))+i
|
||||
Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+IRestrictions.RenderDistance,
|
||||
(Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize))+i
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,8 +27,8 @@ namespace Collector.Dimension
|
||||
batch.Draw(
|
||||
Main.Materials[Chunks.LoadedChunks[chunkpair]],
|
||||
new Rectangle(
|
||||
chunkpair.Item1 << IRestrictions.TileShift,
|
||||
chunkpair.Item2 << IRestrictions.TileShift,
|
||||
chunkpair.Item1,
|
||||
chunkpair.Item2,
|
||||
IRestrictions.TileSize, IRestrictions.TileSize
|
||||
),
|
||||
Color.White
|
||||
|
||||
@@ -1,320 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
//Quadtree code
|
||||
//https://github.com/futurechris/QuadTree
|
||||
|
||||
namespace QuadTree
|
||||
{
|
||||
|
||||
public class QuadTree<T>
|
||||
{
|
||||
private static Stack<Branch> _branchPool = new Stack<Branch>();
|
||||
private static Stack<Leaf> _leafPool = new Stack<Leaf>();
|
||||
|
||||
private readonly Branch _root;
|
||||
private readonly int _splitCount;
|
||||
private readonly int _depthLimit;
|
||||
private readonly Dictionary<T, Leaf> _leafLookup = new Dictionary<T, Leaf>();
|
||||
|
||||
|
||||
public QuadTree(int splitCount, int depthLimit, ref Quad region)
|
||||
{
|
||||
_splitCount = splitCount;
|
||||
_depthLimit = depthLimit;
|
||||
_root = CreateBranch(this, null, 0, ref region);
|
||||
}
|
||||
|
||||
public QuadTree(int splitCount, int depthLimit, Quad region)
|
||||
: this(splitCount, depthLimit, ref region)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public QuadTree(int splitCount, int depthLimit, float x, float y, float width, float height)
|
||||
: this(splitCount, depthLimit, new Quad(x, y, x + width, y + height))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_root.Clear();
|
||||
_root.Tree = this;
|
||||
_leafLookup.Clear();
|
||||
}
|
||||
|
||||
|
||||
public static void ClearPools()
|
||||
{
|
||||
_branchPool = new Stack<Branch>();
|
||||
_leafPool = new Stack<Leaf>();
|
||||
}
|
||||
|
||||
|
||||
private void Insert(T value, ref Quad quad)
|
||||
{
|
||||
if (!_leafLookup.TryGetValue(value, out var leaf))
|
||||
{
|
||||
leaf = CreateLeaf(value, ref quad);
|
||||
_leafLookup.Add(value, leaf);
|
||||
}
|
||||
_root.Insert(leaf);
|
||||
}
|
||||
|
||||
public void Insert(T value, Quad quad)
|
||||
{
|
||||
Insert(value, ref quad);
|
||||
}
|
||||
|
||||
public void Insert(T value, float x, float y, float width, float height)
|
||||
{
|
||||
var quad = new Quad(x, y, x + width, y + height);
|
||||
Insert(value, ref quad);
|
||||
}
|
||||
|
||||
private bool SearchArea(ref Quad quad, ref List<T> values)
|
||||
{
|
||||
if (values != null)
|
||||
values.Clear();
|
||||
else
|
||||
values = new List<T>();
|
||||
_root.SearchQuad(ref quad, values);
|
||||
return values.Count > 0;
|
||||
}
|
||||
|
||||
public bool SearchArea(Quad quad, ref List<T> values)
|
||||
{
|
||||
return SearchArea(ref quad, ref values);
|
||||
}
|
||||
|
||||
public bool SearchArea(float x, float y, float width, float height, ref List<T> values)
|
||||
{
|
||||
var quad = new Quad(x, y, x + width, y + height);
|
||||
return SearchArea(ref quad, ref values);
|
||||
}
|
||||
|
||||
public bool SearchPoint(float x, float y, ref List<T> values)
|
||||
{
|
||||
if (values != null)
|
||||
values.Clear();
|
||||
else
|
||||
values = new List<T>();
|
||||
_root.SearchPoint(x, y, values);
|
||||
return values.Count > 0;
|
||||
}
|
||||
|
||||
public bool FindCollisions(T value, ref List<T> values)
|
||||
{
|
||||
if (values != null)
|
||||
values.Clear();
|
||||
else
|
||||
values = new List<T>(_leafLookup.Count);
|
||||
|
||||
if (!_leafLookup.TryGetValue(value, out var leaf)) return false;
|
||||
var branch = leaf.Branch;
|
||||
|
||||
//Add the leaf's siblings (prevent it from colliding with itself)
|
||||
if (branch.Leaves.Count > 0)
|
||||
foreach (var t in branch.Leaves)
|
||||
if (leaf != t && leaf.Quad.Intersects(ref t.Quad))
|
||||
values.Add(t.Value);
|
||||
|
||||
//Add the branch's children
|
||||
if (branch.Split)
|
||||
for (var i = 0; i < 4; ++i)
|
||||
if (branch.Branches[i] != null)
|
||||
branch.Branches[i].SearchQuad(ref leaf.Quad, values);
|
||||
|
||||
//Add all leaves back to the root
|
||||
branch = branch.Parent;
|
||||
while (branch != null)
|
||||
{
|
||||
if (branch.Leaves.Count > 0)
|
||||
foreach (var t in branch.Leaves)
|
||||
if (leaf.Quad.Intersects(ref t.Quad))
|
||||
values.Add(t.Value);
|
||||
|
||||
branch = branch.Parent;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int CountBranches()
|
||||
{
|
||||
var count = 0;
|
||||
CountBranches(_root, ref count);
|
||||
return count;
|
||||
}
|
||||
|
||||
private static void CountBranches(Branch branch, ref int count)
|
||||
{
|
||||
++count;
|
||||
if (!branch.Split) return;
|
||||
for (var i = 0; i < 4; ++i)
|
||||
if (branch.Branches[i] != null)
|
||||
CountBranches(branch.Branches[i], ref count);
|
||||
}
|
||||
|
||||
private static Branch CreateBranch(QuadTree<T> tree, Branch parent, int branchDepth, ref Quad quad)
|
||||
{
|
||||
var branch = _branchPool.Count > 0 ? _branchPool.Pop() : new Branch();
|
||||
branch.Tree = tree;
|
||||
branch.Parent = parent;
|
||||
branch.Split = false;
|
||||
branch.Depth = branchDepth;
|
||||
var midX = quad.MinX + (quad.MaxX - quad.MinX) * 0.5f;
|
||||
var midY = quad.MinY + (quad.MaxY - quad.MinY) * 0.5f;
|
||||
branch.Quads[0].Set(quad.MinX, quad.MinY, midX, midY);
|
||||
branch.Quads[1].Set(midX, quad.MinY, quad.MaxX, midY);
|
||||
branch.Quads[2].Set(midX, midY, quad.MaxX, quad.MaxY);
|
||||
branch.Quads[3].Set(quad.MinX, midY, midX, quad.MaxY);
|
||||
return branch;
|
||||
}
|
||||
|
||||
private static Leaf CreateLeaf(T value, ref Quad quad)
|
||||
{
|
||||
var leaf = _leafPool.Count > 0 ? _leafPool.Pop() : new Leaf();
|
||||
leaf.Value = value;
|
||||
leaf.Quad = quad;
|
||||
return leaf;
|
||||
}
|
||||
|
||||
public class Branch
|
||||
{
|
||||
internal QuadTree<T> Tree;
|
||||
internal Branch Parent;
|
||||
internal readonly Quad[] Quads = new Quad[4];
|
||||
internal readonly Branch[] Branches = new Branch[4];
|
||||
internal readonly List<Leaf> Leaves = new List<Leaf>();
|
||||
internal bool Split;
|
||||
internal int Depth;
|
||||
|
||||
internal void Clear()
|
||||
{
|
||||
Tree = null;
|
||||
Parent = null;
|
||||
Split = false;
|
||||
|
||||
for (var i = 0; i < 4; ++i)
|
||||
{
|
||||
if (Branches[i] == null) continue;
|
||||
_branchPool.Push(Branches[i]);
|
||||
Branches[i].Clear();
|
||||
Branches[i] = null;
|
||||
}
|
||||
|
||||
foreach (var t in Leaves)
|
||||
{
|
||||
_leafPool.Push(t);
|
||||
t.Branch = null;
|
||||
t.Value = default;
|
||||
}
|
||||
|
||||
Leaves.Clear();
|
||||
}
|
||||
|
||||
internal void Insert(Leaf leaf)
|
||||
{
|
||||
//If this branch is already split
|
||||
if (Split)
|
||||
{
|
||||
for (var i = 0; i < 4; ++i)
|
||||
{
|
||||
if (!Quads[i].Contains(ref leaf.Quad)) continue;
|
||||
Branches[i] ??= CreateBranch(Tree, this, Depth + 1, ref Quads[i]);
|
||||
Branches[i].Insert(leaf);
|
||||
return;
|
||||
}
|
||||
|
||||
Leaves.Add(leaf);
|
||||
leaf.Branch = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
//Add the leaf to this node
|
||||
Leaves.Add(leaf);
|
||||
leaf.Branch = this;
|
||||
|
||||
//Once I have reached capacity, split the node
|
||||
if (Leaves.Count >= Tree._splitCount && Depth < Tree._depthLimit)
|
||||
{
|
||||
Split = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void SearchQuad(ref Quad quad, List<T> values)
|
||||
{
|
||||
if (Leaves.Count > 0)
|
||||
foreach (var t in Leaves)
|
||||
if (quad.Intersects(ref t.Quad))
|
||||
values.Add(t.Value);
|
||||
|
||||
for (var i = 0; i < 4; ++i)
|
||||
if (Branches[i] != null)
|
||||
Branches[i].SearchQuad(ref quad, values);
|
||||
}
|
||||
|
||||
internal void SearchPoint(float x, float y, List<T> values)
|
||||
{
|
||||
if (Leaves.Count > 0) values.AddRange(from t in Leaves where t.Quad.Contains(x, y) select t.Value);
|
||||
|
||||
for (var i = 0; i < 4; ++i)
|
||||
if (Branches[i] != null)
|
||||
Branches[i].SearchPoint(x, y, values);
|
||||
}
|
||||
}
|
||||
|
||||
internal class Leaf
|
||||
{
|
||||
internal Branch Branch;
|
||||
internal T Value;
|
||||
internal Quad Quad;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public struct Quad
|
||||
{
|
||||
public float MinX;
|
||||
public float MinY;
|
||||
public float MaxX;
|
||||
public float MaxY;
|
||||
|
||||
public Quad(float minX, float minY, float maxX, float maxY)
|
||||
{
|
||||
MinX = minX;
|
||||
MinY = minY;
|
||||
MaxX = maxX;
|
||||
MaxY = maxY;
|
||||
}
|
||||
|
||||
public void Set(float minX, float minY, float maxX, float maxY)
|
||||
{
|
||||
MinX = minX;
|
||||
MinY = minY;
|
||||
MaxX = maxX;
|
||||
MaxY = maxY;
|
||||
}
|
||||
|
||||
|
||||
public bool Intersects(ref Quad other)
|
||||
{
|
||||
return MinX < other.MaxX && MinY < other.MaxY && MaxX > other.MinX && MaxY > other.MinY;
|
||||
}
|
||||
|
||||
public bool Contains(ref Quad other)
|
||||
{
|
||||
return other.MinX >= MinX && other.MinY >= MinY && other.MaxX <= MaxX && other.MaxY <= MaxY;
|
||||
}
|
||||
|
||||
public bool Contains(float x, float y)
|
||||
{
|
||||
return x > MinX && y > MinY && x < MaxX && y < MaxY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||