-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActorController.py
108 lines (95 loc) · 3.25 KB
/
ActorController.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
#coding:utf-8
from collections import deque
from Actor import Actor
from Actor import Enemy
from Actor import Player
import random as rnd
class ActorController():
def __init__(self):
self.actors={}
self.move_queue=deque()
self.player=None
self.time=1
def update_target(self,target):
for _,actor in self.actors.items():
if actor!=target:
actor.target=target
def set_actor_as_player(self,player):
if player not in self.actors:
assert ("argument is not in self.actors")
self.player=player
def del_actor_as_player(self,player):
if player not in self.actors:
assert("argument is not in self.actors")
for _,actor in self.actors.items():
if hasattr(actor,"target"):
if actor.target==player:
actor.target=None
self.player=None
del_id=player.act_id
self.actors.pop(del_id)
def add_actor(self,actor,target=None):
l=[i for i in self.actors]
new_id=-1
for n in range(len(l)+1):
if n not in l:
new_id=n
break
actor.target= target
actor.act_id = new_id
self.actors.update({new_id: actor})
def add_actor_as_player(self,actor):
self.add_actor(actor)
self.set_actor_as_player(actor)
def del_actor(self,del_target): #act_id or Actor どちらでも可
del_id=None
if type(del_target)==int:
del_id=del_target
elif type(del_target)==Actor or Enemy or Player:
del_id=del_target.act_id
self.actors.pop(del_id)
def tick_for_move_queue(self):
#print(self.move_queue)
if self.move_queue:
return 0
id_list=[[x.act_id,x.SPD] for _,x in self.actors.items()]
movables = []
#print(id_list)
while True:
for actor in id_list:
if self.time%(1023//actor[1])==0 and actor not in movables:
movables.append(actor)
if len(movables)==0:
self.time=max((self.time+1)%1024,1)
continue
else:
movables.sort(reverse=True,key=lambda x:x[1])
self.move_queue+=[self.actors[x[0]]for x in movables]
break
self.time = max((self.time + 1) % 1024, 1)
return 0
def pick_next_actor(self):
self.tick_for_move_queue()
return self.move_queue.popleft()
def get_next_actor(self):
self.tick_for_move_queue()
return self.move_queue[0]
def get_action(self):
self.tick_for_move_queue()
print(self.move_queue)
actor=self.move_queue.popleft()
print(actor.name)
res={"act_id":actor.act_id}
res.update(actor.get_action())
return res
def get_random_param():
return {"HP":1,"x":20,"y":2,"job":"test","SPD":rnd.randint(30,60)}
if __name__=="__main__":
actCtr=ActorController()
p=Player({"SPD":15,"x":1,"y":2})
actCtr.add_actor_as_player(p)
actCtr.add_actor(Enemy({"SPD":20,"x":2,"y":2}),target=actCtr.player)
print([x.SPD for _,x in actCtr.actors.items()])
for i in range(100):
request=actCtr.get_action()
print(request)