2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
extends RigidBody
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
# member variables
|
2014-02-10 01:10:30 +00:00
|
|
|
const ANIM_FLOOR = 0
|
|
|
|
const ANIM_AIR_UP = 1
|
|
|
|
const ANIM_AIR_DOWN = 2
|
|
|
|
|
|
|
|
const SHOOT_TIME = 1.5
|
|
|
|
const SHOOT_SCALE = 2
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
const CHAR_SCALE = Vector3(0.3, 0.3, 0.3)
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
var facing_dir = Vector3(1, 0, 0)
|
|
|
|
var movement_dir = Vector3()
|
|
|
|
|
|
|
|
var jumping=false
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
var turn_speed = 40
|
2014-02-10 01:10:30 +00:00
|
|
|
var keep_jump_inertia = true
|
|
|
|
var air_idle_deaccel = false
|
2015-12-09 07:38:23 +00:00
|
|
|
var accel = 19.0
|
|
|
|
var deaccel = 14.0
|
2014-02-10 01:10:30 +00:00
|
|
|
var sharp_turn_threshhold = 140
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
var max_speed = 3.1
|
2014-02-10 01:10:30 +00:00
|
|
|
var on_floor = false
|
|
|
|
|
|
|
|
var prev_shoot = false
|
|
|
|
|
|
|
|
var last_floor_velocity = Vector3()
|
|
|
|
|
|
|
|
var shoot_blend = 0
|
|
|
|
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
func adjust_facing(p_facing, p_target, p_step, p_adjust_rate, current_gn):
|
2014-02-10 01:10:30 +00:00
|
|
|
var n = p_target # normal
|
|
|
|
var t = n.cross(current_gn).normalized()
|
|
|
|
|
|
|
|
var x = n.dot(p_facing)
|
|
|
|
var y = t.dot(p_facing)
|
|
|
|
|
|
|
|
var ang = atan2(y,x)
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
if (abs(ang) < 0.001): # too small
|
2014-02-10 01:10:30 +00:00
|
|
|
return p_facing
|
|
|
|
|
|
|
|
var s = sign(ang)
|
2015-12-09 07:38:23 +00:00
|
|
|
ang = ang*s
|
|
|
|
var turn = ang*p_adjust_rate*p_step
|
2014-02-10 01:10:30 +00:00
|
|
|
var a
|
2015-12-09 07:38:23 +00:00
|
|
|
if (ang < turn):
|
|
|
|
a = ang
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-12-09 07:38:23 +00:00
|
|
|
a = turn
|
|
|
|
ang = (ang - a)*s
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
return (n*cos(ang) + t*sin(ang))*p_facing.length()
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
func _integrate_forces(state):
|
2014-02-10 01:10:30 +00:00
|
|
|
var lv = state.get_linear_velocity() # linear velocity
|
|
|
|
var g = state.get_total_gravity()
|
|
|
|
var delta = state.get_step()
|
2015-09-20 16:03:46 +00:00
|
|
|
# var d = 1.0 - delta*state.get_total_density()
|
2015-12-09 07:38:23 +00:00
|
|
|
# if (d < 0):
|
|
|
|
# d = 0
|
|
|
|
lv += g*delta # apply gravity
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
var anim = ANIM_FLOOR
|
2015-12-09 07:38:23 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
var up = -g.normalized() # (up is against gravity)
|
|
|
|
var vv = up.dot(lv) # vertical velocity
|
2015-12-09 07:38:23 +00:00
|
|
|
var hv = lv - up*vv # horizontal velocity
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
var hdir = hv.normalized() # horizontal direction
|
2015-12-09 07:38:23 +00:00
|
|
|
var hspeed = hv.length() # horizontal speed
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
var floor_velocity
|
|
|
|
var onfloor = false
|
2015-12-09 07:38:23 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (state.get_contact_count() == 0):
|
|
|
|
floor_velocity = last_floor_velocity
|
|
|
|
else:
|
|
|
|
for i in range(state.get_contact_count()):
|
|
|
|
if (state.get_contact_local_shape(i) != 1):
|
|
|
|
continue
|
|
|
|
|
|
|
|
onfloor = true
|
|
|
|
floor_velocity = state.get_contact_collider_velocity_at_pos(i)
|
|
|
|
break
|
2015-12-09 07:38:23 +00:00
|
|
|
|
|
|
|
var dir = Vector3() # where does the player intend to walk to
|
2014-02-10 01:10:30 +00:00
|
|
|
var cam_xform = get_node("target/camera").get_global_transform()
|
|
|
|
|
|
|
|
if (Input.is_action_pressed("move_forward")):
|
2015-12-09 07:38:23 +00:00
|
|
|
dir += -cam_xform.basis[2]
|
2014-02-10 01:10:30 +00:00
|
|
|
if (Input.is_action_pressed("move_backwards")):
|
2015-12-09 07:38:23 +00:00
|
|
|
dir += cam_xform.basis[2]
|
2014-02-10 01:10:30 +00:00
|
|
|
if (Input.is_action_pressed("move_left")):
|
2015-12-09 07:38:23 +00:00
|
|
|
dir += -cam_xform.basis[0]
|
2014-02-10 01:10:30 +00:00
|
|
|
if (Input.is_action_pressed("move_right")):
|
2015-12-09 07:38:23 +00:00
|
|
|
dir += cam_xform.basis[0]
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
var jump_attempt = Input.is_action_pressed("jump")
|
|
|
|
var shoot_attempt = Input.is_action_pressed("shoot")
|
2015-12-09 07:38:23 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
var target_dir = (dir - up*dir.dot(up)).normalized()
|
|
|
|
|
|
|
|
if (onfloor):
|
|
|
|
var sharp_turn = hspeed > 0.1 and rad2deg(acos(target_dir.dot(hdir))) > sharp_turn_threshhold
|
2015-12-09 07:38:23 +00:00
|
|
|
|
|
|
|
if (dir.length() > 0.1 and !sharp_turn):
|
|
|
|
if (hspeed > 0.001):
|
2014-02-10 01:10:30 +00:00
|
|
|
#linear_dir = linear_h_velocity/linear_vel
|
2015-12-09 07:38:23 +00:00
|
|
|
#if (linear_vel > brake_velocity_limit and linear_dir.dot(ctarget_dir) < -cos(Math::deg2rad(brake_angular_limit)))
|
|
|
|
# brake = true
|
2014-02-10 01:10:30 +00:00
|
|
|
#else
|
2015-12-09 07:38:23 +00:00
|
|
|
hdir = adjust_facing(hdir, target_dir, delta, 1.0/hspeed*turn_speed, up)
|
2014-02-10 01:10:30 +00:00
|
|
|
facing_dir = hdir
|
|
|
|
else:
|
|
|
|
hdir = target_dir
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
if (hspeed < max_speed):
|
|
|
|
hspeed += accel*delta
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-12-09 07:38:23 +00:00
|
|
|
hspeed -= deaccel*delta
|
|
|
|
if (hspeed < 0):
|
|
|
|
hspeed = 0
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
hv = hdir*hspeed
|
|
|
|
|
|
|
|
var mesh_xform = get_node("Armature").get_transform()
|
2015-12-09 07:38:23 +00:00
|
|
|
var facing_mesh = -mesh_xform.basis[0].normalized()
|
2014-02-10 01:10:30 +00:00
|
|
|
facing_mesh = (facing_mesh - up*facing_mesh.dot(up)).normalized()
|
2015-12-09 07:38:23 +00:00
|
|
|
facing_mesh = adjust_facing(facing_mesh, target_dir, delta, 1.0/hspeed*turn_speed, up)
|
|
|
|
var m3 = Matrix3(-facing_mesh, up, -facing_mesh.cross(up).normalized()).scaled(CHAR_SCALE)
|
|
|
|
|
|
|
|
get_node("Armature").set_transform(Transform(m3, mesh_xform.origin))
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (not jumping and jump_attempt):
|
|
|
|
vv = 7.0
|
2015-12-09 07:38:23 +00:00
|
|
|
jumping = true
|
2014-02-10 01:10:30 +00:00
|
|
|
get_node("sfx").play("jump")
|
|
|
|
else:
|
2015-12-09 07:38:23 +00:00
|
|
|
if (vv > 0):
|
|
|
|
anim = ANIM_AIR_UP
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
2015-12-09 07:38:23 +00:00
|
|
|
anim = ANIM_AIR_DOWN
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
var hs
|
2015-12-09 07:38:23 +00:00
|
|
|
if (dir.length() > 0.1):
|
|
|
|
hv += target_dir*(accel*0.2)*delta
|
2014-02-10 01:10:30 +00:00
|
|
|
if (hv.length() > max_speed):
|
2015-12-09 07:38:23 +00:00
|
|
|
hv = hv.normalized()*max_speed
|
2014-02-10 01:10:30 +00:00
|
|
|
else:
|
|
|
|
if (air_idle_deaccel):
|
2015-12-09 07:38:23 +00:00
|
|
|
hspeed = hspeed - (deaccel*0.2)*delta
|
|
|
|
if (hspeed < 0):
|
|
|
|
hspeed = 0
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
hv = hdir*hspeed
|
2015-12-09 07:38:23 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (jumping and vv < 0):
|
2015-12-09 07:38:23 +00:00
|
|
|
jumping = false
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
lv = hv + up*vv
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (onfloor):
|
|
|
|
movement_dir = lv
|
|
|
|
#lv += floor_velocity
|
|
|
|
last_floor_velocity = floor_velocity
|
|
|
|
else:
|
|
|
|
if (on_floor) :
|
|
|
|
#if (keep_jump_inertia):
|
|
|
|
# lv += last_floor_velocity
|
|
|
|
pass
|
|
|
|
|
|
|
|
last_floor_velocity = Vector3()
|
|
|
|
movement_dir = lv
|
|
|
|
|
|
|
|
on_floor = onfloor
|
|
|
|
|
|
|
|
state.set_linear_velocity(lv)
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
if (shoot_blend > 0):
|
|
|
|
shoot_blend -= delta*SHOOT_SCALE
|
|
|
|
if (shoot_blend < 0):
|
|
|
|
shoot_blend = 0
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (shoot_attempt and not prev_shoot):
|
2015-12-09 07:38:23 +00:00
|
|
|
shoot_blend = SHOOT_TIME
|
2014-02-10 01:10:30 +00:00
|
|
|
var bullet = preload("res://bullet.scn").instance()
|
2015-12-09 07:38:23 +00:00
|
|
|
bullet.set_transform(get_node("Armature/bullet").get_global_transform().orthonormalized())
|
|
|
|
get_parent().add_child(bullet)
|
|
|
|
bullet.set_linear_velocity(get_node("Armature/bullet").get_global_transform().basis[2].normalized()*20)
|
|
|
|
PS.body_add_collision_exception(bullet.get_rid(), get_rid()) # add it to bullet
|
2014-02-10 01:10:30 +00:00
|
|
|
get_node("sfx").play("shoot")
|
2015-12-09 07:38:23 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
prev_shoot = shoot_attempt
|
|
|
|
|
|
|
|
if (onfloor):
|
2015-12-09 07:38:23 +00:00
|
|
|
get_node("AnimationTreePlayer").blend2_node_set_amount("walk", hspeed/max_speed)
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
get_node("AnimationTreePlayer").transition_node_set_current("state", anim)
|
|
|
|
get_node("AnimationTreePlayer").blend2_node_set_amount("gun", min(shoot_blend, 1.0))
|
|
|
|
# state.set_angular_velocity(Vector3())
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
# Initalization here
|
|
|
|
get_node("AnimationTreePlayer").set_active(true)
|