58 lines
1.9 KiB
GDScript
58 lines
1.9 KiB
GDScript
extends Node2D
|
|
var steps: int = 0
|
|
var layer: TileMapLayer = null
|
|
const MAX_STEPS: int = 300
|
|
var landed: bool = false
|
|
|
|
func _ready() -> void:
|
|
var img := Image.load_from_file("res://assets/tiles/tiles.png")
|
|
var tex := ImageTexture.new()
|
|
tex.image = img
|
|
var ts: TileSet = TileSet.new()
|
|
ts.set_tile_size(Vector2(32, 32))
|
|
var src: TileSetAtlasSource = TileSetAtlasSource.new()
|
|
src.texture = tex
|
|
src.texture_region_size = Vector2i(32, 32)
|
|
src.create_tile(Vector2i(0, 0))
|
|
ts.add_source(src, 0)
|
|
ts.add_physics_layer()
|
|
ts.set_physics_layer_collision_layer(0, 1)
|
|
ts.set_physics_layer_collision_mask(0, 1)
|
|
|
|
layer = TileMapLayer.new()
|
|
layer.name = "Level"
|
|
layer.tile_set = ts
|
|
layer.set_collision_enabled(true)
|
|
layer.set_cell(Vector2i(0, 0), 0, Vector2i(0, 0))
|
|
var td := layer.get_cell_tile_data(Vector2i(0, 0))
|
|
td.set_collision_polygons_count(0, 1)
|
|
td.set_collision_polygon_points(0, 0, PackedVector2Array([Vector2(0,0), Vector2(32,0), Vector2(32,32), Vector2(0,32)]))
|
|
add_child(layer)
|
|
|
|
var fox := CharacterBody2D.new()
|
|
fox.name = "Fox"
|
|
fox.set_script(load("res://scripts/player.gd"))
|
|
fox.collision_layer = 1
|
|
fox.collision_mask = 1
|
|
var fcol := CollisionShape2D.new()
|
|
var fshape := CapsuleShape2D.new()
|
|
fshape.radius = 14.0
|
|
fshape.height = 40.0
|
|
fcol.shape = fshape
|
|
fox.add_child(fcol)
|
|
fcol.owner = self
|
|
add_child(fox) # Fox as DIRECT child of root (has physics space)
|
|
fox.position = Vector2(16, -40) # above tile polygon (top edge y=0)
|
|
|
|
func _physics_process(_delta: float) -> void:
|
|
steps += 1
|
|
var fox := get_node("Fox")
|
|
fox.move_and_slide()
|
|
if fox.is_on_floor() and not landed:
|
|
landed = true
|
|
print("FOX landed on TILE: pos=", fox.position, " on_floor=", fox.is_on_floor(), " vel.y=", fox.velocity.y, " steps=", steps)
|
|
get_tree().quit()
|
|
if steps >= MAX_STEPS:
|
|
print("FOX NO landing on tile: pos=", fox.position, " on_floor=", fox.is_on_floor(), " steps=", steps)
|
|
get_tree().quit()
|