DanisRace/Scripts/Map.py
Victorious Children Studios 594e088a83 07-07-24
2024-07-13 16:15:50 +03:00

191 lines
4.6 KiB
Python

# This file is for map functions.
# The points where the map image file ends in the
# space of the game.
# ( Note the map is 180 degrees rotated from the in game orientation of everything.
# This is due to modeling of the map ( while Moria's Race movie was being made ) was
# made before the lore for whether this place is was made up. )
xs = -2633 # The most east point
xe = 2383 # The most west point
ys = -1911 # The most north point
ye = 3106 # THe most south point
# Widths of that image file in meters.
xd = xe - xs
yd = ye - ys
rr = 730 # Radius to the edge of the map in meters
mr = 0.05 # Same radius by as a point on the map.
map_factor = mr / rr # The factor by which the relative point is calculated.
import bge
import math
import mathutils
from Scripts import Reuse
from Scripts import Character_Controll
from Scripts import Vehicle
def UpdateMapUI():
# Scene stuff
scene = bge.logic.getCurrentScene()
dani = scene.objects["Dani_Box"]
cam = scene.active_camera
# Map widget
cui = scene.objects["Map_UI"]
# Rotation
# We use the orientation of the camera
# and rotating it 180 degrees for the map.
r = cam.orientation.to_euler().z
r += math.pi
if r > 2*math.pi: r -= 2*math.pi
r = [0,0,r]
cui.blenderObject["orientation"] = r
# Position
# This one is a bit harder, since it will
# move the mapping of the image texture arround
# in the material. And it's hard to tell it just
# the coordinations ( also there is 180 degree flip ).
dl = dani.position
px = 1-( ( dl.x - xs ) / xd ) - 0.13
py = 1-( ( dl.y - ys ) / yd ) - 0.12
p = [px,py,0]
cui.blenderObject["position"] = p
# Map Dani Indicator
# This is even harder. Since the map is rotated with camera
# this widget should by ofsetted by the camera rotation.
cr = r[2]
mdi = scene.objects["Map_Dani_Indicator"]
rig = Character_Controll.getRig(dani)
r = (2*math.pi)-rig.worldOrientation.to_euler().z
r += cr
if r > 2*math.pi: r -= 2*math.pi
r = [0,r,0]
mdi.localOrientation = r
# Removing old show marks
database = bge.logic.globalDict.get("map-icons", {})
for ID in list(database.keys()):
data = database[ID]
#if data["obj"] in Reuse.reuse.get(str(data["obj"]), [])\
if data["updated"]+0.5 < bge.logic.getRealTime():
Reuse.Delete(data["icon"])
del database[ID]
def Show(obj, color=[1,0,0], icon="Map_Icon_Directional", ID=""):
# This function will show things on the map.
# Scene stuff
scene = bge.logic.getCurrentScene()
dani = scene.objects["Dani_Box"]
cam = scene.active_camera
cui = scene.objects["Map_UI"]
# Making the icon database to track changes.
if "map-icons" not in bge.logic.globalDict:
bge.logic.globalDict["map-icons"] = {}
database = bge.logic.globalDict["map-icons"]
# Making sure that there is an object in the database
if not ID:
if type(obj) == list: ID = str(obj)
else: ID = str(id(obj))
if ID not in database:
database[ID] = {"obj":obj}
data = database[ID]
data["updated"] = bge.logic.getRealTime()
# Making sure that there is an icon in the data
if "icon" not in data:
# Making an icon
icon = Reuse.Create(icon, frompoint=cui)
icon.setParent(cui, True, True)
icon.worldScale = cui.worldScale * 2
icon.applyRotation([math.pi/2,0,0], True)
icon.blenderObject["color"] = color
data["icon"] = icon
icon = data["icon"]
# Now that we have the icon we can map it to the map
f = map_factor
# Getting vector to the target
try:
t = obj.worldPosition.copy()
torot = True
except:
t = mathutils.Vector(obj)
torot = False
d = dani.getDistanceTo(t)
if d > rr: # If the target out of range of the map
f = mr / d # This puts the icon on the edge of the map
f *= cui.worldScale.x * 2
t = t - dani.worldPosition
cr = cam.worldOrientation.to_euler()
cr = mathutils.Euler((0,0,-cr.z))
t.rotate(cr)
t = t * f
t.z = 0.001
icon.worldPosition = Vehicle.RelativePoint(cui, t)
# Rotation
if torot:
cr = cr.z
r = obj.worldOrientation.to_euler().z
r += cr
if r > 2*math.pi: r -= 2*math.pi
r = [math.pi/2,0,r]
icon.localOrientation = r
else:
icon.localOrientation = [math.pi/2, 0,0]