Skip to content

Commit

Permalink
Fix pawn direction code, add EMPTY constant to grid
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanLovato committed Jun 12, 2018
1 parent 11af6c7 commit 954b6f4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 88 deletions.
80 changes: 2 additions & 78 deletions 2d/grid_based_movement/Game.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
[ext_resource path="res://pawns/sprites/character_grey.png" type="Texture" id=6]
[ext_resource path="res://pawns/sprites/star.png" type="Texture" id=7]

[node name="Game" type="Node" index="0"]
[node name="Game" type="Node"]

[node name="Grass" type="TileMap" parent="." index="0"]
[node name="GridLines" type="TileMap" parent="." index="0"]

modulate = Color( 1, 1, 1, 0.271059 )
z_index = -1
Expand Down Expand Up @@ -95,81 +95,5 @@ type = 2

texture = ExtResource( 7 )

[node name="Object2" type="Node2D" parent="Grid" index="3"]

editor/display_folded = true
position = Vector2( 800, 416 )
script = ExtResource( 5 )
type = 2

[node name="Sprite" type="Sprite" parent="Grid/Object2" index="0"]

texture = ExtResource( 7 )

[node name="Object3" type="Node2D" parent="Grid" index="4"]

editor/display_folded = true
position = Vector2( 800, 288 )
script = ExtResource( 5 )
type = 2

[node name="Sprite" type="Sprite" parent="Grid/Object3" index="0"]

texture = ExtResource( 7 )

[node name="Object4" type="Node2D" parent="Grid" index="5"]

editor/display_folded = true
position = Vector2( 672, 288 )
script = ExtResource( 5 )
type = 2

[node name="Sprite" type="Sprite" parent="Grid/Object4" index="0"]

texture = ExtResource( 7 )

[node name="Object5" type="Node2D" parent="Grid" index="6"]

position = Vector2( 672, 544 )
script = ExtResource( 5 )
type = 2

[node name="Sprite" type="Sprite" parent="Grid/Object5" index="0"]

texture = ExtResource( 7 )

[node name="Object6" type="Node2D" parent="Grid" index="7"]

editor/display_folded = true
position = Vector2( 352, 480 )
script = ExtResource( 5 )
type = 2

[node name="Sprite" type="Sprite" parent="Grid/Object6" index="0"]

texture = ExtResource( 7 )

[node name="Object7" type="Node2D" parent="Grid" index="8"]

editor/display_folded = true
position = Vector2( 352, 352 )
script = ExtResource( 5 )
type = 2

[node name="Sprite" type="Sprite" parent="Grid/Object7" index="0"]

texture = ExtResource( 7 )

[node name="Object8" type="Node2D" parent="Grid" index="9"]

editor/display_folded = true
position = Vector2( 480, 544 )
script = ExtResource( 5 )
type = 2

[node name="Sprite" type="Sprite" parent="Grid/Object8" index="0"]

texture = ExtResource( 7 )


[editable path="Grid/Actor"]
6 changes: 3 additions & 3 deletions 2d/grid_based_movement/grid/grid.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extends TileMap

enum CELL_TYPES { ACTOR, OBSTACLE, OBJECT }
enum CELL_TYPES { EMPTY = -1, ACTOR, OBSTACLE, OBJECT}

func _ready():
for child in get_children():
Expand All @@ -19,7 +19,7 @@ func request_move(pawn, direction):

var cell_target_type = get_cellv(cell_target)
match cell_target_type:
-1:
EMPTY:
return update_pawn_position(pawn, cell_start, cell_target)
OBJECT:
var object_pawn = get_cell_pawn(cell_target)
Expand All @@ -32,5 +32,5 @@ func request_move(pawn, direction):

func update_pawn_position(pawn, cell_start, cell_target):
set_cellv(cell_target, pawn.type)
set_cellv(cell_start, -1)
set_cellv(cell_start, EMPTY)
return map_to_world(cell_target) + cell_size / 2
2 changes: 1 addition & 1 deletion 2d/grid_based_movement/pawns/Actor.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[ext_resource path="res://pawns/actor.gd" type="Script" id=1]
[ext_resource path="res://pawns/sprites/character.png" type="Texture" id=2]


[sub_resource type="Animation" id=1]

resource_name = "bump"
Expand Down Expand Up @@ -103,6 +102,7 @@ _sections_unfolded = [ "Transform" ]

[node name="Sprite" type="Sprite" parent="Pivot" index="0"]

position = Vector2( 1.43051e-06, -1.90735e-06 )
texture = ExtResource( 2 )
centered = false
offset = Vector2( -32, -32 )
Expand Down
16 changes: 10 additions & 6 deletions 2d/grid_based_movement/pawns/actor.gd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extends 'pawn.gd'
extends "pawn.gd"

onready var Grid = get_parent()

Expand All @@ -15,7 +15,6 @@ func _process(delta):
var target_position = Grid.request_move(self, input_direction)
if target_position:
move_to(target_position)
$Tween.start()
else:
bump()

Expand All @@ -34,11 +33,16 @@ func update_look_direction(direction):
func move_to(target_position):
set_process(false)
$AnimationPlayer.play("walk")
var move_direction = (position - target_position).normalized()
$Tween.interpolate_property($Pivot, "position", move_direction * 32, Vector2(), $AnimationPlayer.current_animation_length, Tween.TRANS_LINEAR, Tween.EASE_IN)
$Pivot/Sprite.position = position - target_position

# Move the node to the target cell instantly,
# and animate the sprite moving from the start to the target cell
var move_direction = (target_position - position).normalized()
$Tween.interpolate_property($Pivot, "position", - move_direction * 32, Vector2(), $AnimationPlayer.current_animation_length, Tween.TRANS_LINEAR, Tween.EASE_IN)
position = target_position


$Tween.start()

# Stop the function execution until the animation finished
yield($AnimationPlayer, "animation_finished")

set_process(true)
Expand Down

0 comments on commit 954b6f4

Please sign in to comment.