Add new Battle Scene for managing character movement and damage (pending)
This commit is contained in:
parent
2176c95417
commit
47f59a7a6d
82
Scenes/Battle.gd
Normal file
82
Scenes/Battle.gd
Normal file
@ -0,0 +1,82 @@
|
||||
extends Node2D
|
||||
|
||||
signal scene_finished
|
||||
signal scene_failed
|
||||
|
||||
|
||||
var character = null setget new_character
|
||||
var movement_queue = []
|
||||
var character_original_position : Vector2 = Vector2.ZERO
|
||||
var character_movement_range = 5
|
||||
|
||||
|
||||
|
||||
func new_character(chara):
|
||||
|
||||
# don't allow non-character characters
|
||||
if not chara.is_in_group("OnMap"):
|
||||
return
|
||||
|
||||
# clear the movement queue upon a change in character
|
||||
movement_queue = []
|
||||
|
||||
# set the character's map postiton
|
||||
chara.map_position = $TileMap.world_to_map(chara.position)
|
||||
chara.target_position = $TileMap.map_to_world(chara.map_position)
|
||||
chara.target_position += $TileMap.cell_size/2
|
||||
|
||||
# set the original_position
|
||||
character_original_position = chara.map_position
|
||||
|
||||
character = chara
|
||||
|
||||
|
||||
func character_movement_input():
|
||||
if Input.is_action_just_pressed("ui_up"):
|
||||
movement_queue.append(Vector2.UP)
|
||||
if Input.is_action_just_pressed("ui_down"):
|
||||
movement_queue.append(Vector2.DOWN)
|
||||
if Input.is_action_just_pressed("ui_left"):
|
||||
movement_queue.append(Vector2.LEFT)
|
||||
if Input.is_action_just_pressed("ui_right"):
|
||||
movement_queue.append(Vector2.RIGHT)
|
||||
|
||||
if Input.is_action_just_pressed("ui_accept"):
|
||||
emit_signal("scene_finished")
|
||||
|
||||
|
||||
func character_movement():
|
||||
#remove uneeded inputs from the queue
|
||||
for i in len(movement_queue) - 1:
|
||||
#remove if adjacent values are opposites
|
||||
if (movement_queue[i].x * movement_queue[i+1].x == -1 or
|
||||
movement_queue[i].y * movement_queue[i+1].y == -1):
|
||||
movement_queue[i] = Vector2.ZERO
|
||||
movement_queue[i+1] = Vector2.ZERO
|
||||
#remove null values
|
||||
for i in len(movement_queue):
|
||||
if movement_queue.find(Vector2.ZERO) != -1:
|
||||
movement_queue.remove(movement_queue.find(Vector2.ZERO))
|
||||
|
||||
if len(movement_queue) == 0:
|
||||
return
|
||||
|
||||
if not character.moving:
|
||||
|
||||
var total_distance = character.map_position + movement_queue[0]
|
||||
total_distance -= character_original_position
|
||||
|
||||
#if the character is moving too far, cancel movement and empty queue
|
||||
if total_distance.length() > character_movement_range:
|
||||
movement_queue = []
|
||||
return
|
||||
|
||||
#move the character once space in the queue if not moving
|
||||
character.map_position += movement_queue.pop_front()
|
||||
character.target_position = $TileMap.map_to_world(character.map_position)
|
||||
character.target_position += $TileMap.cell_size/2
|
||||
|
||||
|
||||
func _physics_process(_delta):
|
||||
character_movement()
|
||||
character_movement_input()
|
48
Scenes/Battle.tscn
Normal file
48
Scenes/Battle.tscn
Normal file
@ -0,0 +1,48 @@
|
||||
[gd_scene load_steps=5 format=2]
|
||||
|
||||
[ext_resource path="res://icon.png" type="Texture" id=1]
|
||||
[ext_resource path="res://Assets/TestTile.png" type="Texture" id=2]
|
||||
[ext_resource path="res://Scenes/Battle.gd" type="Script" id=3]
|
||||
|
||||
[sub_resource type="TileSet" id=1]
|
||||
0/name = "icon.png 0"
|
||||
0/texture = ExtResource( 1 )
|
||||
0/tex_offset = Vector2( 0, 0 )
|
||||
0/modulate = Color( 1, 1, 1, 1 )
|
||||
0/region = Rect2( 8, 4, 51, 19 )
|
||||
0/tile_mode = 0
|
||||
0/occluder_offset = Vector2( 0, 0 )
|
||||
0/navigation_offset = Vector2( 0, 0 )
|
||||
0/shape_offset = Vector2( 0, 0 )
|
||||
0/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
|
||||
0/shape_one_way = false
|
||||
0/shape_one_way_margin = 0.0
|
||||
0/shapes = [ ]
|
||||
0/z_index = 0
|
||||
1/name = "TestTile.png 1"
|
||||
1/texture = ExtResource( 2 )
|
||||
1/tex_offset = Vector2( 16, 0 )
|
||||
1/modulate = Color( 1, 1, 1, 1 )
|
||||
1/region = Rect2( 0, 0, 160, 64 )
|
||||
1/tile_mode = 0
|
||||
1/occluder_offset = Vector2( 0, 0 )
|
||||
1/navigation_offset = Vector2( 0, 0 )
|
||||
1/shape_offset = Vector2( 0, 0 )
|
||||
1/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
|
||||
1/shape_one_way = false
|
||||
1/shape_one_way_margin = 0.0
|
||||
1/shapes = [ ]
|
||||
1/z_index = 0
|
||||
|
||||
[node name="Battle" type="Node2D"]
|
||||
unique_name_in_owner = true
|
||||
script = ExtResource( 3 )
|
||||
|
||||
[node name="TileMap" type="TileMap" parent="."]
|
||||
mode = 2
|
||||
tile_set = SubResource( 1 )
|
||||
cell_size = Vector2( 128, 64 )
|
||||
cell_custom_transform = Transform2D( 128, 0, 32, 64, 0, 0 )
|
||||
centered_textures = true
|
||||
format = 1
|
||||
tile_data = PoolIntArray( 65538, 1, 0, 65539, 1, 0, 65540, 1, 0, 65541, 1, 0, 131074, 1, 0, 131075, 1, 0, 131076, 1, 0, 131077, 1, 0, 131078, 0, 0, 196610, 1, 0, 196611, 1, 0, 196612, 1, 0, 196613, 1, 0, 196614, 0, 0, 262144, 0, 0, 262145, 0, 0, 262146, 1, 0, 262147, 1, 0, 262148, 1, 0, 262149, 0, 0, 262150, 0, 0, 327681, 0, 0, 327682, 0, 0, 327683, 0, 0, 327684, 0, 0, 327685, 0, 0, 327686, 0, 0, 393217, 0, 0, 393218, 0, 0, 393219, 0, 0, 393220, 0, 0, 393221, 0, 0, 393222, 0, 0, 458754, 0, 0, 458755, 0, 0, 458756, 0, 0, 458757, 0, 0, 524291, 0, 0, 524292, 0, 0 )
|
Loading…
Reference in New Issue
Block a user