generated from Nekojimi/GodotTemplate
parent
e325ab0b21
commit
30287c6fc3
@ -0,0 +1,16 @@ |
|||||||
|
extends Node2D |
||||||
|
|
||||||
|
const OFFSET: Vector2 = Vector2(16,16) |
||||||
|
|
||||||
|
func _draw() -> void: |
||||||
|
for cellGraphData in get_parent().cellGraph: |
||||||
|
for link in cellGraphData.links: |
||||||
|
var from: Vector2 = get_parent().map_pos(cellGraphData.cell) + OFFSET |
||||||
|
var to: Vector2 = get_parent().map_pos(link) + OFFSET |
||||||
|
from = from.move_toward(to,10) |
||||||
|
draw_line( |
||||||
|
from, |
||||||
|
to, |
||||||
|
Color.blueviolet, |
||||||
|
2.0, |
||||||
|
true) |
@ -0,0 +1,11 @@ |
|||||||
|
[gd_scene load_steps=3 format=2] |
||||||
|
|
||||||
|
[ext_resource path="res://tiles.png" type="Texture" id=1] |
||||||
|
|
||||||
|
[sub_resource type="AtlasTexture" id=1] |
||||||
|
atlas = ExtResource( 1 ) |
||||||
|
region = Rect2( 32, 32, 32, 32 ) |
||||||
|
|
||||||
|
[node name="Pickup" type="Sprite"] |
||||||
|
texture = SubResource( 1 ) |
||||||
|
offset = Vector2( 16, 16 ) |
@ -0,0 +1,51 @@ |
|||||||
|
extends Node2D |
||||||
|
|
||||||
|
|
||||||
|
# Declare member variables here. Examples: |
||||||
|
# var a: int = 2 |
||||||
|
# var b: String = "text" |
||||||
|
|
||||||
|
var gridPos: Vector2 = Vector2(0,0) |
||||||
|
var lastPos: Vector2 = Vector2(0,0) |
||||||
|
var anim : float = 0.0 |
||||||
|
|
||||||
|
const ANIM_SPEED : float = 0.02 |
||||||
|
|
||||||
|
# Called when the node enters the scene tree for the first time. |
||||||
|
func _ready() -> void: |
||||||
|
gridPos = get_parent().world_to_map(position) |
||||||
|
|
||||||
|
|
||||||
|
# Called every frame. 'delta' is the elapsed time since the previous frame. |
||||||
|
func _process(delta: float) -> void: |
||||||
|
var duration: float = (lastPos - gridPos).length() * ANIM_SPEED |
||||||
|
if (duration <= 0): |
||||||
|
anim = 1.0 |
||||||
|
if (anim < 1.0): |
||||||
|
anim = min(anim + delta / duration, 1.0) |
||||||
|
|
||||||
|
position = get_parent().map_pos(lerp(lastPos,gridPos,anim)) |
||||||
|
|
||||||
|
func _input(event: InputEvent) -> void: |
||||||
|
if event.is_action_pressed("game_up"): |
||||||
|
move(Vector2(0,-1)) |
||||||
|
elif event.is_action_pressed("game_up_left"): |
||||||
|
move(Vector2(-1,-1)) |
||||||
|
elif event.is_action_pressed("game_left"): |
||||||
|
move(Vector2(-1,0)) |
||||||
|
elif event.is_action_pressed("game_down_left"): |
||||||
|
move(Vector2(-1,1)) |
||||||
|
elif event.is_action_pressed("game_down"): |
||||||
|
move(Vector2(0,1)) |
||||||
|
elif event.is_action_pressed("game_down_right"): |
||||||
|
move(Vector2(1,1)) |
||||||
|
elif event.is_action_pressed("game_right"): |
||||||
|
move(Vector2(1,0)) |
||||||
|
elif event.is_action_pressed("game_up_right"): |
||||||
|
move(Vector2(1,-1)) |
||||||
|
|
||||||
|
func move(dir: Vector2): |
||||||
|
lastPos = gridPos |
||||||
|
gridPos = get_parent().find_move(gridPos,dir) |
||||||
|
anim = 0 |
||||||
|
|
@ -0,0 +1,5 @@ |
|||||||
|
[gd_resource type="NativeScript" format=2] |
||||||
|
|
||||||
|
[resource] |
||||||
|
resource_name = "Player" |
||||||
|
class_name = "Player" |
@ -0,0 +1,13 @@ |
|||||||
|
[gd_scene load_steps=4 format=2] |
||||||
|
|
||||||
|
[ext_resource path="res://Player.gd" type="Script" id=1] |
||||||
|
[ext_resource path="res://tiles.png" type="Texture" id=2] |
||||||
|
|
||||||
|
[sub_resource type="AtlasTexture" id=1] |
||||||
|
atlas = ExtResource( 2 ) |
||||||
|
region = Rect2( 0, 0, 32, 32 ) |
||||||
|
|
||||||
|
[node name="Player" type="Sprite"] |
||||||
|
texture = SubResource( 1 ) |
||||||
|
offset = Vector2( 16, 16 ) |
||||||
|
script = ExtResource( 1 ) |
@ -0,0 +1,74 @@ |
|||||||
|
extends TileMap |
||||||
|
|
||||||
|
enum TileType {WALL = 2, BLANK = 3, HOLE = 4, STOP = 5} |
||||||
|
|
||||||
|
const tileTable: Array = [TileType.BLANK,TileType.BLANK,TileType.WALL,TileType.HOLE,TileType.STOP] |
||||||
|
var cellGraph: Array |
||||||
|
var nextRow: int = -1 |
||||||
|
|
||||||
|
func _ready() -> void: |
||||||
|
generate(get_used_cells()) |
||||||
|
|
||||||
|
func generate(cells: Array): |
||||||
|
for cell in cells: |
||||||
|
var tile: int = tileTable[rand_range(0,tileTable.size())] |
||||||
|
set_cellv(cell,tile) |
||||||
|
|
||||||
|
for cell in cells: |
||||||
|
var cellGraphData: CellGraphData = CellGraphData.new() |
||||||
|
cellGraphData.cell = cell |
||||||
|
|
||||||
|
for x in range(-1,2): |
||||||
|
for y in range(-1,2): |
||||||
|
if (x == 0 && y == 0): |
||||||
|
continue |
||||||
|
var tile: int = get_cellv(cell) |
||||||
|
if (tile == TileType.WALL || tile == TileType.HOLE): |
||||||
|
continue |
||||||
|
var move: Vector2 = find_move(cell, Vector2(x,y)) |
||||||
|
if (move == cell): |
||||||
|
continue |
||||||
|
var moveTile: int = get_cellv(move) |
||||||
|
if (moveTile == TileType.WALL || moveTile == TileType.HOLE): |
||||||
|
continue |
||||||
|
cellGraphData.links.append(move) |
||||||
|
|
||||||
|
cellGraph.append(cellGraphData) |
||||||
|
|
||||||
|
func map_pos(grid: Vector2) -> Vector2: |
||||||
|
return cell_size * grid |
||||||
|
|
||||||
|
func find_move(from: Vector2, dir: Vector2) -> Vector2: |
||||||
|
var curPos: Vector2 = from |
||||||
|
var cont: bool = true |
||||||
|
|
||||||
|
while (cont): |
||||||
|
var nextPos: Vector2 = curPos + dir |
||||||
|
|
||||||
|
var nextCell: int = get_cellv(nextPos) |
||||||
|
if (nextCell == TileType.WALL): # wall |
||||||
|
return curPos |
||||||
|
elif (nextCell == TileType.STOP || nextCell == TileType.HOLE): # stop or hole |
||||||
|
return nextPos |
||||||
|
elif (nextCell == TileMap.INVALID_CELL): |
||||||
|
return nextPos |
||||||
|
else: |
||||||
|
curPos = nextPos |
||||||
|
|
||||||
|
return Vector2(-1,-1) |
||||||
|
|
||||||
|
func _process(delta: float) -> void: |
||||||
|
position.y += delta * 3 |
||||||
|
|
||||||
|
if (position.y > (cell_size.y * abs(nextRow))): |
||||||
|
print("Generating row " + String(nextRow)) |
||||||
|
nextRow += 1 |
||||||
|
var toGenerate: Array = [] |
||||||
|
for x in range(get_used_rect().size.x): |
||||||
|
toGenerate.append(Vector2(x,-nextRow)) |
||||||
|
|
||||||
|
generate(toGenerate) |
||||||
|
|
||||||
|
class CellGraphData: |
||||||
|
var cell: Vector2 |
||||||
|
var links: Array |
File diff suppressed because one or more lines are too long
@ -1,5 +1,7 @@ |
|||||||
[gd_resource type="Environment" load_steps=2 format=2] |
[gd_resource type="Environment" load_steps=2 format=2] |
||||||
|
|
||||||
[sub_resource type="ProceduralSky" id=1] |
[sub_resource type="ProceduralSky" id=1] |
||||||
|
|
||||||
[resource] |
[resource] |
||||||
background_mode = 2 |
background_mode = 2 |
||||||
background_sky = SubResource( 1 ) |
background_sky = SubResource( 1 ) |
||||||
|
@ -0,0 +1,34 @@ |
|||||||
|
[remap] |
||||||
|
|
||||||
|
importer="texture" |
||||||
|
type="StreamTexture" |
||||||
|
path="res://.import/tiles.png-20e12ed313f9b52ca4483ea23302e684.stex" |
||||||
|
metadata={ |
||||||
|
"vram_texture": false |
||||||
|
} |
||||||
|
|
||||||
|
[deps] |
||||||
|
|
||||||
|
source_file="res://tiles.png" |
||||||
|
dest_files=[ "res://.import/tiles.png-20e12ed313f9b52ca4483ea23302e684.stex" ] |
||||||
|
|
||||||
|
[params] |
||||||
|
|
||||||
|
compress/mode=0 |
||||||
|
compress/lossy_quality=0.7 |
||||||
|
compress/hdr_mode=0 |
||||||
|
compress/bptc_ldr=0 |
||||||
|
compress/normal_map=0 |
||||||
|
flags/repeat=0 |
||||||
|
flags/filter=false |
||||||
|
flags/mipmaps=false |
||||||
|
flags/anisotropic=false |
||||||
|
flags/srgb=2 |
||||||
|
process/fix_alpha_border=true |
||||||
|
process/premult_alpha=false |
||||||
|
process/HDR_as_SRGB=false |
||||||
|
process/invert_color=false |
||||||
|
stream=false |
||||||
|
size_limit=0 |
||||||
|
detect_3d=true |
||||||
|
svg/scale=1.0 |
Loading…
Reference in new issue