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._campaign import Campaign
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._level import Level
from ba._lobby import Lobby, Chooser

View File

@ -22,12 +22,13 @@
from __future__ import annotations
from typing import TYPE_CHECKING
import math
import _ba
from ba._enums import TimeType, TimeFormat, SpecialChar
if TYPE_CHECKING:
from typing import Any, Dict, Sequence
from typing import Any, Dict, Sequence, Tuple
import ba
TROPHY_CHARS = {
@ -46,6 +47,14 @@ def get_trophy_string(trophy_id: str) -> str:
return _ba.charstr(TROPHY_CHARS[trophy_id])
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:
"""Return a predefined object for the current Activity, creating if needed.

View File

@ -23,12 +23,12 @@
from __future__ import annotations
import math
import random
import weakref
from typing import TYPE_CHECKING
import ba
from ba import distance
from bastd.actor import spaz as basespaz
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_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:
"""A message saying a ba.SpazBot got punched.
@ -199,8 +192,9 @@ class SpazBot(basespaz.Spaz):
closest = None
assert self._player_pts is not None
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
# (keeps bots from following players off cliffs).
if (closest_dist is None
@ -215,7 +209,7 @@ class SpazBot(basespaz.Spaz):
ba.Vec3(closest_vel[0], closest_vel[1], closest_vel[2]))
return None, None
def set_player_points(self, pts: List[Tuple[ba.Vec3, ba.Vec3]]) -> None:
def set_player_points(self, pts: List[Tuple[ba.Vec3, ba.Vec3]]) -> None:
"""Provide the spaz-bot with the locations of its enemies."""
self._player_pts = pts
@ -263,7 +257,9 @@ class SpazBot(basespaz.Spaz):
target_pt_raw = ba.Vec3(*self.target_flag.node.position)
diff = (target_pt_raw - our_pos)
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()
# 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)
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()
if self._mode == 'throw':