Commit 261948c4 authored by tgw3ff's avatar tgw3ff
Browse files

Implement Safe Player Spawning, Hole Functionality, Level Advancement, Level...

Implement Safe Player Spawning, Hole Functionality, Level Advancement, Level Score, Obstacle Destruction

The player will now always spawn in a non-obstructed location.

When the player steps onto a hole, they will be teleported to the hole's
destination.

If the hole has no destination, the player will instead by spawned into a
new level/map.

The player node now contains a level counter to track the player's progress.
This score is also displayed on screen. This needs to be tidied up,
graphically.

If the player collides with a destructable obstacle, the obstacle will now
be destroyed. This takes the player's full turn to do, however.
parent 00a73582
58046
C:\Users\Username\Desktop\Godot_v3.2.3-stable_mono_win64\Godot_v3.2.3-stable_mono_win64.exe
......@@ -10,14 +10,17 @@ var inputs = {"right": Vector2.RIGHT,
"down": Vector2.DOWN}
var direction = "right"
var texture_node = "player"
var previous_coord = null
var level = 1
func _ready():
position = position.snapped(Vector2.ONE * tile_size)
position += Vector2.ONE * tile_size/2
position = Vector2.ONE * tile_size/2
speed = 12
action_taken = false
get_node("../../TileMap").update_map()
# use this if you want to only move on keypress
func _unhandled_input(event):
for dir in inputs.keys():
......@@ -31,6 +34,9 @@ func _unhandled_input(event):
move(dir)
func move(dir: String):
# Update previous position
previous_coord = get_coord(position)
ray.cast_to = inputs[dir] * tile_size
ray.force_raycast_update()
if !ray.is_colliding():
......@@ -42,15 +48,31 @@ func move(dir: String):
else:
print("-Actor Collision: "+get_node("..").get_actor_id_dir(name, dir))
get_parent().knockback(get_node("../"+get_node("..").get_actor_id_dir(name, dir)), 10, dir)
# If the player steps on a hole, teleport them to that hole's
# destination, or generate a new map if it does not have one
var coord = get_coord(position)
if get_node("../../TileMap").map_gen.grid[coord.y][coord.x].content == get_node("../../TileMap").CELL.HOLE:
var destination = get_node("../../TileMap").map_gen.grid[coord.y][coord.x].destination
if destination == null:
get_node("../../TileMap").update_map()
level += 1
elif previous_coord != destination && previous_coord != get_coord(position):
var tele_direction = Vector2(destination.x - coord.x, coord.y - destination.y)
teleport(tele_direction.x, tele_direction.y)
action_taken = true
else:
print("-Collision")
# Break the obstacle (if breakable)
var obstacle = get_coord(position + inputs[dir] * tile_size * dashRange)
get_node("../../TileMap").break_obstacle(obstacle.x, obstacle.y)
func teleport(x: float, y: float):
print(String(x))
print(String(y))
position += Vector2.RIGHT * x * tile_size
position += Vector2.UP * y * tile_size
print(get_coord(position))
func harm(damage: float):
health -= damage
......@@ -78,8 +100,15 @@ func _draw():
var label = Label.new()
var font = label.get_font('')
label.queue_free()
draw_string(font, Vector2(OS.window_size.x/5.5, OS.window_size.y/5), 'Dashes: ' + str(dashCharges), Color(1.0, 1.0, 0.0, 1.0))
draw_string(font, Vector2(OS.window_size.x/5.5, OS.window_size.y/4), 'Health: ' + str(health),Color(1.0, 1.0, 0.0, 1.0))
draw_string(font, Vector2(OS.window_size.x/5.5, OS.window_size.y/6), 'Dashes: ' + str(dashCharges), Color(1.0, 1.0, 0.0, 1.0))
draw_string(font, Vector2(OS.window_size.x/5.5, OS.window_size.y/5), 'Health: ' + str(health),Color(1.0, 1.0, 0.0, 1.0))
draw_string(font, Vector2(OS.window_size.x/5.5, OS.window_size.y/4), 'Level: ' + str(level),Color(1.0, 1.0, 0.0, 1.0))
# Calculates and returns the coordinate of the player in the map by taking its
# current position and dividing by the tile_size
func get_coord(p):
var normalized_position = Vector2(p.x - 232, p.y - 120)
return normalized_position / tile_size
func _process(_delta):
update()
......
......@@ -22,7 +22,7 @@ cast_to = Vector2( 0, 6 )
[node name="Camera2D" type="Camera2D" parent="."]
offset = Vector2( 4, 4 )
current = true
zoom = Vector2( 0.3, 0.3 )
zoom = Vector2( 0.6, 0.6 )
drag_margin_left = 0.01
drag_margin_top = 0.01
drag_margin_right = 0.01
......
......@@ -5,7 +5,8 @@ const ROOM = preload("res://src/Room.gd")
var rooms = []
var centers = []
var minimum_tree = []
# Declare member variables here
# Stores which coord contains the player spawn
var player_spawn
# Enumerator that stores which integers represent which type of tile
# The pairings also equate to the corresponding tile group's index in the
# atlas tileset
......@@ -410,7 +411,7 @@ func spawn_holes():
randomize()
room = rooms[randi() % rooms.size()]
if room.count_elligible_hole > 0:
if room.count_elligible_hole > 0 && !room.spawn_room:
room_found = true
room.spawn_hole(room.get_random_hole_spawn(), null)
......@@ -419,6 +420,7 @@ func spawn_holes():
func spawn_player():
randomize()
var room = rooms[randi() % rooms.size()]
room.spawn_room = true
var possible_spawns = []
var possible_replacements = [CELL.EMPTY, CELL.PLANT_T, CELL.ROCK_T, CELL.BRICK]
......@@ -431,6 +433,8 @@ func spawn_player():
if room.cells[spawn_coord].content != CELL.BRICK:
room.cells[spawn_coord].content = CELL.EMPTY
room.cells[spawn_coord].player_spawn = true
return spawn_coord
# Generates a new map
func generate_map(fill_percent, width, height):
......@@ -446,6 +450,7 @@ func generate_map(fill_percent, width, height):
minimum_tree = get_minimum_spanning_tree()
spawn_holes()
player_spawn = spawn_player()
spawn_player()
spawn_holes()
......@@ -13,6 +13,8 @@ var cells = {}
var center
# Stores an updated count of the room's cells that are valid hole spawns
var count_elligible_hole = 0
# Stores whether or not this room is the one the player spawns in
var spawn_room = false
# Calulates the average of all of the coordinates of the room's cells and sets
......@@ -152,7 +154,7 @@ func get_random_hole_spawn():
if count_elligible_hole > 0:
var possible_spawns = []
for cell in cells.keys():
if cells[cell].elligible_hole:
if cells[cell].elligible_hole && !cells[cell].player_spawn:
possible_spawns.append(cell)
randomize()
......
......@@ -20,12 +20,10 @@ func custom_clear():
for cell in cells:
set_cell(cell.x, cell.y, -1)
# Prints the tile index and atlas/autotile coord of the tile with the given coords
func print_get_cell_autotile_coord(x, y):
print("Cell x%s y%s = %s, %s" % [x, y, get_cell(x, y), get_cell_autotile_coord(x, y)])
func get_value(x, y):
# If it is outside the grid, clamp the coordinates,
# effectively returning the value of the nearest element in the grid.
......@@ -33,11 +31,9 @@ func get_value(x, y):
y = clamp(y, 0, height - 1)
return map_gen.grid[y][x].content
func is_wall(x, y):
return get_value(x, y) == map_gen.WALL
func update_tile(x, y):
var index = CELL.EMPTY
var autotile := Vector2.ZERO
......@@ -220,7 +216,6 @@ func update_tile(x, y):
set_cell(x, y, index, false, false, false, autotile)
# Generate a new map and update the level map on screen accordingly
func update_map():
if !lines.empty():
......@@ -230,6 +225,11 @@ func update_map():
map_gen.generate_map(fill_percent, width, height)
var player = get_node("../ActorManager/Player")
var spawn_coord = map_gen.player_spawn
var tele_direction = Vector2(spawn_coord.x - player.get_coord(player.position).x, player.get_coord(player.position).y - spawn_coord.y)
player.teleport(tele_direction.x, tele_direction.y)
for x in width:
for y in height:
update_tile(x, y)
......@@ -246,11 +246,23 @@ func update_map():
# lines.back().add_point(Vector2(line[1].x * 16, line[1].y * 16))
# add_child(lines.back())
# Update the collidable tile at the given coord to a non-collidable version
func break_obstacle(x, y):
var index = map_gen.grid[y][x].content
var autotile = null
match index:
CELL.ROCK_T:
autotile = Vector2(1, 0)
CELL.PLANT_T:
autotile = Vector2(1, 0)
if autotile != null:
set_cell(x, y, index, false, false, false, autotile)
# Called when the node enters the scene tree for the first time.
func _ready():
update_map()
#update_map()
pass
# Generate a new map on keypress
func _unhandled_input(event):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment