소스 검색

Retrieve grid in Player

DricomDragon 5 년 전
부모
커밋
ddb5128f2b
2개의 변경된 파일12개의 추가작업 그리고 4개의 파일을 삭제
  1. 1 0
      Game.tscn
  2. 11 4
      Player.gd

+ 1 - 0
Game.tscn

@@ -43,3 +43,4 @@ tile_data = PoolIntArray( 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0,
 
 [node name="Player" parent="Grid" instance=ExtResource( 2 )]
 position = Vector2( 352, 736 )
+gridNode = NodePath("..")

+ 11 - 4
Player.gd

@@ -1,12 +1,17 @@
 extends Area2D
 
 # Nodes
-var grid
+export (NodePath) var gridNode
+var grid:TileMap
 var tween
 var rayFront
 var rayLeft
 var rayRight
 
+# Misc
+var cell_size
+var cell_half_size
+
 # Enum
 const DIR_UP = 0
 const DIR_RIGHT = 1
@@ -26,8 +31,10 @@ var is_moving = false
 var target_pos
 
 func _ready():
-	grid = get_parent()
-	
+	grid = get_node(gridNode)
+	cell_size = int (grid.get_cell_size().x)
+	cell_half_size = cell_size / 2
+
 	posix = int (position.x / 64)
 	posiy = int (position.y / 64)
 
@@ -68,7 +75,7 @@ func _physics_process(delta):
 	posix += dirx
 	posiy += diry
 
-	target_pos = Vector2(posix * 64 + 32, posiy * 64 + 32)
+	target_pos = Vector2(posix * cell_size + cell_half_size, posiy * cell_size + cell_half_size)
 	tween.move_char(self, target_pos)
 	is_moving = true
 	tween.start()