distance (_my_vec3_dist) moved to ba/_gameutils.py

Tests passed
This commit is contained in:
indev 2020-04-08 16:21:47 +03:00
parent defbcd1e55
commit 51bae338f3
3 changed files with 22 additions and 15 deletions

View File

@ -69,7 +69,7 @@ from ba._appdelegate import AppDelegate
from ba._apputils import is_browser_likely_available from ba._apputils import is_browser_likely_available
from ba._campaign import Campaign from ba._campaign import Campaign
from ba._gameutils import (animate, animate_array, show_damage_count, from ba._gameutils import (animate, animate_array, show_damage_count,
sharedobj, timestring, cameraflash) sharedobj, timestring, cameraflash, distance)
from ba._general import WeakCall, Call from ba._general import WeakCall, Call
from ba._level import Level from ba._level import Level
from ba._lobby import Lobby, Chooser from ba._lobby import Lobby, Chooser

View File

@ -22,12 +22,13 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
import math
import _ba import _ba
from ba._enums import TimeType, TimeFormat, SpecialChar from ba._enums import TimeType, TimeFormat, SpecialChar
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Any, Dict, Sequence from typing import Any, Dict, Sequence, Tuple
import ba import ba
TROPHY_CHARS = { TROPHY_CHARS = {
@ -46,6 +47,14 @@ def get_trophy_string(trophy_id: str) -> str:
return _ba.charstr(TROPHY_CHARS[trophy_id]) return _ba.charstr(TROPHY_CHARS[trophy_id])
return '?' return '?'
def distance(vec1: Tuple[float, float, float],
vec2: Tuple[float, float, float]) -> float:
"""Find distance between two positions"""
xlen = vec1[0] - vec2[0]
ylen = vec1[1] - vec2[1]
zlen = vec1[2] - vec2[2]
xylen = math.sqrt(xlen**2 + ylen**2)
return math.sqrt(xylen**2 + zlen**2)
def sharedobj(name: str) -> Any: def sharedobj(name: str) -> Any:
"""Return a predefined object for the current Activity, creating if needed. """Return a predefined object for the current Activity, creating if needed.

View File

@ -23,12 +23,12 @@
from __future__ import annotations from __future__ import annotations
import math
import random import random
import weakref import weakref
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
import ba import ba
from ba import distance
from bastd.actor import spaz as basespaz from bastd.actor import spaz as basespaz
if TYPE_CHECKING: if TYPE_CHECKING:
@ -42,13 +42,6 @@ DEFAULT_BOT_HIGHLIGHT = (0.1, 0.3, 0.1)
PRO_BOT_COLOR = (1.0, 0.2, 0.1) PRO_BOT_COLOR = (1.0, 0.2, 0.1)
PRO_BOT_HIGHLIGHT = (0.6, 0.1, 0.05) PRO_BOT_HIGHLIGHT = (0.6, 0.1, 0.05)
def distance(vec1: Tuple[float], vec2: Tuple[float]) -> float:
xlen = vec1[0] - vec2[0]
ylen = vec1[1] - vec2[1]
zlen = vec1[2] - vec2[2]
xylen = math.sqrt(xlen**2 + ylen**2)
return math.sqrt(xylen**2 + zlen**2)
class SpazBotPunchedMessage: class SpazBotPunchedMessage:
"""A message saying a ba.SpazBot got punched. """A message saying a ba.SpazBot got punched.
@ -199,8 +192,9 @@ class SpazBot(basespaz.Spaz):
closest = None closest = None
assert self._player_pts is not None assert self._player_pts is not None
for plpt, plvel in self._player_pts: for plpt, plvel in self._player_pts:
dist = distance((plpt.x, plpt.y, plpt.z), (botpt.x, botpt.y, botpt.z)) dist = distance(
(plpt.x, plpt.y, plpt.z),
(botpt.x, botpt.y, botpt.z))
# Ignore player-points that are significantly below the bot # Ignore player-points that are significantly below the bot
# (keeps bots from following players off cliffs). # (keeps bots from following players off cliffs).
if (closest_dist is None if (closest_dist is None
@ -263,7 +257,9 @@ class SpazBot(basespaz.Spaz):
target_pt_raw = ba.Vec3(*self.target_flag.node.position) target_pt_raw = ba.Vec3(*self.target_flag.node.position)
diff = (target_pt_raw - our_pos) diff = (target_pt_raw - our_pos)
diff = ba.Vec3(diff[0], 0, diff[2]) # don't care about y diff = ba.Vec3(diff[0], 0, diff[2]) # don't care about y
dist = distance((target_pt_raw.x, 0, target_pt_raw.z), (our_pos.x, 0, our_pos.z)) dist = distance(
(target_pt_raw.x, 0, target_pt_raw.z),
(our_pos.x, 0, our_pos.z))
to_target = diff.normalized() to_target = diff.normalized()
# If we're holding some non-flag item, drop it. # If we're holding some non-flag item, drop it.
@ -331,7 +327,9 @@ class SpazBot(basespaz.Spaz):
target_vel * dist_raw * 0.3 * self._lead_amount) target_vel * dist_raw * 0.3 * self._lead_amount)
diff = (target_pt - our_pos) diff = (target_pt - our_pos)
dist = distance((target_pt.x, target_pt.y, target_pt.z), (our_pos.x, our_pos.y, our_pos.z)) dist = distance(
(target_pt.x, target_pt.y, target_pt.z),
(our_pos.x, our_pos.y, our_pos.z))
to_target = diff.normalized() to_target = diff.normalized()
if self._mode == 'throw': if self._mode == 'throw':