metalwork /robots/scene.py

Language Python Lines 309
MD5 Hash afe9a03705e76fe793b33a00b1cc6341 Estimated Cost $4,672 (why?)
Repository https://bitbucket.org/lordmauve/metalwork View Raw File View Project SPDX
  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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import re
import os.path
import math

import pygame
from pygame.locals import *

from world import World

ALT_INCREMENT = 35
IMAGES_ROOT = 'assets/images'

def load_sprite(fname):
	return pygame.image.load(os.path.join(IMAGES_ROOT, fname)).convert_alpha()


class Actor(object):
	"""An actor is an object with visual presence, but not necessarily physical
	presence, in the 3D world."""
	def __init__(self, x, y, alt=0, description=None):
		self.pos = x, y # actual position
		self.display_pos = None 
		self.alt = alt
		self.display_alt = None
		self.description = description

	def play(self, name):
		"""Play a named animation.

		If name is None, configure the base animation."""

	def position(self):
		return self.display_pos or self.pos

	def altitude(self):
		return self.display_alt or self.alt

	def draw_center(self, screen, sprite, x, y):
		w, h = sprite.get_size()
		screen.blit(sprite, (x - w/2, y - h/2))
	
	def tooltip(self):
		return re.sub(r'([a-z])([A-Z])', r'\1 \2', self.__class__.__name__)

	def is_pushable(self, direction):
		return False


#class AnimatedSprite(object):
#	class Instance(object):
#		def __init__(self, a):
#			self.a = a
#			self.ftime = 0
#			self.currentframe = 0
#			self.finished = False
#
#		def draw(self, screen, x, y):
#			framedelay = self.a.framedelay
#			frames = self.a.frames
#
#			if self.finished:
#				return
#
#			self.ftime += 1
#			if self.ftime == framedelay:
#				self.ftime = 0
#				self.currentframe += 1
#				if self.currentframe == len(frames):
#					if self.a.loop:
#						self.currentframe = 0
#					else:
#						self.currentframe = len(frames) - 1
#						self.finished = True
#
#			dx, dy, f = frames[self.currentframe]
#			w, h = f.get_size()
#			x = x + dx - w/2
#			y = y + dy - h/2
#			screen.blit(f, (x, y))
#
#		def is_finished(self):
#			return self.finished
#
#	def __init__(self, frames, framedelay=3, loop=True):
#		self.framedelay = framedelay
#		self.frames = frames
#		self.loop = loop
#
#	def new(self):
#		return AnimatedSprite.Instance(self)
#
#	@staticmethod
#	def load(path, range, framedelay=3, loop=True):
#		fs = [(0, 0, load_sprite(path % i)) for i in range]
#		return AnimatedSprite(fs, framedelay, loop)


class Viewport(object):
	TILE_SIZE = (124, 72)
	def __init__(self, w=640, h=480, x=0, y=0):
		self.x = x
		self.y = y
		self.w = w
		self.h = h

	def world_pos(self, tx, ty):
		"""Center point of the square in the world"""
		tw, th = self.TILE_SIZE
		return int((tx - ty + 1) * (tw / 2 - 1) + 0.5), int((tx + ty + 1) * (th / 2 - 1) + 0.5)

	def screen_pos(self, t_x, t_y, alt=0):
		"""Center of the square on the screen"""
		x, y = self.world_pos(t_x, t_y)
		return x - int(self.x), y - int(self.y) - alt * ALT_INCREMENT

	def translate(self, dx, dy):
		self.x -= dx
		self.y -= dy

	def translate_tiles(self, tx, ty):
		wx0, wy0 = self.world_pos(0, 0)
		wx, wy = self.world_pos(tx, ty)
		self.x -= wx - wx0
		self.y -= wy - wy0

	def move_to(self, tx, ty):
		self.x, self.y = self.world_pos(tx, ty)
		self.x -= self.w/2
		self.y -= self.h/2

	def tile_for_pos(self, sx, sy):
		tw, th = self.TILE_SIZE
		htw = tw / 2 - 1
		hth = th / 2 - 1
		sx = float(sx + self.x)
		sy = float(sy + self.y)
		tx = (sx / htw + sy / hth) / 2 - 1
		ty = sy / hth - tx - 1
		return int(math.floor(tx + 0.5)), int(math.floor(ty + 0.5))

	def screen_bounds(self, actor):
		r = actor.bounds()
		ax, ay = actor.position()
		x, y = self.screen_pos(ax, ay, actor.altitude())
		return pygame.Rect(r.left + x, r.top + y, r.width, r.height)


class Scene(object):
	"""The scene is a 2D representation of a 3D World.

	This class knows how to draw that world and handle mapping of input to world space. A scene
	also contains a list of actors which are displayed in the 3D world but are not physical objects.
	Icons such as move arrows, require this functionality.

	The scene also maintains a list of animations, playing in the world, which
	represent the transition between one world state and another.
	"""
	TILE = 0
	ACTOR = 1

	def __init__(self, world, viewport):
		self.world = world
		self.viewport = viewport
		self.viewport.move_to(self.world.w/2, self.world.h/2)
		self.animations = []
		self.actions = []
		self.gamestate = None
		self.tilecache = None

		self.hovered = None

		self.load_actions()

		self.world.start()

	def load_actions(self):
		"""Graphics for actions get pre-loaded automagically when the scene
		starts. We could try to do this by listing dependencies for objects,
		but then we would have to work around circular import problems."""

		from robots import actions
		for cls in actions.__dict__.values():
			try:
				is_act = issubclass(cls, actions.Action)
			except TypeError:
				continue
			else:
				if is_act:
					self.world.load(cls)

	def draw_order(self, x, y, alt, type):
		return math.ceil(x + y + 0.1 * type), alt, type, x + y

	def tiles(self):
		if self.tilecache:
			return self.tilecache
		ts = []
		for x, y, tile, draw_south, draw_east in self.world.get_tiles():
			if tile is not None:
				ts.append((self.draw_order(x, y, tile.alt, Scene.TILE), Scene.TILE, (x, y, tile.alt), tile, draw_south, draw_east))
		ts.sort()
		self.tilecache = ts
		return ts

	def actors(self):
		ts = []
		for a in self.world.actors + self.actions:
			ax, ay = a.position()
			ts.append((self.draw_order(ax, ay, a.altitude(), Scene.ACTOR), Scene.ACTOR, a))
		return ts

	def add(self, a):
		self.actions.append(a)
		self.world.load(a.__class__)
		a.play(None)

	def remove(self, a):
		self.actions.remove(a)

	def remove_all(self):
		self.actions = []

	def draw_tile(self, screen, t):
		p, t, draw_south, draw_east = t
		x, y, alt = p
		sx, sy = self.viewport.screen_pos(x, y, alt)
		screen.start(hash((id(t), x, y)))
		t.draw(screen, sx, sy, draw_south, draw_east, x, y)
			
	def draw_actor(self, screen, act):
		screen.start(id(act))
		a = act[0]
		x, y = a.position()
		sx, sy = self.viewport.screen_pos(x, y, a.altitude())
		a.draw(screen, sx, sy)

	def draw(self, scenegraph):
		"""Draw the world."""
		scenegraph.fill(0)
		s = self.tiles() + self.actors()
		s.sort()
		for i in s:
			if i[1] == Scene.TILE:
				self.draw_tile(scenegraph, i[2:])
			elif i[1] == Scene.ACTOR:
				self.draw_actor(scenegraph, i[2:])

	def update(self):
		for anim in self.animations[:]:
			anim.update()
			if anim.is_finished():
				self.animations.remove(anim)

	def add_animation(self, a):
		self.animations.append(a)

	def animation_playing(self):
		return len(self.animations) > 0

	def actors_sorted(self, pos):
		"""Actors sorted in hit order.

		The hit testing checks for actions first, as action graphics
		are generally smaller than objects, and so they are clickable
		"through" anything else."""
		from robots.actions import Action
		actors = [a for a in self.actors() if self.viewport.screen_bounds(a[-1]).collidepoint(pos)]
		actors.sort()
		actors.reverse()# detect clicks in reverse draw order
		actions = []
		objects = []
		for a in actors:
			actor = a[-1]
			if isinstance(actor, Action):
				actions.append(actor)
			else:
				objects.append(actor)
		return actions + objects

	def onclick(self, pos):
		if not self.gamestate:
			return
		if self.animation_playing():
			return
		for a in self.actors_sorted(pos):
			if self.gamestate.onclick(a, pos):
				break

	def update_hover(self, pos):
		if not self.gamestate:
			return
		if self.animation_playing():
			if self.hovered:
				self.gamestate.onunhover(self.hovered)
				self.hovered = None
			return
		for a in self.actors_sorted(pos):
			if self.hovered == a:
				return

			if self.gamestate.onhover(a):
				self.gamestate.onunhover(self.hovered)
				self.hovered = a
				return

		if self.hovered:
			self.gamestate.onunhover(self.hovered)
			self.hovered = None
Back to Top