Skip to content

Instantly share code, notes, and snippets.

@securas
Created September 26, 2018 11:30
Show Gist options
  • Save securas/0082693a47258fd245593ae9dea865e8 to your computer and use it in GitHub Desktop.
Save securas/0082693a47258fd245593ae9dea865e8 to your computer and use it in GitHub Desktop.
extends "res://scripts/controllable.gd"
signal player_dead
#const BULLET_VEL = 250
var cur_bullet = 0
const BULLET_DELAYS = [ 0.15, 0.07 ]
var bullet_scn = preload( "res://player/bullet.tscn" )
var bullet_blast_scn = preload( "res://player/bullet_blast.tscn" )
var fsm = null
var anim_cur = ""
var anim_nxt = ""
var is_active = true
var is_hit = false
var is_fire = false
var aiming_dir = Vector2( 1, 0 )
var energy = 100 setget set_energy
var throwback = Vector2()
var throwback_time = 0
onready var arm = $arm
onready var rotate = $rotate
var is_cutscene = false
func set_energy( v ):
energy = v
$energy_bar.energy = v / 100.0
func _ready():
# register with game
game.player = self
# Initialize states machine
fsm = preload( "res://scripts/fsm.gd" ).new( self, $states, $states/idle )
fsm.state_nxt = fsm.STATES.idle
pass
func set_cutscene():
is_cutscene = true
func clear_cutscene():
$cutscene_timer.start()
func _physics_process( delta ):
if is_cutscene:
anim_nxt = "idle"
else:
# update states machine
fsm.run_machine( delta )
if not is_hit:
# aim with mouse
match fsm.state_cur:
fsm.STATES.idle, fsm.STATES.run:
var mouse_dir = get_global_mouse_position() - arm.global_position
if game.control_type == 0:
mouse_dir = get_global_mouse_position() - arm.global_position
else:
mouse_dir = Vector2( Input.get_joy_axis(0, JOY_ANALOG_RX), \
Input.get_joy_axis(0, JOY_ANALOG_RY) ).normalized()
pass
aiming_dir = mouse_dir
if mouse_dir.x > 0 and rotate.scale.x < 0 or \
mouse_dir.x < 0 and rotate.scale.x > 0:
rotate.scale.x = -rotate.scale.x
arm.scale.y = -arm.scale.y
arm.rotation = mouse_dir.angle()
# fire
if not is_fire and not is_cutscene and Input.is_action_pressed( "btn_fire" ):
is_fire = true
fire_state = FIRE_STATES.FIRE
if is_fire:
fire_fsm( delta )
# animation
if anim_nxt != anim_cur:
anim_cur = anim_nxt
$anim.play( anim_cur )
func hit( type, dir ):
if is_hit or is_cutscene: return
is_hit = true
match type:
0:
self.energy -= 1
throwback = dir * 100 * 0
throwback_time = 0.1 * 0
1:
self.energy -= 5
throwback = dir * 100 * 0
throwback_time = 0.1 * 0
2:
self.energy -= 10
throwback = dir * 100 * 0
throwback_time = 0.1 * 0
if energy <= 0:
energy = 0
# move to dead state
die()
else:
fsm.state_nxt = fsm.STATES.hit
pass
enum FIRE_STATES { INIT, FIRE, WAIT }
var fire_state
var fire_timer
func fire_fsm( delta ):
match fire_state:
INIT:
# do nothing
pass
FIRE:
# create bullet
var bullet = bullet_scn.instance()
bullet.type = cur_bullet
bullet.global_position = $arm/fire_position.global_position
var bdir = aiming_dir
bullet.dir = bdir.normalized()
bullet.set_sprite_rotation( bdir.angle() )
get_parent().add_child( bullet )
# create bullet blast
var blast = bullet_blast_scn.instance()
blast.position = $arm/fire_position.position
#blast.rotation = bdir.angle()
$arm.add_child( blast )
# throwback
vel -= bdir.normalized() * 100 / ( 1 + cur_bullet * 3 )
# nxt state
fire_timer = BULLET_DELAYS[ cur_bullet ]#0.15
fire_state = FIRE_STATES.WAIT
# sfx
#game.play_sfx( game.SFX_BULLET )
WAIT:
fire_timer -= delta
if fire_timer <= 0:
fire_state = FIRE_STATES.INIT
is_fire = false
func die():
fsm.state_nxt = fsm.STATES.dead
func _on_cutscene_timer_timeout():
is_cutscene = false
func bullet_timer():
$bullet_timer.start()
func _on_bullet_timer_timeout():
cur_bullet = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment