Commits

Flaise  committed 8daff13

Added player motion controls and obstacle detection

  • Participants
  • Parent commits 5968dc8

Comments (0)

Files changed (1)

File roguelike/main.py

 
 ########################################################################### app
 
+EVENT_IN_NORTH = 1000
+EVENT_IN_EAST = 1001
+EVENT_IN_SOUTH = 1002
+EVENT_IN_WEST = 1003
+on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_UP, lambda a: proc_event(pygame.event.Event(EVENT_IN_NORTH)))
+on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_DOWN, lambda a: proc_event(pygame.event.Event(EVENT_IN_SOUTH)))
+on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_RIGHT, lambda a: proc_event(pygame.event.Event(EVENT_IN_EAST)))
+on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_LEFT, lambda a: proc_event(pygame.event.Event(EVENT_IN_WEST)))
+
+previous_hat = (0, 0)
+def hat_to_direction4(event):
+	global camera, previous_hat
+	if event.value[0] != previous_hat[0]:
+		if event.value[0] == 1:
+			proc_event(pygame.event.Event(EVENT_IN_EAST))
+		elif event.value[0] == -1:
+			proc_event(pygame.event.Event(EVENT_IN_WEST))
+	if event.value[1] != previous_hat[1]:
+		if event.value[1] == 1:
+			proc_event(pygame.event.Event(EVENT_IN_NORTH))
+		elif event.value[1] == -1:
+			proc_event(pygame.event.Event(EVENT_IN_SOUTH))
+	previous_hat = event.value
+on_event(lambda a: a.type == pygame.JOYHATMOTION, lambda a: hat_to_direction4(a))
+
 pygame.init()
 #pygame.mouse.set_cursor(pygame.cursors.)
 pygame.mouse.set_visible(False)
 	#| pygame.DOUBLEBUF
 	#| pygame.HWSURFACE
 )
-pygame.display.set_caption('Roguelike v0.0.1')
+pygame.display.set_caption('Roguelike v0.0.2')
 
 
 
 TILE_H = .4
 
 
-# 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)
+# tile: (surface, (reg_x, reg_y), reg_width, filler, can_stand_on)
+tile_gray = (pygame.image.load('Plain Block.png').convert_alpha(), (50, 90), 100, True, True)
+tile_dirt = (pygame.image.load('Brown Block.png').convert_alpha(), (50, 90), 100, True, True)
+tile_grass = (pygame.image.load('Grass Block.png').convert_alpha(), (50, 90), 100, True, True)
+tile_water = (pygame.image.load('Water Block.png').convert_alpha(), (50, 90), 100, True, True)
+tile_rock = (pygame.image.load('Rock.png').convert_alpha(), (50, 90), 100, False, True)
+tile_tree = (pygame.image.load('Tree Tall.png').convert_alpha(), (50, 90), 100, False, False)
+tile_player = (pygame.image.load('Character Cat Girl.png').convert_alpha(), (50, 90), 100, False, False)
 
 shadow_e = (pygame.image.load('Shadow East.png'), (50, 90), 100)
 shadow_w = (pygame.image.load('Shadow West.png'), (50, 90), 100)
 def move_camera(delta):
 	global camera
 	camera = (camera[0] + delta[0], camera[1] + delta[1], camera[2])
+def move_camera_to(dest):
+	global camera
+	camera = (dest[0], dest[1], camera[2])
 def zoom_camera(delta):
 	global camera
 	camera = (camera[0], camera[1], max(1, camera[2] + delta))
 	global blocks_changed
 	if blocks_changed:
 		blocks.sort(key=lambda a: a[0][2] * 10000000 + a[0][1])
+		position_to_block.clear()
 		for block in blocks:
 			position_to_block[block[0]] = block
 		blocks_changed = False
 #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 is_block_at(position):
 		if position_to_block[position][1][3]:
 			return True
 	return False
+def is_block_at(position):
+	return position in position_to_block
 
+def move_block(block, delta):
+	block[0] = sum3d(block[0], delta)
+	
+	global blocks_changed
+	blocks_changed = True
 
+def can_stand_on(position):
+	if position in position_to_block:
+		if position_to_block[position][1][4]:
+			return True
+	return False
+def move_player(block, delta):
+	dest = sum3d(block[0], delta)
+	above = sum3d(dest, (0, 1, 0))
+	below = sum3d(dest, (0, -1, 0))
+	
+	if is_block_at(dest):
+		if is_block_at(above):
+			return False
+		elif can_stand_on(dest):
+			move_block(block, sum3d(delta, (0, 1, 0)))
+			return True
+		else:
+			return False
+	else:
+		if is_block_at(below):
+			if can_stand_on(below):
+				move_block(block, delta)
+				return True
+			else:
+				return False
+		
+		### just using an arbitrary search depth for now
+		for i in range(2, 10):
+			if is_block_at(sum3d(dest, (0, -i, 0))):
+				if can_stand_on(sum3d(dest, (0, -i, 0))):
+					move_block(block, sum3d(delta, (0, 1 - i, 0)))
+					return True
+				else:
+					return False
+		return False
 
+def move_player_and_camera(block, delta):
+	if move_player(block, delta):
+		move_camera_to((block[0][0], block[0][2]))
 
-#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(1, 1)
-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)))
-on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_EQUALS, lambda a: zoom_camera(-1))
-on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_MINUS, lambda a: zoom_camera(1))
 
 blocks.append([(1, 1, 2), tile_gray])
 blocks.append([(1, 1, 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([(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, 0, 0), tile_dirt])
 blocks.append([(3, 0, 0), tile_dirt])
 blocks.append([(1, 0, -1), tile_dirt])
+blocks.append([(3, 1, 1), tile_gray])
+blocks.append([(3, 2, 1), tile_dirt])
+blocks.append([(3, 3, 1), tile_grass])
+blocks.append([(3, 2, 2), tile_grass])
+blocks.append([(3, 1, 2), tile_dirt])
+
+block_player = [(0, 1, 0), tile_player]
+blocks.append(block_player)
+
 blocks_changed = True
 
 
+
+on_event(lambda a: a.type == EVENT_IN_NORTH, lambda a: move_player_and_camera(block_player, (0, 0, -1)))
+on_event(lambda a: a.type == EVENT_IN_SOUTH, lambda a: move_player_and_camera(block_player, (0, 0, 1)))
+on_event(lambda a: a.type == EVENT_IN_EAST, lambda a: move_player_and_camera(block_player, (1, 0, 0)))
+on_event(lambda a: a.type == EVENT_IN_WEST, lambda a: move_player_and_camera(block_player, (-1, 0, 0)))
+
+on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_EQUALS, lambda a: zoom_camera(-1))
+on_event(lambda a: a.type == pygame.KEYDOWN and a.key == pygame.K_MINUS, lambda a: zoom_camera(1))
+
+
 fps_clock = pygame.time.Clock()
 gui_font = pygame.font.SysFont('Verdana', 18)
 def draw_fps(event):
 
 assert mag_scale_to_2d((1, 0), 2) == (2, 0)
 assert mag_scale_to_2d((-2, 0), .5) == (-.5, 0)
-
-#print(mag_scale_to_2d((1, 1), 1))
-#print((math.sqrt(2), math.sqrt(2)))
 assert mag_scale_to_2d((1, 1), 1) == (1 / math.sqrt(2), 1 / math.sqrt(2))