Commits

Flaise  committed 5968dc8

Added shadows

  • Participants
  • Parent commits 17243f5

Comments (0)

Files changed (10)

File roguelike/Shadow East.png

Added
New image

File roguelike/Shadow North East.png

Added
New image

File roguelike/Shadow North West.png

Added
New image

File roguelike/Shadow North.png

Added
New image

File roguelike/Shadow Side West.png

Added
New image

File roguelike/Shadow South East.png

Added
New image

File roguelike/Shadow South West.png

Added
New image

File roguelike/Shadow South.png

Added
New image

File roguelike/Shadow West.png

Added
New image

File roguelike/main.py

 '''
 Created: Aug 4, 2013
-Last modified: Aug 7, 2013 
+Last modified: Aug 8, 2013 
 '''
 
 import pygame
 def dif2d(a, b):
 	return (a[0] - b[0], a[1] - b[1])
 
+def sum3d(a, b):
+	return (a[0] + b[0], a[1] + b[1], a[2] + b[2])
+
 def magnitudeSq2d(vec):
 	return vec[0] ** 2 + vec[1] ** 2
 def magnitude2d(vec):
 def event_loop():
 	while True:
 		proc_events()
-		
-		surface_screen.fill(colorBG)
 		proc_event(pygame.event.Event(EVENT_DRAW))
-		pygame.display.update()
+		#pygame.display.update()
+		pygame.display.flip()
 
 def on_event(predicate, callback):
 	### TODO: take priorty as parameter so screen flip and hud drawing can be
 
 ########################################################################### app
 
+pygame.init()
+#pygame.mouse.set_cursor(pygame.cursors.)
+pygame.mouse.set_visible(False)
+surface_screen = pygame.display.set_mode(
+	#(0, 0),
+	#(600, 400),
+	(1024, 768),
+	#(1280, 720),
+	#(1440, 900),
+	#(1600, 1200),
+	#pygame.FULLSCREEN# |
+	#| pygame.DOUBLEBUF
+	#| pygame.HWSURFACE
+)
+pygame.display.set_caption('Roguelike v0.0.1')
+
+
+
+
+on_event(lambda a: a.type == EVENT_DRAW, lambda a: surface_screen.fill(colorBG))
 on_event(lambda a: a.type == pygame.QUIT, lambda a: sys.exit())
+on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_ESCAPE, lambda a: sys.exit())
 
 def draw_joy_input(event):
 	#for joy in range(len(joysticks)):
 colorHud = pygame.Color(255, 255, 255)
 colorHudOutline = pygame.Color(0, 0, 0)
 
-pygame.init()
-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())]
 for joy in joysticks:
 	joy.init()
 TILE_H = .4
 
 
-# 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)
+# tile: (surface, (reg_x, reg_y), reg_width, fills_space)
+tile_gray = (pygame.image.load('Plain Block.png').convert_alpha(), (50, 90), 100, True)
+#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, True)
+tile_grass = (pygame.image.load('Grass Block.png').convert_alpha(), (50, 90), 100, True)
+tile_water = (pygame.image.load('Water Block.png').convert_alpha(), (50, 90), 100, True)
+tile_rock = (pygame.image.load('Rock.png').convert_alpha(), (50, 90), 100, False)
+tile_tree = (pygame.image.load('Tree Tall.png').convert_alpha(), (50, 90), 100, False)
 
+shadow_e = (pygame.image.load('Shadow East.png'), (50, 90), 100)
+shadow_w = (pygame.image.load('Shadow West.png'), (50, 90), 100)
+shadow_s = (pygame.image.load('Shadow South.png'), (50, 90), 100)
+shadow_n = (pygame.image.load('Shadow North.png'), (50, 90), 100)
+shadow_side_w = (pygame.image.load('Shadow Side West.png'), (50, 90), 100)
+shadow_se = (pygame.image.load('Shadow South East.png'), (50, 90), 100)
+shadow_nw = (pygame.image.load('Shadow North West.png'), (50, 90), 100)
+shadow_sw = (pygame.image.load('Shadow South West.png'), (50, 90), 100)
+shadow_ne = (pygame.image.load('Shadow North East.png'), (50, 90), 100)
 
 
 #camera = (0, 0, 6, 4) # (left, top, width, height)
 	global camera
 	camera = (camera[0], camera[1], max(1, camera[2] + delta))
 
+blocks_changed = False
 blocks = [] # contains: ((x, y, z), tile)
-def draw_environment(event):
-	global blocks
-	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_width = tile[2]
-		
-		scale_world2screen = surface_screen.get_height() / (camera[2] * TILE_L)
-		scale_image2tile = tile_surface.get_width() / tile_reg_width
-		
-		dest_w = scale_world2screen * scale_image2tile
-		dest_h = dest_w * tile_surface.get_height() / tile_surface.get_width()
-		
-		surface_screen.blit(
-			pygame.transform.smoothscale(tile_surface, int2d((dest_w, dest_h))),
+position_to_block = {}
+def draw_tile(tile, position):
+	tile_surface = tile[0]
+	tile_reg_width = tile[2]
+	
+	scale_world2screen = surface_screen.get_height() / (camera[2] * TILE_L)
+	scale_image2tile = tile_surface.get_width() / tile_reg_width
+	
+	dest_w = scale_world2screen * scale_image2tile
+	dest_h = dest_w * tile_surface.get_height() / tile_surface.get_width()
+	surface_screen.blit(
+		pygame.transform.smoothscale(tile_surface, int2d((dest_w, dest_h))),
+		sum2d(
+			### positioning before camera motion
+			dif2d(
+				scale2d(
+					(
+						position[0] * TILE_W,
+						position[2] * TILE_L - position[1] * TILE_H
+					),
+					scale_world2screen
+				),
+				scale2d(tile[1], scale_world2screen / tile_reg_width)
+			),
+			
 			sum2d(
-				
-				### positioning before camera motion
-				dif2d(
-					scale2d(
-						(
-							block_pos[0] * TILE_W,
-							block_pos[2] * TILE_L - block_pos[1] * TILE_H
-						),
-						scale_world2screen
-					),
-					scale2d(tile[1], scale_world2screen / tile_reg_width)
+				### camera position
+				scale2d(
+					(TILE_W * (-camera[0]), TILE_L * (-camera[1] )), 
+					scale_world2screen
 				),
-				
-				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) * surface_screen.get_width() / surface_screen.get_height(),
-					#		(camera[2] ) / 2
-					#	),
-					#	scale_world2screen
-					#)
-					
-					#scale2d(
-					#	((camera[2] - 1) * TILE_W * surface_screen.get_width() / surface_screen.get_height() / 2, (camera[2] - .5) * TILE_L / 2),
-					#	scale_world2screen
-					#)
-					
-					#(300, 200)
-					
-					(surface_screen.get_width() / 2, surface_screen.get_height() / 2)
-				)
+				(surface_screen.get_width() / 2, surface_screen.get_height() / 2)
 			)
 		)
+	)
+def draw_environment(event):
+	global blocks_changed
+	if blocks_changed:
+		blocks.sort(key=lambda a: a[0][2] * 10000000 + a[0][1])
+		for block in blocks:
+			position_to_block[block[0]] = block
+		blocks_changed = False
+	
+	for block in blocks:
+		tile = block[1]
+		position = block[0]
+		draw_tile(tile, position)
+		if tile[3]:
+			### surface shadows
+			if not is_filler_at(sum3d(position, (0, 1, 0))):
+				if is_filler_at(sum3d(position, (1, 1, 0))):
+					draw_tile(shadow_e, position)
+				if is_filler_at(sum3d(position, (-1, 1, 0))):
+					draw_tile(shadow_w, position)
+				if is_filler_at(sum3d(position, (0, 1, 1))):
+					draw_tile(shadow_s, position)
+				if is_filler_at(sum3d(position, (0, 1, -1))):
+					draw_tile(shadow_n, position)
+				
+				### corners
+				if not is_filler_at(sum3d(position, (0, 1, 1))):
+					if is_filler_at(sum3d(position, (1, 1, 1))) and not is_filler_at(sum3d(position, (1, 1, 0))):
+						draw_tile(shadow_se, position)
+					if is_filler_at(sum3d(position, (-1, 1, 1))) and not is_filler_at(sum3d(position, (-1, 1, 0))):
+						draw_tile(shadow_sw, position)
+				if is_filler_at(sum3d(position, (-1, 1, -1))) and not is_filler_at(sum3d(position, (0, 1, -1))) and not is_filler_at(sum3d(position, (-1, 1, 0))):
+					draw_tile(shadow_nw, position)
+				if is_filler_at(sum3d(position, (1, 1, -1))) and not is_filler_at(sum3d(position, (0, 1, -1))) and not is_filler_at(sum3d(position, (1, 1, 0))):
+					draw_tile(shadow_ne, position)
+			
+			### front shadows
+			if not is_filler_at(sum3d(position, (0, 0, 1))):
+				if is_filler_at(sum3d(position, (-1, 0, 1))):
+					draw_tile(shadow_side_w, position)
 		
 	#pygame.draw.rect(surface_screen, colorHud, pygame.Rect(300 - 2, 200 - 2, 4, 4)) # centered guide dot
 on_event(lambda a: a.type == EVENT_DRAW, draw_environment)
 
+#def get_blocks(predicate):
+#	for block in blocks:
+#		if predicate(block):
+#			yield block
+#def get_blocks_at(position):
+#	yield from get_blocks(lambda a: a[0] == position)
+def is_filler_at(position):
+	"""for block in blocks:
+		block_pos = block[0]
+		if block_pos != position:
+			continue
+		tile = block[1]
+		if tile[3]:
+			return True
+	return False"""
+	if position in position_to_block:
+		if position_to_block[position][1][3]:
+			return True
+	return False
 
 
 
 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([(5, -1, 2), tile_ground_double])
+#blocks.append([(3, -1, 2), tile_ground_double])
+#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([(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, 0), tile_rock])
+blocks.append([(2, 2, 1), tile_tree])
 blocks.append([(0, 1, -1), tile_gray])
+blocks.append([(-1, 2, -1), tile_gray])
+blocks.append([(-1, 1, -1), tile_gray])
+blocks.append([(-1, 0, -1), tile_gray])
+blocks.append([(-1, -1, 0), tile_dirt])
+blocks.append([(-1, -1, 1), tile_water])
+blocks.append([(0, -1, 1), tile_water])
+blocks.append([(2, 1, -1), tile_grass])
+blocks.append([(2, 0, 0), tile_dirt])
+blocks.append([(3, 0, 0), tile_dirt])
+blocks.append([(1, 0, -1), tile_dirt])
+blocks_changed = True
+
 
 fps_clock = pygame.time.Clock()
 gui_font = pygame.font.SysFont('Verdana', 18)
 	fps_string = 'FPS: ' + str(int(fps_clock.get_fps()))
 	
 	fps_surface = gui_font.render(fps_string, True, colorHudOutline)
-	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))
+	#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))
+	surface_screen.blit(fps_surface, (-1, -1))
+	surface_screen.blit(fps_surface, (1, 1))
+	surface_screen.blit(fps_surface, (-1, 1))
+	surface_screen.blit(fps_surface, (1, -1))
 	
 	fps_surface = gui_font.render(fps_string, True, colorHud)
 	surface_screen.blit(fps_surface, (0, 0))