travel on world map

This commit is contained in:
Le Duc Anh
2026-04-24 15:33:39 +07:00
parent 43d2f7291a
commit dfb7874b88
6 changed files with 268 additions and 9 deletions
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: fce16fcbe8e82ec4588c7fba89c7cb64
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,3 +1,4 @@
using BrewMonster.Network;
using CSNetwork.Common;
using UnityEngine;
using UnityEngine.UI;
@@ -54,10 +55,14 @@ namespace BrewMonster.UI
public override bool Render()
{
// UpdateHostPlayerPositionImage();
UpdateHostPlayerPositionImage();
return base.Render();
}
Vector3 _hostPlayerPosition;
Quaternion _hostPlayerRotation;
private void UpdateHostPlayerPositionImage()
{
if (_hostPlayerPositionImage == null)
@@ -65,7 +70,7 @@ namespace BrewMonster.UI
return;
}
if (!TryGetHostPlayerPosition(out Vector3 hostPosition))
if (!TryGetHostPlayerPosition(out _hostPlayerPosition, out _hostPlayerRotation))
{
_hostPlayerPositionImage.enabled = false;
return;
@@ -81,13 +86,16 @@ namespace BrewMonster.UI
_hostPlayerPositionImage.enabled = true;
RectTransform hostPlayerRectTransform = _hostPlayerPositionImage.rectTransform;
hostPlayerRectTransform.anchoredPosition = new Vector2(
hostPosition.x / _positionFactor,
hostPosition.z / _positionFactor);
_hostPlayerPosition.x / _positionFactor,
_hostPlayerPosition.z / _positionFactor);
hostPlayerRectTransform.localRotation = Quaternion.Euler(0, 0, -_hostPlayerRotation.eulerAngles.y);
}
private bool TryGetHostPlayerPosition(out Vector3 hostPlayerPosition)
private bool TryGetHostPlayerPosition(out Vector3 hostPlayerPosition, out Quaternion hostPlayerRotation)
{
hostPlayerPosition = Vector3.zero;
hostPlayerRotation = Quaternion.identity;
CECHostPlayer hostPlayer = GetHostPlayer();
if (hostPlayer == null || hostPlayer.transform == null)
@@ -96,6 +104,7 @@ namespace BrewMonster.UI
}
hostPlayerPosition = hostPlayer.transform.position;
hostPlayerRotation = hostPlayer.transform.rotation;
return true;
}
@@ -139,6 +148,20 @@ namespace BrewMonster.UI
}
}
/// <summary>
/// When user click on the map texture.
/// We will calculate the world coordinates from the local cursor position. Then move the host player to the world coordinates.
/// </summary>
/// <param name="localCursorPosition"></param>
public void OnMapClicked(Vector2 localCursorPosition)
{
var worldCoordinates = localCursorPosition * _positionFactor;
UnityGameSession.c2s_CmdGoto(worldCoordinates.x, 1.0f, worldCoordinates.y);
// close the map
OnCloseButtonClicked();
}
private void OnCloseButtonClicked()
{
CloseDialogue();
@@ -0,0 +1,59 @@
using UnityEngine;
using UnityEngine.EventSystems;
namespace BrewMonster.UI
{
public class WorldMapClickHandler : MonoBehaviour, IPointerClickHandler
{
[Tooltip("The RectTransform of the Map Image (Usually this GameObject)")]
private RectTransform mapRectTransform;
[SerializeField] private RectTransform _hostPlayerPositionImage;
public DlgWorldMap dlgWorldMap;
// The host player player (0,0,0) is not at the center of the map. It usually has an offset that we have to calculate at Awake
private Vector2 _hostPlayerOffsetPosition;
private void Awake()
{
// Get the RectTransform of the map
mapRectTransform = GetComponent<RectTransform>();
CalculateHostPlayerOffsetPosition();
}
private void CalculateHostPlayerOffsetPosition()
{
_hostPlayerOffsetPosition = _hostPlayerPositionImage.anchoredPosition;
// if the max/min of the anchor is 0.5 0.5, then the host player is at the center of the map
// however we have to calculate the offset of the player because it is not at the center of the map
// we can calculate the offset by the max/min of the anchor
var maxAnchor = _hostPlayerPositionImage.anchorMax;
var minAnchor = _hostPlayerPositionImage.anchorMin;
_hostPlayerOffsetPosition = new Vector2((maxAnchor.x - 0.5f) * mapRectTransform.rect.width, (maxAnchor.y - 0.5f) * mapRectTransform.rect.height);
}
// This triggers automatically when the user clicks/taps on this Image
public void OnPointerClick(PointerEventData eventData)
{
Vector2 localCursorPosition;
// Convert the screen click position to local anchored position inside the Map
bool isConverted = RectTransformUtility.ScreenPointToLocalPointInRectangle(
mapRectTransform,
eventData.position,
eventData.pressEventCamera,
out localCursorPosition
);
if (isConverted)
{
// convert the localCursorPosition to the local position of the host player position image
localCursorPosition -= _hostPlayerOffsetPosition;
dlgWorldMap.OnMapClicked(localCursorPosition);
}
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 876ab9894018f2b4cb4a0b04bf534bc5
+171 -4
View File
@@ -1,5 +1,80 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &191621185379502263
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 9142400375056319150}
- component: {fileID: 7949922842883969624}
- component: {fileID: 6790297770241223980}
m_Layer: 0
m_Name: click_pos
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!224 &9142400375056319150
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 191621185379502263}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 7169122999130120872}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 15, y: 15}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &7949922842883969624
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 191621185379502263}
m_CullTransparentMesh: 1
--- !u!114 &6790297770241223980
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 191621185379502263}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 0}
m_Type: 0
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!1 &936441858863998774
GameObject:
m_ObjectHideFlags: 0
@@ -35,7 +110,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 403.03613, y: 341.03613}
m_AnchoredPosition: {x: 549, y: 465}
m_SizeDelta: {x: 58, y: 58}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &6772256914059310038
@@ -286,7 +361,7 @@ MonoBehaviour:
imageProgress: {fileID: 0}
mapImage: {fileID: 1174346096914174862}
_hostPlayerPositionImage: {fileID: 4036230907032538800}
_positionFactor: 2.5
_positionFactor: 1.8
_closeButton: {fileID: 8858186809287203567}
--- !u!1 &8308536083041954008
GameObject:
@@ -363,6 +438,81 @@ MonoBehaviour:
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!1 &8900623989843312765
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1510574663178069641}
- component: {fileID: 5533213955690836106}
- component: {fileID: 7414570214724661608}
m_Layer: 0
m_Name: hostplayerpos_debug
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!224 &1510574663178069641
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8900623989843312765}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 7169122999130120872}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.518}
m_AnchorMax: {x: 0.5, y: 0.518}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 13, y: 16}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &5533213955690836106
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8900623989843312765}
m_CullTransparentMesh: 1
--- !u!114 &7414570214724661608
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8900623989843312765}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 448046091, guid: 99520ceed6182dd408f2da040fe0c033, type: 3}
m_Type: 0
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!1 &8924313797425690832
GameObject:
m_ObjectHideFlags: 0
@@ -374,6 +524,7 @@ GameObject:
- component: {fileID: 7169122999130120872}
- component: {fileID: 7283747291599937432}
- component: {fileID: 1174346096914174862}
- component: {fileID: 4521997594693838426}
m_Layer: 0
m_Name: MapTexture
m_TagString: Untagged
@@ -394,12 +545,14 @@ RectTransform:
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 5906545349664091413}
- {fileID: 1510574663178069641}
- {fileID: 9142400375056319150}
m_Father: {fileID: 7323734624486819451}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 1024, y: 1024}
m_AnchoredPosition: {x: -6.0056, y: -6.0056}
m_SizeDelta: {x: 1408.0024, y: 1408.0024}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &7283747291599937432
CanvasRenderer:
@@ -439,3 +592,17 @@ MonoBehaviour:
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!114 &4521997594693838426
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8924313797425690832}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 876ab9894018f2b4cb4a0b04bf534bc5, type: 3}
m_Name:
m_EditorClassIdentifier:
_hostPlayerPositionImage: {fileID: 5906545349664091413}
dlgWorldMap: {fileID: 135853640611757204}