Skip to content

Commit

Permalink
Add ice and snow to cliffs for winter.
Browse files Browse the repository at this point in the history
- Ice on cliffs is not climbable and will cause player to fall if
  touched.
  • Loading branch information
knightofiam committed Apr 21, 2021
1 parent e0395ad commit e41c9d8
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 41 deletions.
18 changes: 18 additions & 0 deletions Cliffs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class Cliffs : Area2D
private bool _isPlayerIntersectingCliffs;
private AudioStreamPlayer _audio;
private AudioStreamPlayer _music;
private TileMap _iceTileMap;
private Vector2 _topLeft;
private Vector2 _bottomRight;
private Vector2 _topRight;
private Vector2 _bottomLeft;

public override void _Ready()
{
Expand All @@ -25,6 +30,7 @@ public override void _Ready()
LoopAudio (_music.Stream);
_cliffsCollider = GetNode <CollisionShape2D> ("CollisionShape2D");
_cliffsPosition = _cliffsCollider.GlobalPosition;
_iceTileMap = GetNode <TileMap> ("Ice");

// TODO Remove.
for (var i = 1; i < 11; ++i)
Expand Down Expand Up @@ -78,6 +84,18 @@ public override void _Process (float delta)
_cliffsRect.Size = _cliffsExtents * 2;

GetNode <Player> ("../Player").IsInCliffs = _isPlayerIntersectingCliffs && _cliffsRect.Encloses (_playerRect);

_topLeft = _playerArea.GlobalPosition - _playerExtents;
_bottomRight = _playerArea.GlobalPosition + _playerExtents;
_topRight.x = _playerArea.GlobalPosition.x + _playerExtents.x;
_topRight.y = _playerArea.GlobalPosition.y - _playerExtents.y;
_bottomLeft.x = _playerArea.GlobalPosition.x - _playerExtents.x;
_bottomLeft.y = _playerArea.GlobalPosition.y + _playerExtents.y;

GetNode <Player> ("../Player").IsTouchingCliffIce = IsIntersectingAnyTile (_topLeft, _iceTileMap) ||
IsIntersectingAnyTile (_bottomRight, _iceTileMap) ||
IsIntersectingAnyTile (_topRight, _iceTileMap) ||
IsIntersectingAnyTile (_bottomLeft, _iceTileMap);
}

private void Sounds()
Expand Down
10 changes: 8 additions & 2 deletions Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public class Player : KinematicBody2D
// ReSharper disable once UnassignedField.Global
public bool IsInCliffs;

// Field must be publicly accessible from Cliffs.cs
// ReSharper disable once MemberCanBePrivate.Global
// ReSharper disable once UnassignedField.Global
public bool IsTouchingCliffIce;

public enum State
{
Idle,
Expand Down Expand Up @@ -355,6 +360,7 @@ private string DumpState() =>
"\nClimbing prep: " + _stateMachine.Is (State.ClimbingPrep) +
"\nClimbing prep timer: " + _climbingPrepTimer.TimeLeft +
"\nClimbing up: " + _stateMachine.Is (State.ClimbingUp) +
"\nTouching cliff ice: " + IsTouchingCliffIce +
"\nScraping cliff: " + _stateMachine.Is (State.Scraping) +
"\nCliff hanging: " + _stateMachine.Is (State.CliffHanging) +
"\nClimbing Horizontally: " + _stateMachine.Is (State.Traversing) +
Expand Down Expand Up @@ -441,14 +447,14 @@ private void InitializeStateMachine()
_stateMachine.AddTrigger (State.Jumping, State.FreeFalling, () => IsMovingDown() && !IsOnFloor());
_stateMachine.AddTrigger (State.ClimbingPrep, State.Idle, WasUpArrowReleased);
_stateMachine.AddTrigger (State.ClimbingPrep, State.ClimbingUp, () => IsUpArrowPressed() && _climbingPrepTimer.TimeLeft == 0);
_stateMachine.AddTrigger (State.ClimbingUp, State.FreeFalling, () => WasUpArrowReleased() || !IsInCliffs);
_stateMachine.AddTrigger (State.ClimbingUp, State.FreeFalling, () => WasUpArrowReleased() || !IsInCliffs || IsTouchingCliffIce);
_stateMachine.AddTrigger (State.FreeFalling, State.Idle, () => !IsOneActiveOf (Input.Horizontal) && IsOnFloor() && !IsMovingHorizontally());
_stateMachine.AddTrigger (State.FreeFalling, State.Running, () => IsOneActiveOf (Input.Horizontal) && IsOnFloor());
_stateMachine.AddTrigger (State.FreeFalling, State.Scraping, () => IsItemKeyPressed() && IsInCliffs && _velocity.y >= CliffScrapingActivationVelocity);
_stateMachine.AddTrigger (State.CliffHanging, State.ClimbingUp, IsUpArrowPressed);
_stateMachine.AddTrigger (State.CliffHanging, State.FreeFalling, WasDownArrowPressedOnce);
_stateMachine.AddTrigger (State.CliffHanging, State.Traversing, () => IsOneActiveOf (Input.Horizontal));
_stateMachine.AddTrigger (State.Traversing, State.FreeFalling, () => WasDownArrowPressedOnce() || !IsInCliffs);
_stateMachine.AddTrigger (State.Traversing, State.FreeFalling, () => WasDownArrowPressedOnce() || !IsInCliffs || IsTouchingCliffIce);
_stateMachine.AddTrigger (State.Traversing, State.CliffHanging, () => !IsOneActiveOf (Input.Horizontal) && !IsMovingHorizontally());
_stateMachine.AddTrigger (State.Scraping, State.FreeFalling, WasItemKeyReleased);
_stateMachine.AddTrigger (State.Scraping, State.CliffHanging, () => !IsOnFloor() && !IsMoving() && IsInCliffs);
Expand Down
5 changes: 5 additions & 0 deletions Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,9 @@ public static void LoopAudio (AudioStream stream, float loopBeginSeconds, float

public static string ToString <T> (IEnumerable <T> e, string sep = ", ", Func <T, string> f = null) =>
e.Select (f ?? (s => s.ToString())).DefaultIfEmpty (string.Empty).Aggregate ((a, b) => a + sep + b);

public static bool IsIntersectingAnyTile (Vector2 globalPosition, TileMap tileMap)
{
return tileMap.GetCellv (tileMap.WorldToMap (tileMap.ToLocal (globalPosition))) != -1;
}
}
Binary file modified cliffs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
167 changes: 167 additions & 0 deletions cliffs.tres
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,170 @@
24/shape_one_way_margin = 0.0
24/shapes = [ ]
24/z_index = 1
25/name = "cliffs.png 25"
25/texture = ExtResource( 1 )
25/tex_offset = Vector2( 0, 0 )
25/modulate = Color( 1, 1, 1, 1 )
25/region = Rect2( 72, 25, 0, 0 )
25/tile_mode = 1
25/autotile/bitmask_mode = 0
25/autotile/bitmask_flags = [ ]
25/autotile/icon_coordinate = Vector2( 0, 0 )
25/autotile/tile_size = Vector2( 32, 32 )
25/autotile/spacing = 0
25/autotile/occluder_map = [ ]
25/autotile/navpoly_map = [ ]
25/autotile/priority_map = [ ]
25/autotile/z_index_map = [ ]
25/occluder_offset = Vector2( 0, 0 )
25/navigation_offset = Vector2( 0, 0 )
25/shape_offset = Vector2( 0, 0 )
25/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
25/shape_one_way = false
25/shape_one_way_margin = 0.0
25/shapes = [ ]
25/z_index = 0
26/name = "cliffs.png 26"
26/texture = ExtResource( 1 )
26/tex_offset = Vector2( 0, 0 )
26/modulate = Color( 1, 1, 1, 1 )
26/region = Rect2( 73, 24, 0, 0 )
26/tile_mode = 1
26/autotile/bitmask_mode = 0
26/autotile/bitmask_flags = [ ]
26/autotile/icon_coordinate = Vector2( 0, 0 )
26/autotile/tile_size = Vector2( 32, 32 )
26/autotile/spacing = 0
26/autotile/occluder_map = [ ]
26/autotile/navpoly_map = [ ]
26/autotile/priority_map = [ ]
26/autotile/z_index_map = [ ]
26/occluder_offset = Vector2( 0, 0 )
26/navigation_offset = Vector2( 0, 0 )
26/shape_offset = Vector2( 0, 0 )
26/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
26/shape_one_way = false
26/shape_one_way_margin = 0.0
26/shapes = [ ]
26/z_index = 0
27/name = "cliff-snow-top-left"
27/texture = ExtResource( 1 )
27/tex_offset = Vector2( 0, 0 )
27/modulate = Color( 1, 1, 1, 1 )
27/region = Rect2( 48, 0, 16, 16 )
27/tile_mode = 0
27/occluder_offset = Vector2( 0, 0 )
27/navigation_offset = Vector2( 0, 0 )
27/shape_offset = Vector2( 0, 0 )
27/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
27/shape_one_way = false
27/shape_one_way_margin = 0.0
27/shapes = [ ]
27/z_index = 0
28/name = "cliff-snow-top-middle"
28/texture = ExtResource( 1 )
28/tex_offset = Vector2( 0, 0 )
28/modulate = Color( 1, 1, 1, 1 )
28/region = Rect2( 64, 0, 16, 16 )
28/tile_mode = 0
28/occluder_offset = Vector2( 0, 0 )
28/navigation_offset = Vector2( 0, 0 )
28/shape_offset = Vector2( 0, 0 )
28/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
28/shape_one_way = false
28/shape_one_way_margin = 0.0
28/shapes = [ ]
28/z_index = 0
29/name = "cliff-snow-top-right"
29/texture = ExtResource( 1 )
29/tex_offset = Vector2( 0, 0 )
29/modulate = Color( 1, 1, 1, 1 )
29/region = Rect2( 80, 0, 16, 16 )
29/tile_mode = 0
29/occluder_offset = Vector2( 0, 0 )
29/navigation_offset = Vector2( 0, 0 )
29/shape_offset = Vector2( 0, 0 )
29/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
29/shape_one_way = false
29/shape_one_way_margin = 0.0
29/shapes = [ ]
29/z_index = 0
30/name = "cliff-grass-top-right"
30/texture = ExtResource( 1 )
30/tex_offset = Vector2( 0, 0 )
30/modulate = Color( 1, 1, 1, 1 )
30/region = Rect2( 32, 0, 16, 16 )
30/tile_mode = 0
30/occluder_offset = Vector2( 0, 0 )
30/navigation_offset = Vector2( 0, 0 )
30/shape_offset = Vector2( 0, 0 )
30/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
30/shape_one_way = false
30/shape_one_way_margin = 0.0
30/shapes = [ ]
30/z_index = 0
31/name = "cliff-ice-auto-tile"
31/texture = ExtResource( 1 )
31/tex_offset = Vector2( 0, 0 )
31/modulate = Color( 1, 1, 1, 1 )
31/region = Rect2( 48, 16, 48, 48 )
31/tile_mode = 1
31/autotile/bitmask_mode = 0
31/autotile/bitmask_flags = [ Vector2( 0, 0 ), 256, Vector2( 0, 1 ), 260, Vector2( 0, 2 ), 4, Vector2( 1, 0 ), 320, Vector2( 1, 1 ), 325, Vector2( 1, 2 ), 5, Vector2( 2, 0 ), 64, Vector2( 2, 1 ), 65, Vector2( 2, 2 ), 1 ]
31/autotile/icon_coordinate = Vector2( 0, 0 )
31/autotile/tile_size = Vector2( 16, 16 )
31/autotile/spacing = 0
31/autotile/occluder_map = [ ]
31/autotile/navpoly_map = [ ]
31/autotile/priority_map = [ ]
31/autotile/z_index_map = [ ]
31/occluder_offset = Vector2( 0, 0 )
31/navigation_offset = Vector2( 0, 0 )
31/shape_offset = Vector2( 0, 0 )
31/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
31/shape_one_way = false
31/shape_one_way_margin = 0.0
31/shapes = [ ]
31/z_index = 0
32/name = "cliff-ice-left"
32/texture = ExtResource( 1 )
32/tex_offset = Vector2( 0, 0 )
32/modulate = Color( 1, 1, 1, 1 )
32/region = Rect2( 48, 16, 16, 16 )
32/tile_mode = 0
32/occluder_offset = Vector2( 0, 0 )
32/navigation_offset = Vector2( 0, 0 )
32/shape_offset = Vector2( 0, 0 )
32/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
32/shape_one_way = false
32/shape_one_way_margin = 0.0
32/shapes = [ ]
32/z_index = 0
33/name = "cliff-ice-middle"
33/texture = ExtResource( 1 )
33/tex_offset = Vector2( 0, 0 )
33/modulate = Color( 1, 1, 1, 1 )
33/region = Rect2( 64, 16, 16, 16 )
33/tile_mode = 0
33/occluder_offset = Vector2( 0, 0 )
33/navigation_offset = Vector2( 0, 0 )
33/shape_offset = Vector2( 0, 0 )
33/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
33/shape_one_way = false
33/shape_one_way_margin = 0.0
33/shapes = [ ]
33/z_index = 0
34/name = "cliff-ice-right"
34/texture = ExtResource( 1 )
34/tex_offset = Vector2( 0, 0 )
34/modulate = Color( 1, 1, 1, 1 )
34/region = Rect2( 80, 16, 16, 16 )
34/tile_mode = 0
34/occluder_offset = Vector2( 0, 0 )
34/navigation_offset = Vector2( 0, 0 )
34/shape_offset = Vector2( 0, 0 )
34/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
34/shape_one_way = false
34/shape_one_way_margin = 0.0
34/shapes = [ ]
34/z_index = 0
Loading

0 comments on commit e41c9d8

Please sign in to comment.