-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snowman.gd
114 lines (83 loc) · 3.1 KB
/
Snowman.gd
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
extends KinematicBody
export(float) var throwFrequency = 5.0
export(bool) var walkingEnabled = false
export(float) var walkingSpeedMin = 0.2
export(float) var walkingSpeedMax = 1.0
const COLLAPSE_TIME = 2.0
var lastThrowTime = 0.0
var throwCount = 0
var player
var rng = RandomNumberGenerator.new()
var randomWalkDir = Vector3()
var lastWalkDirChangeTime = 0.0
var newDirTime = rng.randf_range(0.5, 2.0)
var velocity = Vector3()
var collapsed = false
var collapseTime = 0.0
var collapsedSnowman = null
const Snowball = preload("res://Snowball.tscn")
const CollapsedSnowman = preload("res://CollapsedSnowman.tscn")
func _ready():
rng.randomize()
lastThrowTime = rng.randf_range(-1, 0)
func _physics_process(delta):
if collapsed:
collapseTime -= delta
if collapseTime <= 0:
remove_child(collapsedSnowman)
collapsedSnowman = null
collapseTime = 0
collapsed = false
$CollisionShape.disabled = false
$AnimatedSprite3D.visible = true
lastThrowTime = rng.randf_range(-2, -1)
lastWalkDirChangeTime = 0.0
return
lastThrowTime += delta
lastWalkDirChangeTime += delta
if lastWalkDirChangeTime >= newDirTime:
var angle = rng.randf_range(0, 2 * PI)
randomWalkDir = Vector3(cos(angle), 0, sin(angle)) * rng.randf_range(walkingSpeedMin, walkingSpeedMax)
lastWalkDirChangeTime = 0.0
newDirTime = rng.randf_range(0.5, 2.0)
if walkingEnabled:
velocity = randomWalkDir
velocity = move_and_slide(velocity, Vector3(0, 1, 0))
if lastThrowTime >= 1 / throwFrequency and player != null:
# Get player
var throwDir = player.global_transform.origin - global_transform.origin
throwDir.y = 0
var d = throwDir.length()
throwDir = throwDir.normalized()
if d < 10:
var throwVel = 8
var throwAngle = 0.1 * PI * min(d / 8, 1)
var snowball = Snowball.instance()
snowball.velocity = throwVel * (throwDir * cos(throwAngle) + Vector3(0, 1, 0) * sin(throwAngle))
var sideOffs = Vector3(throwDir.z, 0, -throwDir.x) * .3
if throwCount % 2 == 1:
sideOffs = -sideOffs
snowball.transform.origin = transform.origin + 0.9 * throwDir + sideOffs + Vector3(0, 0.2, 0)
get_parent().add_child(snowball)
add_collision_exception_with(snowball)
throwCount += 1
lastThrowTime = 0.0
func hit_by_paintball(paintball):
collapse()
func collapse():
if collapsed:
return
collapsed = true
collapseTime = COLLAPSE_TIME
$CollisionShape.disabled = true
$AnimatedSprite3D.visible = false
collapsedSnowman = CollapsedSnowman.instance()
var headRandDir = Vector3(rng.randf_range(-1, 1), rng.randf_range(-0.5, 0.5), rng.randf_range(-1, 1)) * 3.0
var upperBodyRandDir = Vector3(rng.randf_range(-1, 1), rng.randf_range(-0.5, 0.5), rng.randf_range(-1, 1)) * 3.0
var lowerBodyRandDir = Vector3(rng.randf_range(-1, 1), rng.randf_range(-0.5, 0.5), rng.randf_range(-1, 1)) * 3.0
add_child(collapsedSnowman)
collapsedSnowman.get_node("Head").apply_central_impulse(headRandDir)
collapsedSnowman.get_node("UpperBody").apply_central_impulse(upperBodyRandDir)
collapsedSnowman.get_node("LowerBody").apply_central_impulse(lowerBodyRandDir)
func set_player(p):
player = p