This repository was archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturn.py
140 lines (101 loc) · 3.97 KB
/
turn.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
__author__ = 'tenpaMk2'
import logging
import logging.config
logging.config.fileConfig("config/logging.conf")
import observer
from abc import ABCMeta, abstractmethod
import npcai
class TurnManager(object):
def __init__(self):
self.queue = []
self.total_turn_count = 0
def register(self, turn_queue_entry: "TurnQueueEntryBase"):
# まずは追加。その後に次回ターンまでの時間でソートする。
self.queue.append(turn_queue_entry)
self.queue.sort(key=lambda x: x.next_turn_time)
def remove(self, turn_queue_entry: "TurnQueueEntryBase"):
self.queue.remove(turn_queue_entry)
logging.info("removed turn_queue_entry")
def update(self):
current_entry = self.queue.pop(0)
spent_time = current_entry.next_turn_time
self._decrease_turn_time(spent_time)
logging.info("start_turn: {0}".format(current_entry))
current_entry.start_turn()
def _decrease_turn_time(self, decreasing_time: int):
for entry in self.queue:
entry.decrease_turn_time(decreasing_time)
def _print_queue(self):
logging.debug("<-----queue----->")
for entry in self.queue:
logging.debug("remain time = {0}: {1}".format(entry.next_turn_time, entry))
class TurnQueueEntryBase(metaclass=ABCMeta):
@abstractmethod
def __init__(self, turn_period: int):
self.next_turn_time = turn_period
def decrease_turn_time(self, decreasing_time: int):
self.next_turn_time -= decreasing_time
@abstractmethod
def start_turn(self):
pass
class HeroTurnQueueEntry(TurnQueueEntryBase):
def __init__(self, obs: "observer.ObserverBase", turn_period: int):
super(HeroTurnQueueEntry, self).__init__(turn_period)
self.observer = obs
def start_turn(self):
self.observer.update_turn_start()
class NPCTurnQueueEntry(TurnQueueEntryBase):
def __init__(self, ai: "npcai.AIBase", turn_period: int):
super(NPCTurnQueueEntry, self).__init__(turn_period)
self.ai = ai
def decrease_turn_time(self, decreasing_time: int):
self.next_turn_time -= decreasing_time
def start_turn(self):
self.ai.act()
class TurnQueueEntryFactory(object):
@staticmethod
def make_hero_turn_queue(obs: "observer.ObserverBase", turn_period: int):
return HeroTurnQueueEntry(obs, turn_period)
@staticmethod
def make_npc_turn_queue(ai: "npcai.AIBase", turn_period: int):
return NPCTurnQueueEntry(ai, turn_period)
if __name__ == "__main__":
class ObserverDummy(observer.ObserverBase):
# noinspection PyMissingConstructor
def __init__(self, name: str="hoge"):
self.name = name
def update(self):
pass
def update_turn_start(self):
logging.debug("-----start Hero turn-----")
logging.debug(self.name)
logging.debug('')
class AIDummy(npcai.AIBase):
def __init__(self, name: str="AI"):
self.name = name
def act(self):
logging.debug("-----start NPC turn-----")
logging.debug(self.name)
logging.debug('')
tm = TurnManager()
hero_entry = TurnQueueEntryFactory.make_hero_turn_queue(ObserverDummy("I'm Hero."), 2)
villager_entry = TurnQueueEntryFactory.make_npc_turn_queue(AIDummy("I'm Villager."), 23)
sonchou_entry = TurnQueueEntryFactory.make_npc_turn_queue(AIDummy("私が村長です。"), 23)
enemy_entry = TurnQueueEntryFactory.make_npc_turn_queue(AIDummy("I'm Enemy."), 12)
boss_entry = TurnQueueEntryFactory.make_npc_turn_queue(AIDummy("I'm Boss."), 33)
tm.register(hero_entry)
tm.register(villager_entry)
tm.register(sonchou_entry)
tm.register(enemy_entry)
tm.register(boss_entry)
tm._print_queue()
tm.update()
tm._print_queue()
tm.update()
tm._print_queue()
tm.update()
tm._print_queue()
tm.update()
tm._print_queue()