1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
extends RigidBody
###################-VARIABLES-####################
# Camera
export(float) var mouse_sensitivity = 12.0
export(NodePath) var cam_path
export(NodePath) var ai_follower = null
export(float) var FOV = 90.0
var mouse_axis := Vector2()
onready var cam: Camera = get_node(cam_path)
# Move
var velocity := Vector3()
var direction := Vector3()
var move_axis := Vector2()
# Walk
const FLOOR_MAX_ANGLE: float = deg2rad(46.0)
export(float) var gravity = 17.35
export(float) var jump_height = 1.084
var jump_vel = 5.89#sqrt(2 * gravity * jump_height)
var is_on_floor : bool = false
onready var feet = get_node("Feet")
var controlling_vehicle = false
var vehicle = null
export(float) var acceleration = 70
export(int) var walk_speed = 6
export(float) var _airspeed_cap = 1
export(float) var air_control = 1
# Fly
export(int) var fly_speed = 50
export(int) var fly_accel = 10
var flying := false
# Crouch
var is_duck = 0
const duck_height = .5*.6*1.8 #one half the hitbox duck height
var duck_speed = 2.3
var knockback_mult = 1
var duck_progress = 0
var world
# Shoot
var countdown = 0;
export(float) var firing_cooldown = .8
export(Vector3) var snap
static func compare_floats(a, b, epsilon = 0.00001):
return abs(a - b) <= epsilon
func walk(delta):
knockback_mult = 1.35 if is_duck else 1
#check if is on floor
if feet.is_colliding():
is_on_floor = true
# Duck
if not compare_floats(duck_progress, is_duck):
var h = $CollisionBox.get_shape().get_extents().y
var dir = -1 if is_duck else 1
var dh = dir*duck_speed*state.step
var desh = duck_height if is_duck else 0.9
if abs(h+dh - desh) < .05:
dh = desh-h
$CollisionBox.get_shape().set_extents(Vector3(.325,h+dh,.325))
$CollisionBox.translate_object_local(Vector3(0,-dh/2,0))
duck_progress = 1-(h+dh-duck_height)/(0.9-duck_height)
# Input
direction = Vector3()
var aim: Basis = get_global_transform().basis
if move_axis.x >= 0.5:
direction -= aim.z
elif move_axis.x <= -0.5:
direction += aim.z
if move_axis.y <= -0.5:
direction -= aim.x
elif move_axis.y >= 0.5:
direction += aim.x
direction.y = 0
direction = direction.normalized()
# Jump
if is_on_floor:
snap = Vector3.DOWN
if Input.is_action_just_pressed("move_jump"):
snap = Vector3.ZERO
velocity.y += jump_vel
velocity.y -= gravity * state.step
if abs(velocity.y) > 1:
snap = Vector3.ZERO
#max walk speed
var _speed = walk_speed if not is_duck else walk_speed*0.5
var _temp_accel: float = acceleration
if not is_on_floor or Input.is_action_just_pressed("move_jump"):
_temp_accel *= air_control
else: #apply friction
var _cspeed = sqrt(pow(velocity.x,2)+pow(velocity.z,2))
if _cspeed != 0:
var walkdir = Vector2(velocity.x,velocity.z)
var fvar = _cspeed*friction if direction.length() != 0 else _cspeed*2*friction
#fvar *= 4 if is_duck else 1
velocity.x -= walkdir.normalized().x*fvar*state.step
velocity.z -= walkdir.normalized().y*fvar*state.step
var projVel = Vector2(velocity.x,velocity.z).dot(Vector2(direction.x,direction.z))
_temp_accel *= state.step
if is_on_floor:
if _speed - (projVel + _temp_accel) > 0:
velocity.x += direction.x*_temp_accel
velocity.z += direction.z*_temp_accel
else:
velocity.x += direction.x*(_speed-projVel)
velocity.z += direction.z*(_speed-projVel)
elif _airspeed_cap - projVel > 0:
velocity.x += direction.x*_temp_accel
velocity.z += direction.z*_temp_accel
# clamping (to stop on slopes)
if direction.dot(velocity) == 0:
var _vel_clamp := 0.25
if abs(velocity.x) < _vel_clamp:
velocity.x = 0
if abs(velocity.z) < _vel_clamp:
velocity.z = 0
# Move
print(velocity)
set_linear_velocity(velocity)
velocity = get_linear_velocity()
|