-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.py
76 lines (60 loc) · 2.01 KB
/
misc.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Contains miscellaneous classes and functions which are used by multiple modules."""
import sys
import pygame
def leave():
"""Close the game."""
pygame.quit()
sys.exit(0)
def time_to_str(seconds):
"""Return a time string given a time in seconds."""
time_s = str(int(seconds % 60))
if len(time_s) == 1:
time_s = "0" + time_s
time_m = str(int(seconds // 60))
return time_m + ":" + time_s
def pluralise(value, singular, plural=None):
"""Return the singular or plural of a word depending on whether the value is non-one.
If plural is None (default), add an 's' to the end of the word instead.
"""
if value == 1:
word = singular
elif plural is None:
word = singular + "s"
else:
word = plural
return str(value) + " " + word
class DynamicPos:
"""A vector value which linearly interpolates to a position.
Specifically for the camera in this module.
"""
def __init__(self, pos, speed):
self.current = pos
self.goal = pos
self.speed = speed
def move(self, pos, instant=False):
"""Set a target position. Instant moves it there instantly."""
self.goal = pos
if instant:
self.current = pos
def update(self, delta):
"""Linearly interpolate to target position."""
x = (self.goal[0] - self.current[0])*min(1, delta*self.speed*0.001)
y = (self.goal[1] - self.current[1])*min(1, delta*self.speed*0.001)
self.current = (self.current[0]+x, self.current[1] + y)
@property
def x(self):
"""Get x value of vector."""
return self.current[0]
@property
def y(self):
"""Get y value of vector."""
return self.current[1]
def __str__(self):
keys = ("current", "goal", "speed")
string = "DynamicPos("
for key in keys:
string += key+"="+str(getattr(self, key))+" "
string += ")"
return string
def __getitem__(self, index):
return self.current[index]