forked from Naburimannu/libtcodpy-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
58 lines (45 loc) · 1.55 KB
/
log.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
"""
Global message log.
Call log.init() before using.
Retrieve (message, color) tuples from log.game_msgs[].
Append them using log.message().
"""
import libtcodpy as libtcod
import textwrap
import config
MSG_WIDTH = config.SCREEN_WIDTH - config.BAR_WIDTH - 2
# Number of lines of messages normally displayed on screen
MSG_HEIGHT = config.PANEL_HEIGHT - 1
# Number of lines of messages retained for history display (^p)
MSG_LIMIT = 150
class ExplicitMessage(object):
def __init__(self, message, color, count):
self.message = message
self.color = color
self.count = count
def can_merge(self, other):
return (self.message == other.message and
self.color == other.color and
self.count + other.count < 10)
def init():
global game_msgs
# The list of game messages and their colors; starts empty.
game_msgs = []
def message(new_msg, color=libtcod.white):
"""
Add a colored string to the end of the log;
does wordwrap at MSG_WIDTH-5 characters
since a count e.g. " (x3)" can add up to 5.
"""
global game_msgs
new_msg_lines = textwrap.wrap(new_msg, MSG_WIDTH-5)
for line in new_msg_lines:
# If the buffer is full, remove the first line
# to make room for the new one.
if len(game_msgs) == MSG_LIMIT:
del game_msgs[0]
new_message = ExplicitMessage(line, color, 1)
if game_msgs and game_msgs[-1].can_merge(new_message):
game_msgs[-1].count += 1
return
game_msgs.append(new_message)