-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdialogueframe.py
69 lines (53 loc) · 2.16 KB
/
dialogueframe.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
import collections
import entity
import graphics
import menuframe
import stateframe
from entity import Player
from graphics import DisplayTextBox
from menuframe import MenuFrame
from stateframe import StateFrame, stack
class NoDialogueError(Exception):
def __init__(self, npc):
self.msg = 'Lack of dialog options for ' + npc.name
def __str__(self):
return self.msg
class DialogueFrame(StateFrame):
FONTSIZE = 20
SCREEN_HEIGHT = 600
SCREEN_WIDTH = 800
@staticmethod
def ForPlayer(player, npc, show=True):
for plotEvent in reversed(npc.dialogueList):
if not plotEvent or player.plotEvents[plotEvent]:
ret = DialogueFrame(npc.dialogueList[plotEvent][0],
lambda player=player, npc=npc: npc.dialogueList[plotEvent][1](player, npc))
if show:
ret.Show()
return ret
raise NoDialogueError(npc)
def __init__(self, dialogueList, endFunction = lambda: None, showAccept=False):
super(DialogueFrame, self).__init__()
if isinstance(dialogueList, str):
dialogueList = [dialogueList]
assert len(dialogueList) > 0
self.dialogueList = dialogueList
self.showAccept = showAccept
self.endFunction = endFunction
self.currentDialogueLineIndex = 0
@property
def currentDialogue(self):
return self.dialogueList[self.currentDialogueLineIndex]
def Render(self, ctx, size):
self.RenderParent(ctx, size)
DisplayTextBox(ctx, self.currentDialogue, (0,400), (800,200), 20, True)
if self.showAccept:
DisplayTextBox(ctx, "Press A to continue", (0,0), (180,20), 15)
def InjectInput(self, event, down):
if not down and event in ['left', 'right', 'up', 'down', 'enter']:
self.currentDialogueLineIndex += 1
if(self.currentDialogueLineIndex >= len(self.dialogueList)):
self.EndDialogue()
def EndDialogue(self):
self.KillSelf()
self.endFunction()