Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to grid based mining #29

Merged
merged 8 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions Actors/Player/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,18 @@ signal pickaxe_used # Emitted when the player uses their pickaxe
func _ready():
$ScentTimer.connect("timeout", self, "leave_scent")

func get_camera():
return get_node("Camera")
func get_camera() -> Camera2D:
return get_node("Camera") as Camera2D

func get_feet_position() -> Vector2:
return get_node("CollisionShape").global_position

# Determines the player's facing direction based on the mouse position
func determine_facing():
var facing_before = facing

var mouse_pos = get_local_mouse_position()
if -abs(mouse_pos.y) > mouse_pos.x: facing = Vector2.LEFT
elif abs(mouse_pos.y) < mouse_pos.x: facing = Vector2.RIGHT
elif -abs(mouse_pos.x) > mouse_pos.y: facing = Vector2.UP
else: facing = Vector2.DOWN

# Mouse direction from the player is the same as its local position
var mouse_direction = get_local_mouse_position()
facing = Utils.approximate_direction_4_ways(mouse_direction)
# Render body sprite above item sprite if facing upwards
if facing != facing_before:
if facing == Vector2.UP: move_child(body_sprite, 1)
Expand Down
10 changes: 4 additions & 6 deletions Actors/Player/Player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ collision_mask = 14
script = ExtResource( 2 )

[node name="BodySprite" type="Sprite" parent="."]
position = Vector2( 0.5, -12 )
position = Vector2( 0.5, -6.5 )
texture = ExtResource( 1 )
hframes = 6
vframes = 8

[node name="ItemSprite" type="Sprite" parent="."]
position = Vector2( -7.5, 0.5 )
position = Vector2( -7.5, 6 )
texture = ExtResource( 11 )
centered = false
offset = Vector2( -18, -22 )
Expand All @@ -70,14 +70,14 @@ anims/Walk_Up = ExtResource( 5 )

[node name="CollisionShape" type="CollisionShape2D" parent="."]
modulate = Color( 0, 0.6, 0.701961, 0.839216 )
position = Vector2( 0, 5.5 )
rotation = 1.5708
shape = SubResource( 1 )

[node name="SoftCollision" parent="." instance=ExtResource( 23 )]
position = Vector2( 0, -4 )
position = Vector2( 0, 1.5 )

[node name="Hitbox" type="Area2D" parent="."]
position = Vector2( 0, -5.5 )
collision_layer = 16
collision_mask = 256

Expand All @@ -86,7 +86,6 @@ modulate = Color( 0.0784314, 0.941176, 0.27451, 0.839216 )
shape = SubResource( 2 )

[node name="Hurtbox" type="Area2D" parent="."]
position = Vector2( 0, -5.5 )
collision_layer = 0
collision_mask = 96

Expand Down Expand Up @@ -136,7 +135,6 @@ script = ExtResource( 18 )
script = ExtResource( 19 )

[node name="Camera" type="Camera2D" parent="."]
position = Vector2( 0, -5.5 )
current = true
zoom = Vector2( 0.2, 0.2 )
limit_smoothed = true
Expand Down
2 changes: 1 addition & 1 deletion Actors/State Machines/Player States/PlayerWalkState.gd
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ func take_damage(attacker, damage: int):
func leave_scent():
var scent = PLAYER_SCENT.instance()
scent.player = player
scent.position = player.position
scent.position = player.get_feet_position()
player.get_parent().add_child(scent)
player.scent_trail.push_front(scent)
2 changes: 1 addition & 1 deletion Assets/Actors/Player/Animations/Idle_Down.tres
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tracks/1/keys = {
"times": PoolRealArray( 0, 0.5 ),
"transitions": PoolRealArray( 1, 1 ),
"update": 1,
"values": [ Vector2( -7.5, 0.5 ), Vector2( -7.5, 1.5 ) ]
"values": [ Vector2( -7.5, 6 ), Vector2( -7.5, 7 ) ]
}
tracks/2/type = "value"
tracks/2/path = NodePath("ItemSprite:rotation_degrees")
Expand Down
2 changes: 1 addition & 1 deletion Assets/Actors/Player/Animations/Idle_Left.tres
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tracks/1/keys = {
"times": PoolRealArray( 0, 0.5 ),
"transitions": PoolRealArray( 1, 1 ),
"update": 1,
"values": [ Vector2( -7, 0 ), Vector2( -7, 1 ) ]
"values": [ Vector2( -7, 5.5 ), Vector2( -7, 6.5 ) ]
}
tracks/2/type = "value"
tracks/2/path = NodePath("ItemSprite:rotation_degrees")
Expand Down
2 changes: 1 addition & 1 deletion Assets/Actors/Player/Animations/Idle_Right.tres
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tracks/1/keys = {
"times": PoolRealArray( 0, 0.5 ),
"transitions": PoolRealArray( 1, 1 ),
"update": 1,
"values": [ Vector2( -6.8, 0 ), Vector2( -6.8, 1 ) ]
"values": [ Vector2( -6.8, 5.5 ), Vector2( -6.8, 6.5 ) ]
}
tracks/2/type = "value"
tracks/2/path = NodePath("ItemSprite:rotation_degrees")
Expand Down
2 changes: 1 addition & 1 deletion Assets/Actors/Player/Animations/Idle_Up.tres
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tracks/1/keys = {
"times": PoolRealArray( 0, 0.5 ),
"transitions": PoolRealArray( 1, 1 ),
"update": 1,
"values": [ Vector2( 7.5, 0.5 ), Vector2( 7.5, 1.5 ) ]
"values": [ Vector2( 7.5, 6 ), Vector2( 7.5, 7 ) ]
}
tracks/2/type = "value"
tracks/2/path = NodePath("ItemSprite:rotation_degrees")
Expand Down
2 changes: 1 addition & 1 deletion Assets/Actors/Player/Animations/Melee_Down.tres
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tracks/1/keys = {
"times": PoolRealArray( 0, 0.07, 0.14, 0.21 ),
"transitions": PoolRealArray( 1, 1, 1, 1 ),
"update": 0,
"values": [ Vector2( -7.5, 0.5 ), Vector2( -3.5, 0.5 ), Vector2( 0, 0.5 ), Vector2( 3.5, 0.5 ) ]
"values": [ Vector2( -7.5, 6 ), Vector2( -3.5, 6 ), Vector2( 0, 6 ), Vector2( 3.5, 6 ) ]
}
tracks/2/type = "value"
tracks/2/path = NodePath("ItemSprite:offset")
Expand Down
2 changes: 1 addition & 1 deletion Assets/Actors/Player/Animations/Melee_Left.tres
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tracks/0/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
"update": 1,
"values": [ Vector2( -7, 0 ) ]
"values": [ Vector2( -7, 5.5 ) ]
}
tracks/1/type = "value"
tracks/1/path = NodePath("ItemSprite:rotation_degrees")
Expand Down
2 changes: 1 addition & 1 deletion Assets/Actors/Player/Animations/Melee_Right.tres
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tracks/0/keys = {
"times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ),
"update": 1,
"values": [ Vector2( -6.8, 0 ) ]
"values": [ Vector2( -6.8, 5.5 ) ]
}
tracks/1/type = "value"
tracks/1/path = NodePath("ItemSprite:rotation_degrees")
Expand Down
2 changes: 1 addition & 1 deletion Assets/Actors/Player/Animations/Melee_Up.tres
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tracks/0/keys = {
"times": PoolRealArray( 0, 0.07, 0.14, 0.21 ),
"transitions": PoolRealArray( 1, 1, 1, 1 ),
"update": 0,
"values": [ Vector2( 7.5, 0.5 ), Vector2( 3.5, -3.5 ), Vector2( 0, -3.5 ), Vector2( -3.5, -3.5 ) ]
"values": [ Vector2( 7.5, 6 ), Vector2( 3.5, 2 ), Vector2( 0, 2 ), Vector2( -3.5, 2 ) ]
}
tracks/1/type = "value"
tracks/1/path = NodePath("ItemSprite:rotation_degrees")
Expand Down
2 changes: 1 addition & 1 deletion Assets/Actors/Player/Animations/Walk_Down.tres
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tracks/1/keys = {
"times": PoolRealArray( 0, 0.1, 0.3, 0.4 ),
"transitions": PoolRealArray( 1, 1, 1, 1 ),
"update": 1,
"values": [ Vector2( -7.5, 0.5 ), Vector2( -4, -2 ), Vector2( -7.5, 0.5 ), Vector2( -7.5, -1.5 ) ]
"values": [ Vector2( -7.5, 6 ), Vector2( -4, 3.5 ), Vector2( -7.5, 6 ), Vector2( -7.5, 4 ) ]
}
tracks/2/type = "value"
tracks/2/path = NodePath("ItemSprite:rotation_degrees")
Expand Down
8 changes: 4 additions & 4 deletions Assets/Actors/Player/Animations/Walk_Left.tres
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tracks/0/keys = {
"values": [ Vector2( 0, 7 ), Vector2( 1, 7 ), Vector2( 2, 7 ), Vector2( 3, 7 ), Vector2( 4, 7 ), Vector2( 5, 7 ) ]
}
tracks/1/type = "value"
tracks/1/path = NodePath("ItemSprite:rotation_degrees")
tracks/1/path = NodePath("ItemSprite:position")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/imported = false
Expand All @@ -25,10 +25,10 @@ tracks/1/keys = {
"times": PoolRealArray( 0, 0.1, 0.3, 0.4 ),
"transitions": PoolRealArray( 1, 1, 1, 1 ),
"update": 1,
"values": [ -25.0, -45.0, -25.0, 45.0 ]
"values": [ Vector2( -7, 5.5 ), Vector2( -7, 3.5 ), Vector2( -7, 5.5 ), Vector2( -4.5, 3.5 ) ]
}
tracks/2/type = "value"
tracks/2/path = NodePath("ItemSprite:position")
tracks/2/path = NodePath("ItemSprite:rotation_degrees")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/imported = false
Expand All @@ -37,7 +37,7 @@ tracks/2/keys = {
"times": PoolRealArray( 0, 0.1, 0.3, 0.4 ),
"transitions": PoolRealArray( 1, 1, 1, 1 ),
"update": 1,
"values": [ Vector2( -7, 0 ), Vector2( -7, -2 ), Vector2( -7, 0 ), Vector2( -4.5, -2 ) ]
"values": [ -25.0, -45.0, -25.0, 45.0 ]
}
tracks/3/type = "value"
tracks/3/path = NodePath("ItemSprite:offset")
Expand Down
2 changes: 1 addition & 1 deletion Assets/Actors/Player/Animations/Walk_Right.tres
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tracks/1/keys = {
"times": PoolRealArray( 0, 0.1, 0.3, 0.4 ),
"transitions": PoolRealArray( 1, 1, 1, 1 ),
"update": 1,
"values": [ Vector2( -6.8, 0 ), Vector2( -3.2, -2.5 ), Vector2( -6.8, 0 ), Vector2( -6.8, -2 ) ]
"values": [ Vector2( -6.8, 5.5 ), Vector2( -3.2, 3 ), Vector2( -6.8, 5.5 ), Vector2( -6.8, 3.5 ) ]
}
tracks/2/type = "value"
tracks/2/path = NodePath("ItemSprite:rotation_degrees")
Expand Down
2 changes: 1 addition & 1 deletion Assets/Actors/Player/Animations/Walk_Up.tres
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tracks/1/keys = {
"times": PoolRealArray( 0, 0.1, 0.3, 0.4 ),
"transitions": PoolRealArray( 1, 1, 1, 1 ),
"update": 1,
"values": [ Vector2( 7.5, 0.5 ), Vector2( 4, -2 ), Vector2( 7.5, 0.5 ), Vector2( 7.5, -1.5 ) ]
"values": [ Vector2( 7.5, 6 ), Vector2( 4, 3.5 ), Vector2( 7.5, 6 ), Vector2( 7.5, 4 ) ]
}
tracks/2/type = "value"
tracks/2/path = NodePath("ItemSprite:rotation_degrees")
Expand Down
47 changes: 16 additions & 31 deletions GUI/HUD/Minimap.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,24 @@ func update_minimap(walls: TileMap):
# Sets the previous tile position to GROUND tile
# and the new tile position to PLAYER tile
func set_player_tile(new_tile_pos: Vector2):
if player_tile_pos: set_cell(player_tile_pos.x, player_tile_pos.y, GROUND)
set_cell(new_tile_pos.x, new_tile_pos.y, PLAYER)
if player_tile_pos: set_cellv(player_tile_pos, GROUND)
set_cellv(new_tile_pos, PLAYER)
player_tile_pos = new_tile_pos

# Updates the player tile position displayed in the minimap
func update_player_pos(new_player_pos: Vector2, ceiling: TileMap):
var new_tile_pos = ceiling.world_to_map(new_player_pos)
if new_tile_pos == player_tile_pos: return

# If new tile is unobstructed, set player tile here
if ceiling.get_cell(new_tile_pos.x, new_tile_pos.y) == -1:
set_player_tile(new_tile_pos)
func update_player_tile_pos(player_pos: Vector2, ceiling: TileMap):
var actual_tile_pos = ceiling.world_to_map(player_pos)
# Set player tile to the actual tile pos, if unobstructed
if ceiling.get_cellv(actual_tile_pos) == -1:
set_player_tile(actual_tile_pos)
else:
# If new tile is obstructed, get the centre pos of the new tile
var offset = Vector2.ONE * ceiling.cell_size / 2
var centre_pos = ceiling.map_to_world(new_tile_pos) + offset

# Find the direction from the centre of the tile to the player
var direction = centre_pos.direction_to(new_player_pos)

# Get the next tile in the estimated direction
var next_tile_pos: Vector2
if abs(direction.x) > abs(direction.y):
if direction.x < 0:
next_tile_pos = new_tile_pos + Vector2.LEFT
else:
next_tile_pos = new_tile_pos + Vector2.RIGHT
else:
if direction.y < 0:
next_tile_pos = new_tile_pos + Vector2.UP
else:
next_tile_pos = new_tile_pos + Vector2.DOWN

# Set the player tile at the next tile pos instead
if ceiling.get_cell(next_tile_pos.x, next_tile_pos.y) == -1:
# If actual tile is obstructed, get the centre pos of the actual tile
var offset = Vector2.ONE * cell_size/2
var centre_pos = ceiling.map_to_world(actual_tile_pos) + offset
# Get the direction from the centre of the tile to the player
var direction = centre_pos.direction_to(player_pos)
# Approximate direction and get the next tile in that direction
var next_tile_pos = actual_tile_pos + Utils.approximate_direction_4_ways(direction)
# Set player tile to the next tile pos instead, if unobstructed
if ceiling.get_cellv(next_tile_pos) == -1:
set_player_tile(next_tile_pos)
41 changes: 34 additions & 7 deletions Globals/Utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,37 @@ func percent_chance(chance: float, rng: RandomNumberGenerator = null) -> bool:
func choose_from(array: Array):
return array[randi() % array.size()]

# Approximates the given direction vector
# Returns either LEFT, RIGHT, UP or DOWN
func approximate_direction(direction: Vector2):
if abs(direction.x) > abs(direction.y):
return Vector2.LEFT if direction.x < 0 else Vector2.RIGHT
else:
return Vector2.UP if direction.y < 0 else Vector2.DOWN
# Approximates the given vector in 4 directions:
# RIGHT, DOWN, LEFT and UP
func approximate_direction_4_ways(direction: Vector2) -> Vector2:
var section = get_8_way_section(direction.rotated(-PI/8))
match section:
7,0: return Vector2.RIGHT
1,2: return Vector2.DOWN
3,4: return Vector2.LEFT
5,6: return Vector2.UP
return Vector2.ZERO

# Approximates the given vector in 8 directions:
# RIGHT, RIGHT+DOWN, DOWN, LEFT+DOWN, LEFT, LEFT+UP, UP and RIGHT+UP
func approximate_direction_8_ways(direction: Vector2) -> Vector2:
var section = get_8_way_section(direction)
match section:
0: return Vector2.RIGHT
1: return Vector2(1, 1) # RIGHT+DOWN
2: return Vector2.DOWN
3: return Vector2(-1, 1) # LEFT+DOWN
4: return Vector2.LEFT
5: return Vector2(-1, -1) # LEFT+UP
6: return Vector2.UP
7: return Vector2(1, -1) # RIGHT+UP
return Vector2.ZERO

# For a circle split into 8 sections,
# this function returns an integer from 0 to 7 (inclusive),
# that represents the section of the circle the given direction lies within:
# 0 = RIGHT, 7 = RIGHT+UP
func get_8_way_section(direction: Vector2) -> int:
if direction == Vector2.ZERO: return -1
var section = stepify(direction.angle(), PI/4) / (PI/4)
return wrapi(section, 0, 8)
10 changes: 5 additions & 5 deletions World/Level/Ceiling.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ func update_ceiling_layer():
# Destroys the tile at the given tile position and handles subsequent actions,
# as a result of the tile being removed.
func destroy_tile(tile_pos: Vector2, update: bool):
var obstructed = get_cell(tile_pos.x, tile_pos.y) != -1
var solid = get_cellv(tile_pos) != -1
var out_of_bounds_x = 1 > tile_pos.x or tile_pos.x > Globals.CAVE_SIZE
var out_of_bounds_y = 1 > tile_pos.y or tile_pos.y > Globals.CAVE_SIZE
if not obstructed or out_of_bounds_x or out_of_bounds_y: return
if not solid or out_of_bounds_x or out_of_bounds_y: return

minimap.set_cell(tile_pos.x, tile_pos.y, minimap.GROUND)
walls.set_cell(tile_pos.x, tile_pos.y, -1)
minimap.set_cellv(tile_pos, minimap.GROUND)
walls.set_cellv(tile_pos, -1)
objects.destroy_gemstone_if_present(tile_pos)
set_cell(tile_pos.x, tile_pos.y, -1)
set_cellv(tile_pos, -1)
if update:
walls.update_bitmask_region(tile_pos-Vector2.ONE, tile_pos+Vector2.ONE)
update_bitmask_region(tile_pos-Vector2.ONE, tile_pos+Vector2.ONE)
Expand Down
2 changes: 1 addition & 1 deletion World/Level/Level.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func _ready():
func _process(delta):
if objects.player_exists():
var player_pos = objects.get_player().position
minimap.update_player_pos(player_pos, ceiling)
minimap.update_player_tile_pos(player_pos, ceiling)

# Generates a new level
func generate_level():
Expand Down
Loading