Advertisement
PokemonMaster124

Camera Tile Loop Problem

Apr 25th, 2023
1,648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.50 KB | Gaming | 0 0
  1. extends Node2D
  2.  
  3. var pos: Vector2 = Vector2.ZERO
  4. var mapSize: Vector2 = Vector2(32, 24)
  5. var tileCount: Vector2 = Vector2(11, 9)
  6. var nextPos: Vector2
  7. var map: String = "map1"
  8. var has_limit: bool = true
  9. var file
  10. var json
  11. var distance
  12.  
  13. func _physics_process(_delta):
  14.     file = FileAccess.open("res://assets/data/maps.json", FileAccess.READ)
  15.     json = JSON.parse_string(file.get_as_text())
  16.     distance = (tileCount - Vector2.ONE) / 2
  17.     mapSize = Vector2(json[map].size(), json[map][0].size())
  18.    
  19.     if Input.is_action_pressed("l"):
  20.         pos.x -= 1
  21.     if Input.is_action_pressed("r"):
  22.         pos.x += 1
  23.     if Input.is_action_pressed("u"):
  24.         pos.y -= 1
  25.     if Input.is_action_pressed("d"):
  26.         pos.y += 1
  27.    
  28.     pos.x = wrapf(pos.x, 0, mapSize.x * 20)
  29.     pos.y = wrapf(pos.y, 0, mapSize.y * 20)
  30.    
  31.     $Player.position.x = (int(pos.x) % 20) + 80
  32.     $Player.position.y = (int(pos.y) % 20) + 60
  33.     $Camera.position.x = $Player.position.x + 10
  34.     $Camera.position.y = $Player.position.y + 10
  35.    
  36.     drawMapAroundPlayer()
  37.  
  38. func drawMapAroundPlayer():
  39.     for x in range(tileCount.x):
  40.         for y in range(tileCount.y):
  41.             $Map.set_cell(0, Vector2i(x, y), 0, getCoordinates(getID(json, floor(pos.x / 20), floor(pos.y / 20), x, y), 10))
  42.  
  43. func getCoordinates(idx: int, columns: int):
  44.     var x = idx % columns
  45.     var y = float(idx - x) / columns
  46.     return Vector2i(x, y)
  47.  
  48. func getID(dict: Dictionary, x: int, y: int, ox: int, oy: int):
  49.     var result_x = wrapf(x + ox, 0, mapSize.x)
  50.     var result_y = wrapf(y + oy, 0, mapSize.y)
  51.     return dict[map][result_y][result_x]
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement