Commits

Anonymous committed a51efc7

Added camera motion and zooming

Comments (0)

Files changed (1)

roguelike/main.py

 	
 	### The MadCatz controller reports a vector magnitude of >1 when the sticks
 	### are tilted towards the corners, which seems to be because of a dead
-	### zone around the outside. Negating this by normalizing the vector
-	### whenever its magnitude is >1.
+	### zone around the outside. Normalizing the vector whenever its magnitude
+	### is >1 seems to fix this.
 	if magSq > 1:
 		return normalize2d(state)
 	
 def proc_event(event):
 	for handler in event_handlers:
 		if handler[0](event):
-			handler[1]()
+			handler[1](event)
 			
 def proc_events():
 	for event in pygame.event.get():
 	while True:
 		proc_events()
 		
-		surfaceScreen.fill(colorBG)
+		surface_screen.fill(colorBG)
 		proc_event(pygame.event.Event(EVENT_DRAW))
 		pygame.display.update()
 
 
 ########################################################################### app
 
-on_event(lambda a: a.type == pygame.QUIT, lambda: sys.exit())
+on_event(lambda a: a.type == pygame.QUIT, lambda a: sys.exit())
 
-def draw_joy_input():
+def draw_joy_input(event):
 	#for joy in range(len(joysticks)):
 	for joyIndex, joy in enumerate(joysticks):
 		for button in range(joy.get_numbuttons()):
 			pygame.draw.rect(
-				surfaceScreen,
+				surface_screen,
 				(colorDebugToggleOn if joy.get_button(button) else colorDebugToggleOff),
 				pygame.Rect(button * 40 + 5, joyIndex * 50 + 10, 30, 30)
 			)
 		for i in range(len(sticks)):
 			axes = sticks[i]
 			stick_center = ((joy.get_numbuttons() + i) * 40 + 5 + 15, joyIndex * 50 + 10 + 15)
-			pygame.draw.circle(surfaceScreen, colorDebug0, stick_center, 15)
+			pygame.draw.circle(surface_screen, colorDebug0, stick_center, 15)
 			pygame.draw.line(
-				surfaceScreen, colorDebug1, stick_center,
+				surface_screen, colorDebug1, stick_center,
 				int2d(sum2d(stick_center, mag_scale_2d(get_stick(joy, axes[0], axes[1]), 15))),
 				3
 			)
 		
 		trigger_height = 30 * joy.get_axis(2)
 		pygame.draw.rect(
-			surfaceScreen,
+			surface_screen,
 			colorDebug0,
 			pygame.Rect((joy.get_numbuttons() + i) * 40 + 5 + 40, joyIndex * 50 + 10, 30, 30)
 		)
 		if trigger_height > 0:
 			pygame.draw.rect(
-				surfaceScreen,
+				surface_screen,
 				colorDebugToggleOn,
 				pygame.Rect((joy.get_numbuttons() + i) * 40 + 5 + 40, joyIndex * 50 + 10 + (30 - trigger_height), 30, trigger_height)
 			)
 		
 		### so... pygame won't detect the right-side trigger?
 		pygame.draw.rect(
-			surfaceScreen,
+			surface_screen,
 			colorDebug0,
 			pygame.Rect((joy.get_numbuttons() + i) * 40 + 5 + 80, joyIndex * 50 + 10, 30, 30)
 		)
 		for hat in range(joy.get_numhats()):
 			hat_center = ((joy.get_numbuttons() + i) * 40 + 5 + 40 + 80 + 15, joyIndex * 50 + 10 + 15)
 			pygame.draw.rect(
-				surfaceScreen,
+				surface_screen,
 				colorDebug0,
 				pygame.Rect((joy.get_numbuttons() + i) * 40 + 5 + 40 + 80, joyIndex * 50 + 10, 30, 30),
 				3
 			)
 			pygame.draw.line(
-				surfaceScreen, colorDebug1, hat_center,
+				surface_screen, colorDebug1, hat_center,
 				int2d(sum2d(hat_center, flipY2d(mag_scale_2d(joy.get_hat(hat), 15)))),
 				3
 			)
 colorHudOutline = pygame.Color(0, 0, 0)
 
 pygame.init()
-surfaceScreen = pygame.display.set_mode((600, 400)) # TODO: flags and depth
+surface_screen = pygame.display.set_mode((600, 400)) # TODO: flags and depth
 # what about fullscreen?
 
 joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
 
 
 TILE_W = 1
-TILE_D = .85
+TILE_L = .8
 TILE_H = .4
 
 
-# tile: (surface, (reg_x, reg_y), reg_diameter_horizontal)
-tile_ground = [pygame.image.load('Plain Block.png').convert_alpha(), (50, 85), 101]
-tile_ground_double = [pygame.image.load('Plain Block.png').convert_alpha(), (50, 85), 50]
-tile_ground_brown = [pygame.image.load('Brown Block.png').convert_alpha(), (50, 85), 101]
-tile_grass = [pygame.image.load('Grass Block.png').convert_alpha(), (50, 85), 101]
+# tile: (surface, (reg_x, reg_y), reg_width)
+tile_gray = (pygame.image.load('Plain Block.png').convert_alpha(), (50, 90), 100)
+tile_ground_double = (pygame.image.load('Plain Block.png').convert_alpha(), (50, 90), 50)
+tile_dirt = (pygame.image.load('Brown Block.png').convert_alpha(), (50, 90), 100)
+tile_grass = (pygame.image.load('Grass Block.png').convert_alpha(), (50, 90), 100)
+tile_water = (pygame.image.load('Water Block.png').convert_alpha(), (50, 90), 100)
+tile_rock = (pygame.image.load('Rock.png').convert_alpha(), (50, 130), 100)
+tile_tree = (pygame.image.load('Tree Tall.png').convert_alpha(), (50, 130), 100)
 
-camera = (0, 0, 6, 4) # (left, top, width, height)
+
+
+#camera = (0, 0, 6, 4) # (left, top, width, height)
+camera = (0, 0, 5) # (centerX, centerY, height) ~~~ width depends on screen
+def move_camera(delta):
+	global camera
+	camera = (camera[0] + delta[0], camera[1] + delta[1], camera[2])
+
 blocks = [] # contains: ((x, y, z), tile)
-def draw_environment():
+def draw_environment(event):
 	global blocks
-	blocks = sorted(blocks, key=lambda a: a[0][2] + a[0][1] * 1000000)#cmp=lambda a, b: a[0][2] - b[0][2])
+	blocks = sorted(blocks, key=lambda a: a[0][2] * 10000000 + a[0][1])
 	for block in blocks:
 		block_pos = block[0]
 		tile = block[1]
 		tile_surface = tile[0]
-		tile_reg_diameter = tile[2]
+		tile_reg_width = tile[2]
 		
-		scale_world2screen = 101
-		scale_image2tile = tile_surface.get_width() / tile_reg_diameter
+		scale_world2screen = surface_screen.get_height() / (camera[2] * TILE_L)
+		scale_image2tile = tile_surface.get_width() / tile_reg_width
 		
-		#dest_size = int2d(scale2d(tile_surface.get_size(), scale_image2tile))
 		dest_w = scale_world2screen * scale_image2tile
 		dest_h = dest_w * tile_surface.get_height() / tile_surface.get_width()
 		
-		surfaceScreen.blit(
+		surface_screen.blit(
 			pygame.transform.smoothscale(tile_surface, int2d((dest_w, dest_h))),
-			#dif2d(
-			#	(block[0][0] * 100, block[0][2] * 85 - block[0][1] * 40),
-			#	scale2d(tile[1], scale_image2tile)
-			#)
-			dif2d(
-				scale2d(
-					(
-						block_pos[0] * TILE_W,# - tile[1][0] * scale_world2screen / tile_reg_diameter,#  - tile[1][0] * scale_image2tile / scale_world2screen,
-						block_pos[2] * TILE_D - block_pos[1] * TILE_H
+			sum2d(
+				
+				### positioning before camera motion
+				dif2d(
+					scale2d(
+						(
+							block_pos[0] * TILE_W,
+							block_pos[2] * TILE_L - block_pos[1] * TILE_H
+						),
+						scale_world2screen
 					),
-					scale_world2screen
+					scale2d(tile[1], scale_world2screen / tile_reg_width)
 				),
-				#scale2d(tile[1], scale_image2tile / scale_world2screen)
-				scale2d(tile[1], scale_world2screen / tile_reg_diameter)
+				
+				sum2d(
+					### camera position
+					scale2d(
+						(TILE_W * (-camera[0]), TILE_L * (-camera[1] )), 
+						scale_world2screen
+					),
+					### camera registration (center)
+					scale2d(
+						(
+							#( camera[2] * surface_screen.get_height() / surface_screen.get_width()) / 2,
+							(camera[2] / 2 - .5) * surface_screen.get_width() / surface_screen.get_height(),
+							(camera[2] ) / 2 - .5
+						),
+						scale_world2screen
+					)
+				)
 			)
 		)
 on_event(lambda a: a.type == EVENT_DRAW, draw_environment)
 
 
 
-blocks.append([(1, 1, 2), tile_ground])
-blocks.append([(1, 1, 1), tile_ground])
-blocks.append([(1, 0, 1), tile_ground])
-blocks.append([(1, 0, 2), tile_ground])
-blocks.append([(0, 0, 0), tile_ground])
-blocks.append([(1, 0, 0), tile_ground])
+
+#on_event(lambda a: a.type == pygame.JOYHATMOTION, lambda a: print(a))
+
+previous_hat = (0, 0)
+def on_hat(event):
+	global camera, previous_hat
+	#delta = (event.value[0] - previous_hat[0], event.value[1] - previous_hat[1])
+	if event.value[0] != 0 and event.value[0] != previous_hat[0]:
+		camera = (camera[0] + event.value[0], camera[1], camera[2])
+	if event.value[1] != 0 and event.value[1] != previous_hat[1]:
+		camera = (camera[0], camera[1] - event.value[1], camera[2])
+	previous_hat = event.value
+on_event(lambda a: a.type == pygame.JOYHATMOTION, on_hat)
+
+pygame.key.set_repeat() # disables key repetition
+on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_UP, lambda a: move_camera((0, -1)))
+on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_RIGHT, lambda a: move_camera((1, 0)))
+on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_DOWN, lambda a: move_camera((0, 1)))
+on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_LEFT, lambda a: move_camera((-1, 0)))
+
+blocks.append([(1, 1, 2), tile_gray])
+blocks.append([(1, 1, 1), tile_gray])
+blocks.append([(1, 0, 1), tile_gray])
+blocks.append([(1, 0, 2), tile_gray])
+blocks.append([(0, 0, 0), tile_gray])
+blocks.append([(1, 0, 0), tile_gray])
 blocks.append([(3, -1, 2), tile_ground_double])
-blocks.append([(3, 0, 2), tile_ground])
-blocks.append([(2, 0, 1), tile_ground_brown])
-blocks.append([(2, 0, 2), tile_ground_brown])
+blocks.append([(5, -1, 2), tile_ground_double])
+blocks.append([(3, 0, 2), tile_gray])
+blocks.append([(2, 0, 1), tile_dirt])
+blocks.append([(2, 0, 2), tile_dirt])
 blocks.append([(2, 1, 1), tile_grass])
 blocks.append([(2, 1, 2), tile_grass])
 blocks.append([(1, 0, 3), tile_grass])
+blocks.append([(1, 0, 4), tile_water])
+blocks.append([(0, 0, 4), tile_water])
+blocks.append([(1, 0, 5), tile_dirt])
+blocks.append([(0, -1, 5), tile_dirt])
+blocks.append([(0, 0, -2), tile_dirt])
+blocks.append([(0, 0, 2), tile_dirt])
+blocks.append([(0, 0, 0), tile_rock])
+blocks.append([(2, 1, 1), tile_tree])
+blocks.append([(0, 1, -1), tile_gray])
 
 fps_clock = pygame.time.Clock()
 gui_font = pygame.font.SysFont('Verdana', 18)
-def draw_fps():
+def draw_fps(event):
 	fps_clock.tick()
 	
 	fps_string = 'FPS: ' + str(int(fps_clock.get_fps()))
 	
 	fps_surface = gui_font.render(fps_string, True, colorHudOutline)
-	surfaceScreen.blit(fps_surface, (-2, 0))
-	surfaceScreen.blit(fps_surface, (2, 0))
-	surfaceScreen.blit(fps_surface, (0, 2))
-	surfaceScreen.blit(fps_surface, (0, -2))
+	surface_screen.blit(fps_surface, (-2, 0))
+	surface_screen.blit(fps_surface, (2, 0))
+	surface_screen.blit(fps_surface, (0, 2))
+	surface_screen.blit(fps_surface, (0, -2))
 	
 	fps_surface = gui_font.render(fps_string, True, colorHud)
-	surfaceScreen.blit(fps_surface, (0, 0))
+	surface_screen.blit(fps_surface, (0, 0))
 on_event(lambda a: a.type == EVENT_DRAW, draw_fps)