-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamedata.py
197 lines (147 loc) · 6.08 KB
/
gamedata.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import pygame
import sys
import math
from map import *
from character import *
from combattext import *
from vector2 import *
class GameData:
res = Vector2(1280, 720)
tile_size = Vector2(32, 32)
map_pos = Vector2(0, 0)
map_size = Vector2(28 * 32, 22 * 32)
characters = []
projectiles = []
images = {}
game_over = False
tracked_character = None
combat_text = []
def load_map(self, filename, tileset):
self.map_data = Map(self.tile_size)
self.map_data.load(filename, tileset)
def init_pygame(self):
pygame.init()
self.screen = pygame.display.set_mode(self.res.to_int_tuple())
pygame.display.set_caption("Pyrogue")
def init_assets(self):
self.font = pygame.freetype.Font("font/Deutsch.ttf", 12)
self.images["Blood"] = pygame.image.load("images/blood.png")
def get_image(self, name):
if (name in self.images):
return self.images[name]
else:
print(f"Can't find image {name}!")
def set_player_archetype(self, archetype, level):
self.player_archetype = archetype
self.player_level = level
def spawn_player(self, c, x, y, param):
self.player = Character(self.player_archetype, self.player_level, Vector2(x, y), 0, self)
#self.player.controller = KeyboardController(self.player)
self.characters.append(self.player)
def spawn_enemy(self, c, x, y, param):
enemy = Character(param[0], param[1], Vector2(x, y), 1, self)
#enemy.controller = AIController(enemy)
self.characters.append(enemy)
def get_enemy_in_position(self, pos, faction):
for character in self.characters:
if (self.is_hostile(character.faction,faction)):
if (character.position == pos) and (character.health > 0):
return character
return None
def get_projectile_at_position(self, pos):
for p in self.projectiles:
if (p.position == pos) and (p.duration > 0):
return p
return None
def get_character_in_position(self, pos):
for character in self.characters:
if (character.position == pos) and (character.health > 0):
return character
return None
def get_closest_enemy(self, pos, faction):
closest_dist = sys.float_info.max
closest = None
for character in self.characters:
if (self.is_hostile(character.faction,faction)):
if (character.health > 0):
dist = Vector2.distance(pos, character.position)
if (dist < closest_dist):
closest_dist = dist
closest = character
return closest
def is_hostile(self, f1, f2):
return f1 != f2
def track_character(self, character):
self.tracked_character = character
def center_camera(self, pos):
ts = Vector2(self.map_size.x / self.tile_size.x, self.map_size.y / self.tile_size.y)
camera_pos = pos - ts/2
if (camera_pos.x + ts.x >= self.map_data.size.x):
camera_pos.x = self.map_data.size.x - ts.x
if (camera_pos.y + ts.y >= self.map_data.size.y):
camera_pos.y = self.map_data.size.y - ts.y
if (camera_pos.x < 0):
camera_pos.x = 0
if (camera_pos.y < 0):
camera_pos.y = 0
self.camera_pos = camera_pos
def render(self):
self.render_map()
self.render_characters()
self.render_projectiles()
self.render_stats()
for ct in self.combat_text:
ct.render(self.screen)
if (self.game_over):
center_text_xy(self.screen, self.res / 2, self.font, " GAME OVER ", (255, 0, 0), 60, (0, 0, 0))
def render_map(self):
self.map_data.draw(self.map_pos, self.map_size, self.camera_pos, self.screen)
def render_characters(self):
for character in self.characters:
character.render()
def render_projectiles(self):
for p in self.projectiles:
p.render()
def render_stats(self):
mx = self.map_pos.x + self.map_size.x
rect = (mx, 0, self.res.x - mx, self.res.y)
pygame.draw.rect(self.screen, (0,0,0), rect, 0)
y = self.player.render_stats(self.map_pos + Vector2(self.map_size.x + 10, 10))
y = y + 30
y = self.render_player_actions(y)
if (self.tracked_character != None):
if (self.tracked_character.health <= 0):
self.tracked_character = None
else:
y = y + 30
y = self.tracked_character.render_stats(Vector2(self.map_pos.x + self.map_size.x + 10, y))
def render_player_actions(self, y):
# Write player actions (instructions)
return self.player.render_actions(Vector2(self.map_pos.x + self.map_size.x + 10, y))
def tick(self):
for character in self.characters:
if (character.health > 0):
character.update()
for projectile in self.projectiles:
projectile.tick()
self.projectiles = [p for p in self.projectiles if p.duration > 0]
for character in self.characters:
if (character.health > 0):
character.upkeep()
if (self.player.health <= 0):
self.game_over = True
def spawn_combat_text(self, tile_pos, color, text, size, life = 1, total_delta = 40):
pos = self.convert_tile_pos_to_pixel(tile_pos)
pos = Vector2(pos.x + self.tile_size.x / 2, pos.y)
self.combat_text.append(CombatText(pos, color, text, self.font, size, life, total_delta))
def convert_tile_pos_to_pixel(self, tile_pos):
p = self.map_pos + self.tile_size * (tile_pos - self.camera_pos)
return p
def animate(self, elapsed_time):
for ct in self.combat_text:
ct.update(elapsed_time)
self.combat_text = [ct for ct in self.combat_text if ct.life > 0]
def add_projectile(self, projectile):
self.projectiles.append(projectile)
global GAMEDATA
GAMEDATA = GameData()