added basic worldgen

This commit is contained in:
MasterGordon 2022-12-21 18:10:26 +01:00
parent 6cd5852cd1
commit b1dbad6dd4
8 changed files with 126 additions and 37 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 634 B

After

Width:  |  Height:  |  Size: 797 B

View File

@ -23,7 +23,7 @@ public class FontManager
var (ptr, size) = this.resourceLoader.LoadToIntPtr(path);
var sdlBuffer = SDL_RWFromConstMem(ptr, size);
var font = TTF_OpenFontRW(sdlBuffer, 1, fontSize);
var font = TTF_OpenFontRW(sdlBuffer, 1, fontSize+4);
if (font == IntPtr.Zero)
{
throw new SDLException(TTF_GetError());

View File

@ -41,7 +41,7 @@ public class Move
}
}
}
[Interaction(InteractorKind.Client, PacketType.Tick)]
public static void SelfMovedClient()
{

View File

@ -30,7 +30,7 @@ public class ChunkGenerator
{
for (var j = 0; j < Constants.ChunkSize; j++)
{
fill.Id = (int)wg.GetRandomOreAt(j + y);
fill.Id = (int)wg.GetRandomOreAt(j + y*32);
chunk.SetTile(i, j, fill);
}
}

View File

@ -6,85 +6,109 @@ using Mine2d.game.core.tiles;
namespace Mine2d.game.core.world;
public class GenerationSettings {
public class GenerationSettings
{
public int xOffset { get; set; }
public int yOffset { get; set; }
public Tiles tile { get; set; }
public int GetWeight(int height) {
return (int)((-Math.Pow(height - this.xOffset, 2)*0.01) + this.yOffset + (32*10));
public int GetWeight(int height)
{
return (int)((-Math.Pow(height - (this.xOffset + (32 * 10)), 2) * 0.001) + this.yOffset);
}
}
public class WorldGenerator
{
private readonly List<GenerationSettings> settings = new ();
private readonly List<GenerationSettings> settings = new();
public WorldGenerator() {
this.settings.Add(new GenerationSettings {
public WorldGenerator()
{
this.settings.Add(new GenerationSettings
{
xOffset = 10,
yOffset = 15,
tile = Tiles.IronOre
});
this.settings.Add(new GenerationSettings {
this.settings.Add(new GenerationSettings
{
xOffset = 15,
yOffset = 20,
tile = Tiles.CopperOre
});
this.settings.Add(new GenerationSettings {
this.settings.Add(new GenerationSettings
{
xOffset = 20,
yOffset = 10,
tile = Tiles.TinOre
});
this.settings.Add(new GenerationSettings {
this.settings.Add(new GenerationSettings
{
xOffset = 40,
yOffset = 3,
tile = Tiles.SilverOre
});
this.settings.Add(new GenerationSettings {
xOffset = 40,
this.settings.Add(new GenerationSettings
{
xOffset = 80,
yOffset = 3,
tile = Tiles.GoldOre
});
this.settings.Add(new GenerationSettings {
xOffset = 50,
this.settings.Add(new GenerationSettings
{
xOffset = 90,
yOffset = 15,
tile = Tiles.LeadOre
});
this.settings.Add(new GenerationSettings {
xOffset = 60,
yOffset = 2,
this.settings.Add(new GenerationSettings
{
xOffset = 160,
yOffset = 3,
tile = Tiles.PlatinumOre
});
this.settings.Add(new GenerationSettings {
xOffset = 60,
this.settings.Add(new GenerationSettings
{
xOffset = 160,
yOffset = 3,
tile = Tiles.CobaltOre
});
this.settings.Add(new GenerationSettings {
xOffset = 65,
this.settings.Add(new GenerationSettings
{
xOffset = 165,
yOffset = 10,
tile = Tiles.TungstenOre
});
this.settings.Add(new GenerationSettings {
xOffset = 90,
this.settings.Add(new GenerationSettings
{
xOffset = 290,
yOffset = 1,
tile = Tiles.DiamondOre
});
this.settings.Add(new GenerationSettings {
xOffset = 94,
this.settings.Add(new GenerationSettings
{
xOffset = 294,
yOffset = 2,
tile = Tiles.UraniumOre
});
}
public Tiles GetRandomOreAt(int height) {
public Tiles GetRandomOreAt(int height)
{
var random = new Random();
var weight = random.Next(0, 4000);
var ore = this.settings.FirstOrDefault(x => x.GetWeight(height) > weight, null);
if(ore == null) {
return Tiles.Stone;
var ores = new List<Tiles>();
foreach (var setting in this.settings)
{
for (var i = 0; i < setting.GetWeight(height); i++)
{
ores.Add(setting.tile);
}
}
return ore.tile;
var rng = random.Next(0, 700);
if (rng < ores.Count)
{
return ores[rng];
}
return Tiles.Stone;
}
}

View File

@ -110,12 +110,23 @@ namespace Mine2d.game.frontend.inventory
{
player.Inventory.SwapWithCursor(index, hotbar);
}
if (e.button.button == SDL_BUTTON_RIGHT)
{
if (player.Inventory.Cursor == null)
{
player.Inventory.TakeHalf(index, hotbar);
}
else
{
player.Inventory.DropOne(index, hotbar);
}
}
}
// is inventory
if (cursorPosition.X >= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.X <= this.x + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 9 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.Y >= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.Y <= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 5 * Context.Get().FrontendGameState.Settings.UiScale)
&& cursorPosition.Y <= this.y + (4 * Context.Get().FrontendGameState.Settings.UiScale) + (21 * 6 * Context.Get().FrontendGameState.Settings.UiScale)
)
{
var player = PlayerEntity.GetSelf();
@ -126,6 +137,17 @@ namespace Mine2d.game.frontend.inventory
{
player.Inventory.SwapWithCursor(index, inventory);
}
if (e.button.button == SDL_BUTTON_RIGHT)
{
if (player.Inventory.Cursor == null)
{
player.Inventory.TakeHalf(index, inventory);
}
else
{
player.Inventory.DropOne(index, inventory);
}
}
}
}
}

View File

@ -27,8 +27,8 @@ public class FrontendGameState
public class Settings
{
public int GameScale { get; set; } = 4;
public int UiScale { get; set; } = 3;
public int GameScale { get; set; } = 6;
public int UiScale { get; set; } = 4;
public bool ShowCollision { get; set; } = true;
public bool Fullscreen { get; set; } = false;
}

View File

@ -34,6 +34,49 @@ public class PlayerInventory
public void SwapWithCursor(int slot, ItemStack[] inventory)
{
(inventory[slot], this.Cursor) = (this.Cursor, inventory[slot]);
if (this.Cursor != null && inventory[slot] != null && this.Cursor.Id == inventory[slot].Id)
{
inventory[slot].Count += this.Cursor.Count;
this.Cursor = null;
}
else
{
(inventory[slot], this.Cursor) = (this.Cursor, inventory[slot]);
}
}
public void TakeHalf(int slot, ItemStack[] inventory)
{
if (inventory[slot] == null)
return;
if(inventory[slot].Count == 1)
{
this.Cursor = inventory[slot];
inventory[slot] = null;
return;
}
this.Cursor = new ItemStack(inventory[slot].Id, inventory[slot].Count / 2);
inventory[slot].Count -= this.Cursor.Count;
}
public void DropOne(int slot, ItemStack[] inventory)
{
if (inventory[slot] == null)
{
inventory[slot] = new ItemStack(this.Cursor.Id, 1);
}
else if (inventory[slot].Id == this.Cursor.Id)
{
inventory[slot].Count++;
}
else
{
return;
}
this.Cursor.Count--;
if (this.Cursor.Count == 0)
{
this.Cursor = null;
}
}
}