Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Server/Components/Pages/GamePage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
@using Fracture.Server.Modules.MapGenerator.UI.Models
@using Fracture.Server.Modules.Pathfinding.Models
@using Fracture.Server.Modules.Shared
@using Fracture.Server.Modules.Shared.ImageChangers
@using Fracture.Server.Modules.Users
@using Fracture.Server.Modules.Users.Services
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage
@implements IDisposable
@page "/game"

@rendermode @(new InteractiveServerRenderMode(false))
Expand All @@ -26,8 +24,9 @@
@inject NavigationManager NavigationManager
@inject IItemsRepository ItemsRepository
@inject VersionInfoProvider VersionInfoProvider
@inject BackgroundImageChanger BackgroundImageChanger
@inject UserService UserService
@inject MovementService MovementService
@inject ILogger<GamePage> Logger

<link rel="icon" type="image/png" href="assets/icons/favicon.png" />

Expand Down Expand Up @@ -68,8 +67,8 @@
<div class="statManager ">
</div>
<div class="dialogueWindowBg"
style="background: url(@BackgroundImage.ImagePath);background-repeat: no-repeat; background-size: cover; ">
<div class="dialogueWindow" @onkeydown="GetBacgroundAsync">
style="background: url(@BackgroundImage);background-repeat: no-repeat; background-size: cover; ">
<div class="dialogueWindow">
Fracture @VersionInfoProvider.InformationalVersion<br />
tutaj log mojej gry<br />
<PopupContainer @ref="_popup"></PopupContainer>
Expand Down Expand Up @@ -132,7 +131,7 @@
</div>
</div>
</div>
<MapView Map="Map" MapDisplayData="_mapDisplayData" Path="Path"></MapView>
<MapView Map="MovementService.CurrentMap" MapDisplayOptions="_mapDisplayOptions" Path="Path" CharacterX="MovementService.CurrentX" CharacterY="MovementService.CurrentY"></MapView>
</div>
<div class="settings">
<div class="settingsBtn">
Expand Down
127 changes: 104 additions & 23 deletions Server/Components/Pages/GamePage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
using Fracture.Server.Modules.MapGenerator.Models.Map;
using Fracture.Server.Modules.MapGenerator.UI.Models;
using Fracture.Server.Modules.Pathfinding.Models;
using Fracture.Server.Modules.Shared.ImageChangers;
using Fracture.Server.Modules.Users.Models;
using Fracture.Server.Modules.Users.Services;

namespace Fracture.Server.Components.Pages;

public partial class GamePage
{
private Dictionary<string, object> _mapPopupParameters = null!;
public static Map Map { get; set; }

private PopupContainer _popup = null!;

public static BackgroundImage BackgroundImage { get; set; } = new(string.Empty);
public string BackgroundImage { get; set; } = string.Empty;

private readonly MapDisplayData _mapDisplayData = new();
private readonly MapDisplayOptions _mapDisplayOptions = new();

private List<IPathfindingNode>? Path { get; set; }

Expand All @@ -28,18 +27,29 @@ protected override async Task OnInitializedAsync()
NavigationManager.NavigateTo("/");
}

Map = MapManagerService.GetWorldMap() ?? throw new InvalidOperationException();
_mapDisplayData.ShowColorMap = true;
_mapPopupParameters = new Dictionary<string, object>
if (MovementService.CurrentMap is null)
{
MovementService.Initialize();
BackgroundImage = GetBackgroundImagePath();
}

MovementService.OnMapEntered += async (sender, args) =>
{
{ "Map", Map },
{ "MapDisplayData", _mapDisplayData },
BackgroundImage = GetBackgroundImagePath();
StateHasChanged();
};

BackgroundImageChanger.BackgroundImage = BackgroundImage;
await GetBacgroundAsync();
MovementService.OnMoved += async (sender, args) =>
{
BackgroundImage = GetBackgroundImagePath();
StateHasChanged();
};

BackgroundImageChanger.BackgroundImageChanged += OnBgChanged!;
_mapDisplayOptions.ShowColorMap = true;
_mapPopupParameters = new Dictionary<string, object>
{
{ "MapDisplayData", _mapDisplayOptions },
};

await base.OnInitializedAsync();
}
Expand Down Expand Up @@ -74,19 +84,90 @@ private void Logout()
ProtectedSessionStore.DeleteAsync("username");
}

//this piece of codes refreshes GamePage after changing background image. It is necessary to show new background.
private async Task GetBacgroundAsync()
private string GetBackgroundImagePath()
{
await BackgroundImageChanger.ChangeBackgroundImageAsync();
}
if (MovementService.CurrentMap is null)
{
Logger.LogError("Current map is null");
return string.Empty;
}

private void OnBgChanged(object sender, EventArgs e)
{
this.InvokeAsync(this.StateHasChanged);
}
if (
MovementService.CurrentX < 0
|| MovementService.CurrentY < 0
|| MovementService.CurrentX >= MovementService.CurrentMap.Width
|| MovementService.CurrentY >= MovementService.CurrentMap.Height
)
{
Logger.LogError("Character is out of map");
return string.Empty;
}
var cell = MovementService.CurrentMap.Grid[
MovementService.CurrentX,
MovementService.CurrentY
];
var biome = cell.Biome;

public void Dispose()
{
BackgroundImageChanger.BackgroundImageChanged -= OnBgChanged!;
if (biome is null)
{
Logger.LogError("Biome is null");
return string.Empty;
}

string? imagePath = null;
if (cell.LocationType != LocationType.None)
{
var currentLocationName = cell.LocationType.ToString();
var location = biome.Locations.FirstOrDefault(l =>
string.Equals(l.Name, currentLocationName, StringComparison.OrdinalIgnoreCase)
);

if (location is null)
{
Logger.LogWarning(
"No matching location config for LocationType {LocationType} at ({X},{Y}) in biome {BiomeName}. Available: {Locations}",
cell.LocationType,
MovementService.CurrentX,
MovementService.CurrentY,
biome.Name,
string.Join(
", ",
biome
.Locations.Where(l => !string.IsNullOrWhiteSpace(l.Name))
.Select(l => l.Name)
)
);
}
else
{
imagePath = location.BackgroundImage;
Logger.LogDebug(
"Using location background image '{ImagePath}' for LocationType {LocationType} at ({X},{Y})",
imagePath,
cell.LocationType,
MovementService.CurrentX,
MovementService.CurrentY
);
}
}
if (string.IsNullOrWhiteSpace(imagePath))
{
imagePath = biome.BackgroundImage;
Logger.LogDebug(
"Using biome background image '{ImagePath}' for biome {BiomeName} at ({X},{Y})",
imagePath,
biome.Name,
MovementService.CurrentX,
MovementService.CurrentY
);
}

if (string.IsNullOrWhiteSpace(imagePath))
{
Logger.LogError("Background image path is null or empty");
return string.Empty;
}

return imagePath;
}
}
13 changes: 6 additions & 7 deletions Server/Components/Popups/MapPopup.razor
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
@inject NavigationManager NavigationManager
@inject MovementService MovementService

@page "/map"
@using Fracture.Server.Modules.MapGenerator.Models.Map
@using Fracture.Server.Modules.MapGenerator.UI
@using Fracture.Server.Modules.MapGenerator.UI.Models
@using Fracture.Server.Modules.Users.Services


<div class="row">
<MapView Map="Map" MapDisplayData="MapDisplayData" Path="null" IsMiniMap="false" CharacterX="x"
CharacterY="y"></MapView>
<MapView Map="MovementService.CurrentMap" MapDisplayOptions="MapDisplayData" Path="null" IsMiniMap="false"></MapView>
</div>

@code {
[Parameter] public required Map Map { get; set; }
[Parameter] public required MapDisplayData MapDisplayData { get; set; }
readonly int x = MapView.CharacterXX;
readonly int y = MapView.CharacterYY;
@code {
[Parameter] public required MapDisplayOptions MapDisplayData { get; set; }
}
2 changes: 1 addition & 1 deletion Server/Modules/Items/Models/ItemStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ItemStatistics

[InverseProperty(nameof(Item.Statistics))]
[JsonIgnore]
public virtual Item Item { get; set; }
public virtual required Item Item { get; set; }

public int GetStatFromItemStat(ItemStat stat)
{
Expand Down
2 changes: 1 addition & 1 deletion Server/Modules/MapGenerator/Models/Map/Biome/Biome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public class Biome
public float MinTemperature { get; set; }
public float MaxTemperature { get; set; }
public bool Walkable { get; set; } = true;
public string BackgroundImage { get; set; }
public string? BackgroundImage { get; set; }
public List<Location> Locations { get; set; } = new();
}
9 changes: 4 additions & 5 deletions Server/Modules/MapGenerator/Models/Map/Biome/BiomeCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ namespace Fracture.Server.Modules.MapGenerator.Models.Map.Biome;

public class BiomeCategory
{
public string BiomeType { get; set; }
public string TerrainType { get; set; }
public required string BiomeType { get; set; }
public required string TerrainType { get; set; }

public string BackgroundImage { get; set; }
public string? BackgroundImage { get; set; }

public float MaxHeight { get; set; }
public float MinHeight { get; set; }

[Required]
public List<Biome> Biomes { get; set; }
public required List<Biome> Biomes { get; set; } = [];
}
4 changes: 2 additions & 2 deletions Server/Modules/MapGenerator/Models/Map/Biome/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public class Location
{
public string? Name { get; set; } // name of the location what you want to generate for example town
public string? Name { get; set; } // name of the location what you want to generate, for example: town
public int Weight { get; set; }
public float Mult { get; set; }
public string BackgroundImage { get; set; }
public string? BackgroundImage { get; set; }
}
23 changes: 18 additions & 5 deletions Server/Modules/MapGenerator/Models/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ public class Map
{
public string? Name { get; set; }

public LocationType LocationType { get; set; }
public LocationType LocationType { get; init; }

public List<LocationGroup> LocationGroups { get; set; } = new();
public List<LocationGroup> LocationGroups { get; init; } = [];

public int Width { get; set; }
public int Height { get; set; }
public Node[,] Grid { get; set; }
public int Width { get; init; }
public int Height { get; init; }
public required Node[,] Grid { get; init; }

public Position GetRandomWalkableNode()
{
var rnd = new Random();

Position node;
do
{
node = new Position() { X = rnd.Next(0, Width), Y = rnd.Next(0, Height) };
} while (!Grid[node.X, node.Y].Walkable);

return node;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public interface IMapObject
{
string Type { get; set; }
string Id { get; set; }
bool IsInteractive { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
public class LocationMapObject : IMapObject
{
public Map? SubMap { get; set; }
public string Type { get; set; } = "Location";

public string Id { get; set; } = "Location";

public bool IsInteractive { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public class NpcMapObject : IMapObject
{
public string Type { get; set; }
public required string Id { get; set; }
public bool IsInteractive { get; set; }
}
28 changes: 7 additions & 21 deletions Server/Modules/MapGenerator/Models/Map/Node.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
using System.ComponentModel.DataAnnotations;
using Fracture.Server.Modules.MapGenerator.Models.Map.Biome;
using Fracture.Server.Modules.MapGenerator.Models.Map.MapObjects;
using Fracture.Server.Modules.MapGenerator.Models.Map.MapObjects;
using Fracture.Server.Modules.Pathfinding.Models;

namespace Fracture.Server.Modules.MapGenerator.Models.Map;

public class Node : IPathfindingNode
{
public Node(int xId, int yId, Biome.Biome biome)
{
XId = xId;
YId = yId;
Biome = biome;
}
public required Biome.Biome Biome { get; set; }

[Required]
public Biome.Biome Biome { get; set; }
public required string TerrainType { get; set; }

[Required]
public string TerrainType { get; set; }

[Required]
public float NoiseValue { get; set; }

public string GroupName { get; set; }
public string? GroupName { get; set; }

public LocationType LocationType { get; set; } = LocationType.None;
public IMapObject MapObject { get; set; }
public IMapObject? MapObject { get; set; }

[Required]
public int XId { get; set; }
public required int XId { get; set; }

[Required]
public int YId { get; set; }
public required int YId { get; set; }

public int GCost { get; set; }
public int HCost { get; set; }
Expand Down
7 changes: 7 additions & 0 deletions Server/Modules/MapGenerator/Models/Map/Position.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Fracture.Server.Modules.MapGenerator.Models.Map;

public record struct Position(int X, int Y)
{
public int X { get; init; } = X;
public int Y { get; init; } = Y;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Text.Json;
using Fracture.Server.Modules.MapGenerator.Models.Map.Biome;

namespace Fracture.Server.Modules.MapGenerator.Models.Map.Town;
namespace Fracture.Server.Modules.MapGenerator.Models.Map.Town;

public class LocationParameters
{
Expand Down
Loading
Loading