Initial Commit
This commit is contained in:
parent
2dce133760
commit
dbb0f17743
63 changed files with 2881 additions and 1 deletions
8
Scripts/Units/NPCExample.gd
Normal file
8
Scripts/Units/NPCExample.gd
Normal file
|
@ -0,0 +1,8 @@
|
|||
extends NPCUnit
|
||||
|
||||
|
||||
func before_tick():
|
||||
if scene.rng.randf() < 0.5:
|
||||
facing = Constants.Direction.RIGHT
|
||||
else:
|
||||
facing = Constants.Direction.LEFT
|
79
Scripts/Units/NPCUnit.gd
Normal file
79
Scripts/Units/NPCUnit.gd
Normal file
|
@ -0,0 +1,79 @@
|
|||
extends Unit
|
||||
|
||||
class_name NPCUnit
|
||||
|
||||
var spawn_point : Vector2
|
||||
|
||||
export var tick_duration : float
|
||||
var tick_timer : float = 0
|
||||
|
||||
export(Dictionary) var action_sequence_map # action sequence to weight, [] = do nothing
|
||||
# action sequence is an array of action type and an array of timestamps
|
||||
# action type is the string representation of Constants.ActionType
|
||||
# example action map: {[[action1], [0]]: 1, [[action2, action3], [0, 1]]: 2}
|
||||
var weight_sum : float
|
||||
|
||||
export var action_duration_map = {} # specific durations for given action
|
||||
var current_npc_action_times_elapsed = {}
|
||||
var current_npc_action_active = {}
|
||||
|
||||
var current_action_sequence = null
|
||||
var current_action_sequence_time_elapsed : float = 0
|
||||
var current_action_sequence_index : int = 0
|
||||
|
||||
func _ready():
|
||||
for action_sequence in action_sequence_map.keys():
|
||||
weight_sum += action_sequence_map[action_sequence]
|
||||
for action in action_duration_map:
|
||||
current_npc_action_times_elapsed[action] = 0
|
||||
current_npc_action_active[action] = false
|
||||
|
||||
func before_tick():
|
||||
pass
|
||||
|
||||
func handle_input(delta):
|
||||
if current_action_sequence != null:
|
||||
for action in current_npc_action_active:
|
||||
if current_npc_action_active[action]:
|
||||
if current_npc_action_times_elapsed[action] < action_duration_map[action]:
|
||||
set_action(Constants.ActionType.get(action))
|
||||
current_npc_action_times_elapsed[action] += delta
|
||||
else:
|
||||
current_npc_action_active[action] = false
|
||||
if (current_action_sequence_index < current_action_sequence[1].size()
|
||||
and current_action_sequence_time_elapsed >= current_action_sequence[1][current_action_sequence_index]):
|
||||
var action = current_action_sequence[0][current_action_sequence_index]
|
||||
set_action(Constants.ActionType.get(action))
|
||||
if action_duration_map.has(action):
|
||||
current_npc_action_active[action] = true
|
||||
current_npc_action_times_elapsed[action] = 0
|
||||
current_action_sequence_index += 1
|
||||
var current_action_sequence_duration : float = current_action_sequence[1][-1]
|
||||
if action_duration_map.has(current_action_sequence[0][-1]):
|
||||
current_action_sequence_duration += action_duration_map[current_action_sequence[0][-1]]
|
||||
if current_action_sequence_time_elapsed > current_action_sequence_duration:
|
||||
reset_npc_unit()
|
||||
current_action_sequence_time_elapsed += delta
|
||||
else:
|
||||
if tick_timer == 0:
|
||||
before_tick()
|
||||
var rand_num : float = scene.rng.randf() * weight_sum
|
||||
var temp_sum : float = 0
|
||||
for action_sequence in action_sequence_map.keys():
|
||||
temp_sum += action_sequence_map[action_sequence]
|
||||
if temp_sum >= rand_num:
|
||||
if action_sequence == []:
|
||||
tick_timer = tick_duration
|
||||
else:
|
||||
current_action_sequence = action_sequence
|
||||
current_action_sequence_time_elapsed = 0
|
||||
current_action_sequence_index = 0
|
||||
break
|
||||
else:
|
||||
tick_timer = max(0, tick_timer - delta)
|
||||
|
||||
func reset_npc_unit():
|
||||
current_action_sequence = null
|
||||
tick_timer = tick_duration
|
||||
for action in current_npc_action_active:
|
||||
current_npc_action_active[action] = false
|
92
Scripts/Units/Player.gd
Normal file
92
Scripts/Units/Player.gd
Normal file
|
@ -0,0 +1,92 @@
|
|||
extends Unit
|
||||
|
||||
# Player-specific code
|
||||
class_name Player
|
||||
|
||||
const RECOIL_PUSHBACK = 15
|
||||
|
||||
func _init():
|
||||
pos = Vector2(position.x / Constants.GRID_SIZE, -1 * position.y / Constants.GRID_SIZE)
|
||||
position.x = position.x * Constants.SCALE_FACTOR
|
||||
position.y = position.y * Constants.SCALE_FACTOR
|
||||
|
||||
func execute_actions(delta):
|
||||
.execute_actions(delta)
|
||||
for action_num in Constants.UNIT_TYPE_ACTIONS[Constants.UnitType.PLAYER]:
|
||||
if !actions[action_num]:
|
||||
continue
|
||||
match action_num:
|
||||
# handle custom actions
|
||||
Constants.ActionType.RECOIL:
|
||||
recoil()
|
||||
_:
|
||||
pass
|
||||
|
||||
func recoil():
|
||||
if is_current_action_timer_done(Constants.UnitCurrentAction.RECOILING):
|
||||
set_current_action(Constants.UnitCurrentAction.IDLE)
|
||||
else:
|
||||
set_current_action(Constants.UnitCurrentAction.RECOILING)
|
||||
set_sprite(Constants.SpriteClass.RECOIL)
|
||||
|
||||
func handle_input(delta):
|
||||
scene.handle_player_input()
|
||||
|
||||
func _on_Player_area_entered(area: Area2D) -> void:
|
||||
if get_condition(Constants.UnitCondition.IS_INVINCIBLE, false):
|
||||
return
|
||||
if area is Unit:
|
||||
hit_from_area(area)
|
||||
|
||||
func hit_from_area(other_area : Area2D):
|
||||
var collision_dir : int
|
||||
if other_area.position > position:
|
||||
collision_dir = Constants.Direction.RIGHT
|
||||
else:
|
||||
collision_dir = Constants.Direction.LEFT
|
||||
hit(collision_dir)
|
||||
|
||||
func hit(dir : int):
|
||||
.hit(dir)
|
||||
set_unit_condition_with_timer(Constants.UnitCondition.IS_INVINCIBLE)
|
||||
start_flash()
|
||||
set_action(Constants.ActionType.RECOIL)
|
||||
set_current_action(Constants.UnitCurrentAction.RECOILING)
|
||||
set_unit_condition(Constants.UnitCondition.MOVING_STATUS, Constants.UnitMovingStatus.IDLE)
|
||||
|
||||
func invincibility_ended():
|
||||
is_flash = false
|
||||
if get_overlapping_areas().size() > 0:
|
||||
if get_overlapping_areas()[0] is Unit:
|
||||
hit_from_area(get_overlapping_areas()[0])
|
||||
|
||||
func handle_recoil():
|
||||
if not hit_queued:
|
||||
return
|
||||
hit_queued = false
|
||||
if get_condition(Constants.UnitCondition.IS_ON_GROUND, true):
|
||||
if h_speed > 0:
|
||||
if hit_dir == Constants.Direction.LEFT:
|
||||
v_speed -= RECOIL_PUSHBACK
|
||||
else:
|
||||
v_speed += RECOIL_PUSHBACK
|
||||
elif h_speed < 0:
|
||||
if hit_dir == Constants.Direction.LEFT:
|
||||
v_speed += RECOIL_PUSHBACK
|
||||
else:
|
||||
v_speed -= RECOIL_PUSHBACK
|
||||
else:
|
||||
v_speed = -RECOIL_PUSHBACK
|
||||
if hit_dir == Constants.Direction.LEFT:
|
||||
h_speed = Constants.QUANTUM_DIST
|
||||
else:
|
||||
h_speed = -Constants.QUANTUM_DIST
|
||||
if v_speed > 0:
|
||||
h_speed *= -1
|
||||
v_speed = -v_speed
|
||||
else:
|
||||
if hit_dir == Constants.Direction.LEFT:
|
||||
h_speed += RECOIL_PUSHBACK
|
||||
else:
|
||||
h_speed -= RECOIL_PUSHBACK
|
||||
facing = hit_dir
|
Loading…
Add table
Add a link
Reference in a new issue