File: entity.py

package info (click to toggle)
balder2d 1.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 12,552 kB
  • ctags: 1,226
  • sloc: cpp: 8,623; xml: 398; python: 364; ansic: 214; makefile: 79; sh: 9
file content (50 lines) | stat: -rw-r--r-- 1,605 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import math
try:
  import psyco
except ImportError:
  # no psyco, so don't use it
  pass
else:
  psyco.full()

def FindClosestProbe(this_probe_id, probe_dict, max_distance = 1000):
    """ returns the ID of the closest probe
    """
    closest_distance = max_distance**2;
    closest_probe_id = -1
    xorigin = probe_dict[this_probe_id]['x']
    yorigin = probe_dict[this_probe_id]['y']
    for probe in probe_dict.items():
        id = probe[0]
        if id == this_probe_id:
            continue
        xtarget = math.fabs(xorigin - probe_dict[id]['x'])
        ytarget = math.fabs(yorigin - probe_dict[id]['y'])
        distance = xtarget**2 + ytarget**2
        if distance < closest_distance:
            closest_distance = distance
            closest_probe_id = id
    return closest_probe_id

def FindClosestEntity(x, y, entity_list, max_distance = 1000):
    """ returns the x and y coordinates of the closest entity in the given list
        to the given point, or 0,0 if no entity is close enough
    """
    xent, yent = 0, 0
    closest_distance = max_distance**2;
    for entity in entity_list:
        xtarget = math.fabs(x - entity['x'])
        ytarget = math.fabs(y - entity['y'])
        distance = xtarget**2 + ytarget**2
        if distance < closest_distance:
            closest_distance = distance
            xent, yent = entity['x'], entity['y']
    return xent, yent

def DistanceTo(x, y, entity):
    """ returns the distance from the entity to the given point
    """
    xent = entity['x']
    yent = entity['y']
    return int(math.sqrt((x - xent)**2 + (y - yent)**2))