Skip to content

Commit

Permalink
Added visual chat divider
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerolmed committed Nov 18, 2024
1 parent 22a33a1 commit 209c934
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 46 deletions.
1 change: 1 addition & 0 deletions Core/Lib/Scenes/Ingame/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Core.Scenes.Ingame.Battle;
using Core.Scenes.Ingame.Localization;
using Core.Scenes.Ingame.Modes.Battle;
using Core.Scenes.Ingame.Modes.Overworld;
using Core.States;
using Core.Utils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Microsoft.Xna.Framework;
using Newtonsoft.Json;

namespace Core.Scenes.Ingame;
namespace Core.Scenes.Ingame.Modes.Overworld;

public class OverworldMode : IMode, IStateManager
{
Expand Down Expand Up @@ -60,23 +60,23 @@ public void Load(Dictionary<string, object> data)
weakNextID = (string) data["WeakState"];
Load(new ModeParameters().AppendData("state", (string) data["State"] ?? "null"));
if (data.ContainsKey("LoadedMap"))
worldGameView.mapDataRegistry.LoadMap((string) data["LoadedMap"]);
worldGameView.MapDataRegistry.LoadMap((string) data["LoadedMap"]);
if (data.ContainsKey("PlayerPosX") && data.ContainsKey("PlayerPosY"))
worldGameView.player.TeleportPlayer(new Vector2(int.Parse((string) data["PlayerPosX"]),
worldGameView.Player.TeleportPlayer(new Vector2(int.Parse((string) data["PlayerPosX"]),
int.Parse((string) data["PlayerPosY"])));
if (data.ContainsKey("DiscoveredTiles"))
worldGameView.discoveredTiles =
worldGameView.DiscoveredTiles =
JsonConvert.DeserializeObject<Dictionary<string, List<Vector2>>>((string) data["DiscoveredTiles"]);
}

public void Save(Dictionary<string, object> data)
{
data.Add("State", LastSaveState);
data.Add("WeakState", LastSaveStateWeak);
data.Add("LoadedMap", worldGameView.mapDataRegistry.GetLoadedMapName());
data.Add("PlayerPosX", (worldGameView.player.CurrentPos.X / 32).ToString());
data.Add("PlayerPosY", (worldGameView.player.CurrentPos.Y / 32).ToString());
data.Add("DiscoveredTiles", JsonConvert.SerializeObject(worldGameView.discoveredTiles));
data.Add("LoadedMap", worldGameView.MapDataRegistry.GetLoadedMapName());
data.Add("PlayerPosX", (worldGameView.Player.CurrentPos.X / 32).ToString());
data.Add("PlayerPosY", (worldGameView.Player.CurrentPos.Y / 32).ToString());
data.Add("DiscoveredTiles", JsonConvert.SerializeObject(worldGameView.DiscoveredTiles));
}

public string weakNextID
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Core.Scenes.Ingame.Localization;
using Core.Scenes.Ingame.Views;
using Core.States;

namespace Core.Scenes.Ingame.Views;
namespace Core.Scenes.Ingame.Modes.Overworld;

public class StateChatView : BaseChatView
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,37 @@
using System.Collections.Generic;
using Core.Content;
using Core.Input;
using Core.Scenes.Ingame.Views;
using Core.Scenes.Ingame.World;
using Core.States;
using Core.Utils;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Core.Scenes.Ingame.Views;
namespace Core.Scenes.Ingame.Modes.Overworld;

public class WorldGameView : IGameView, IRenderer<IngameRenderContext>, IUpdate<IngameUpdateContext>, ILoadable
public class WorldGameView : IGameView
{
private readonly ContentRegistry _contentRegistry;

private Vector2 _cameraCulling;
public Dictionary<string, List<Vector2>> discoveredTiles = new();
public Dictionary<string, List<Vector2>> DiscoveredTiles = new();

public MapDataRegistry mapDataRegistry;
public Player player;
public TileDataRegistry tileDataRegistry;
public MapDataRegistry MapDataRegistry;
public readonly Player Player;
public TileDataRegistry TileDataRegistry;

public WorldGameView(IGlobalEventHandler eventHandler, IStateManager gameManager, ISoundPlayer soundPlayer,
ContentRegistry content)
{
_contentRegistry = content;
player = new Player(eventHandler, gameManager, this, soundPlayer, content);
Player = new Player(eventHandler, gameManager, this, soundPlayer, content);
}

public void Render(SpriteBatch spriteBatch, IngameRenderContext context)
{
// if theres no map loaded, dont render anything
if (mapDataRegistry.GetLoadedMap() == null) return;
if (MapDataRegistry.GetLoadedMap() == null) return;

// get corner of camera screen, we'll render from there on so we dont have to do any loop containing all world tiles
_cameraCulling = new Vector2((int) Math.Round(context.TopLevelContext.Camera.Position.X / 32) - 1,
Expand All @@ -42,18 +43,18 @@ public void Render(SpriteBatch spriteBatch, IngameRenderContext context)
for (var x = (int) _cameraCulling.X; x < (int) _cameraCulling.X + 9; x++)
{
// make sure we have discovered tiles
if (!discoveredTiles.ContainsKey(mapDataRegistry.GetLoadedMap().name))
discoveredTiles.Add(mapDataRegistry.GetLoadedMap().name, new List<Vector2>());
if (!DiscoveredTiles.ContainsKey(MapDataRegistry.GetLoadedMap().name))
DiscoveredTiles.Add(MapDataRegistry.GetLoadedMap().name, new List<Vector2>());

if (discoveredTiles[mapDataRegistry.GetLoadedMap().name].Contains(new Vector2(x, y)) ||
!mapDataRegistry.GetLoadedMap()
if (DiscoveredTiles[MapDataRegistry.GetLoadedMap().name].Contains(new Vector2(x, y)) ||
!MapDataRegistry.GetLoadedMap()
.explorable) // only render tiles explored, unless the map isnt set to be explorable
{
var tileData = mapDataRegistry.GetLoadedMap().GetTile(new Vector2(x, y));
var tileData = MapDataRegistry.GetLoadedMap().GetTile(new Vector2(x, y));

if (tileData != null) // dont render whats not there :P
{
var sprite = tileDataRegistry.GetTile(tileData.name).GetSprite(_contentRegistry); // grab sprite
var sprite = TileDataRegistry.GetTile(tileData.name).GetSprite(_contentRegistry); // grab sprite

spriteBatch.Draw(
sprite,
Expand All @@ -66,30 +67,30 @@ public void Render(SpriteBatch spriteBatch, IngameRenderContext context)
}
}

var roundedPos = (float) Math.Ceiling(player.CurrentPos.Y / 32);
var roundedPos = (float) Math.Ceiling(Player.CurrentPos.Y / 32);
if (roundedPos == y) // render player right after its current tile
player.Render(spriteBatch, context);
Player.Render(spriteBatch, context);
}
}

public void Load(ContentLoader content)
{
tileDataRegistry = _contentRegistry.tileDataRegistry;
mapDataRegistry = _contentRegistry.mapDataRegistry.SetupDiscovery(discoveredTiles);
TileDataRegistry = _contentRegistry.tileDataRegistry;
MapDataRegistry = _contentRegistry.mapDataRegistry.SetupDiscovery(DiscoveredTiles);
}

public void Update(float deltaTime, IngameUpdateContext context)
{
player.Update(deltaTime, context);
Player.Update(deltaTime, context);

if (Controls.MoveUp())
player.MovePlayer(new Vector2(0, -1));
Player.MovePlayer(new Vector2(0, -1));
if (Controls.MoveDown())
player.MovePlayer(new Vector2(0, 1));
Player.MovePlayer(new Vector2(0, 1));
if (Controls.MoveLeft())
player.MovePlayer(new Vector2(-1, 0));
Player.MovePlayer(new Vector2(-1, 0));
if (Controls.MoveRight())
player.MovePlayer(new Vector2(1, 0));
Player.MovePlayer(new Vector2(1, 0));

// world tints i liked in case we use them
//new Color(110, 145, 155)); // night tint
Expand Down
4 changes: 4 additions & 0 deletions Core/Lib/Scenes/Ingame/Views/BaseChatView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class BaseChatView : IChatView
private bool _sticky = true;
protected Queue<IChatComponent> QueuedComponents = new();
protected int Width;
private Texture2D _divider;

public BaseChatView(ILocalizationManager localizationManager, IFontManager fontManager)
{
Expand All @@ -34,6 +35,7 @@ public BaseChatView(ILocalizationManager localizationManager, IFontManager fontM

public void Load(ContentLoader content)
{
_divider = content.Load<Texture2D>("Sprites/divider.png");
}

public void Clear()
Expand Down Expand Up @@ -80,6 +82,8 @@ public void Render(SpriteBatch spriteBatch, IngameRenderContext context)
// Draw UI here
spriteBatch.FillRectangle(new Vector2(), new Size2(context.ChatWidth, context.BaseScreenSize.Y),
context.BackgroundColor);
spriteBatch.Draw(_divider, new Rectangle(new Point(context.ChatWidth-3, 0), new Point(3, 224)), Color.White);

var offsetY = 0f + -_scrollOffset;
RunningComponents.ForEach(component =>
{
Expand Down
19 changes: 10 additions & 9 deletions Core/Lib/Scenes/Ingame/World/Player.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Core.Content;
using Core.Scenes.Ingame.Modes.Overworld;
using Core.Scenes.Ingame.Views;
using Core.States;
using Core.Utils;
Expand Down Expand Up @@ -112,7 +113,7 @@ public void Update(float deltaTime, IngameUpdateContext context)

public void TeleportPlayer(Vector2 mapPos) // can be used to move to spawn
{
if (_worldRenderer.mapDataRegistry.GetLoadedMap() == null) return; // cant move if no map loaded
if (_worldRenderer.MapDataRegistry.GetLoadedMap() == null) return; // cant move if no map loaded
if (CurrentPos != _targetPos) return; // cant move if currently moving
//if (_worldRenderer.mapDataRegistry.GetLoadedMap().GetTile(CurrentPos / 32) == null ||
// _worldRenderer.tileDataRegistry.GetTile(_worldRenderer.mapDataRegistry.GetLoadedMap().GetTile(CurrentPos / 32).name) == null) return; // cant move to what doesnt exist
Expand All @@ -126,16 +127,16 @@ public void TeleportPlayer(Vector2 mapPos) // can be used to move to spawn
public void MovePlayer(Vector2 direction)
{
if (!_gameManager.ActiveState.AllowMove) return; // cant move if... cant move
if (_worldRenderer.mapDataRegistry.GetLoadedMap() == null) return; // cant move if no map loaded
if (_worldRenderer.MapDataRegistry.GetLoadedMap() == null) return; // cant move if no map loaded
if (CurrentPos != _targetPos) return; // cant move if currently moving

_previousTileData = _worldRenderer.mapDataRegistry.GetLoadedMap().GetTile(CurrentPos / 32);
_targetTileData = _worldRenderer.mapDataRegistry.GetLoadedMap().GetTile(CurrentPos / 32 + direction);
_previousTileData = _worldRenderer.MapDataRegistry.GetLoadedMap().GetTile(CurrentPos / 32);
_targetTileData = _worldRenderer.MapDataRegistry.GetLoadedMap().GetTile(CurrentPos / 32 + direction);

if (_targetTileData == null) return; // cant move to what doesnt exist

var currentTile = _worldRenderer.tileDataRegistry.GetTile(_previousTileData.name);
var targetTile = _worldRenderer.tileDataRegistry.GetTile(_targetTileData.name);
var currentTile = _worldRenderer.TileDataRegistry.GetTile(_previousTileData.name);
var targetTile = _worldRenderer.TileDataRegistry.GetTile(_targetTileData.name);

if (currentTile.AllowsDirection(direction) && // check both tiles allow the direction
targetTile.AllowsDirection(direction * new Vector2(-1, -1)))
Expand All @@ -149,13 +150,13 @@ public void MovePlayer(Vector2 direction)

private void DiscoverTiles()
{
if (!_worldRenderer.mapDataRegistry.GetLoadedMap().explorable)
if (!_worldRenderer.MapDataRegistry.GetLoadedMap().explorable)
return; // dont save discovered tiles if map isnt explorable

foreach (var offset in _discoverTileRadius)
if (!_worldRenderer.discoveredTiles[_worldRenderer.mapDataRegistry.GetLoadedMap().name]
if (!_worldRenderer.DiscoveredTiles[_worldRenderer.MapDataRegistry.GetLoadedMap().name]
.Contains(offset + CurrentPos / 32))
_worldRenderer.discoveredTiles[_worldRenderer.mapDataRegistry.GetLoadedMap().name]
_worldRenderer.DiscoveredTiles[_worldRenderer.MapDataRegistry.GetLoadedMap().name]
.Add(offset + CurrentPos / 32);
}
}
11 changes: 6 additions & 5 deletions Core/Lib/States/RenderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using Core.Scenes.Ingame;
using Core.Scenes.Ingame.Battle;
using Core.Scenes.Ingame.Modes.Overworld;
using Core.Scenes.Ingame.Views;
using Microsoft.Xna.Framework;
using NLua;
Expand Down Expand Up @@ -70,23 +71,23 @@ public void Exit()

public void MovePlayer(int x, int y)
{
_worldGameView.player.MovePlayer(new Vector2(x, y));
_worldGameView.Player.MovePlayer(new Vector2(x, y));
}

public void TeleportPlayer(int x, int y)
{
_worldGameView.player.TeleportPlayer(new Vector2(x, y));
_worldGameView.Player.TeleportPlayer(new Vector2(x, y));
}

public void LoadMap(string name, int x, int y)
{
_worldGameView.mapDataRegistry.LoadMap(name);
_worldGameView.player.TeleportPlayer(new Vector2(x, y));
_worldGameView.MapDataRegistry.LoadMap(name);
_worldGameView.Player.TeleportPlayer(new Vector2(x, y));
}

public string GetLoadedMap()
{
return _worldGameView.mapDataRegistry.GetLoadedMapName();
return _worldGameView.MapDataRegistry.GetLoadedMapName();
}

public void PlaySFX(string name)
Expand Down
Binary file added Core/Mods/CoreMod/Sprites/divider.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 209c934

Please sign in to comment.