Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- var pos: Vector2 = Vector2.ZERO
- var mapSize: Vector2 = Vector2(32, 24)
- var tileCount: Vector2 = Vector2(11, 9)
- var nextPos: Vector2
- var map: String = "map1"
- var has_limit: bool = true
- var file
- var json
- var distance
- func _physics_process(_delta):
- file = FileAccess.open("res://assets/data/maps.json", FileAccess.READ)
- json = JSON.parse_string(file.get_as_text())
- distance = (tileCount - Vector2.ONE) / 2
- mapSize = Vector2(json[map].size(), json[map][0].size())
- if Input.is_action_pressed("l"):
- pos.x -= 1
- if Input.is_action_pressed("r"):
- pos.x += 1
- if Input.is_action_pressed("u"):
- pos.y -= 1
- if Input.is_action_pressed("d"):
- pos.y += 1
- pos.x = wrapf(pos.x, 0, mapSize.x * 20)
- pos.y = wrapf(pos.y, 0, mapSize.y * 20)
- $Player.position.x = (int(pos.x) % 20) + 80
- $Player.position.y = (int(pos.y) % 20) + 60
- $Camera.position.x = $Player.position.x + 10
- $Camera.position.y = $Player.position.y + 10
- drawMapAroundPlayer()
- func drawMapAroundPlayer():
- for x in range(tileCount.x):
- for y in range(tileCount.y):
- $Map.set_cell(0, Vector2i(x, y), 0, getCoordinates(getID(json, floor(pos.x / 20), floor(pos.y / 20), x, y), 10))
- func getCoordinates(idx: int, columns: int):
- var x = idx % columns
- var y = float(idx - x) / columns
- return Vector2i(x, y)
- func getID(dict: Dictionary, x: int, y: int, ox: int, oy: int):
- var result_x = wrapf(x + ox, 0, mapSize.x)
- var result_y = wrapf(y + oy, 0, mapSize.y)
- return dict[map][result_y][result_x]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement