-
Notifications
You must be signed in to change notification settings - Fork 6
/
mob.go
99 lines (89 loc) · 2.75 KB
/
mob.go
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
/*
Copyright (C) 2021 Alexander Lunsford
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"github.com/thetophatdemon/feta-feles-rebirth/audio"
"github.com/thetophatdemon/feta-feles-rebirth/vmath"
)
type Mob struct {
*Actor
health int
hurtTimer float64
currAnim *Anim
dead bool
lastSeenPlayerPos *vmath.Vec2f
vecToPlayer *vmath.Vec2f
distToPlayer float64
seesPlayer bool
hunting bool //Switched on after monster sees player for the first time
}
func (mb *Mob) Update(game *Game, obj *Object) {
mb.vecToPlayer = game.playerObj.pos.Clone().Sub(obj.pos)
mb.distToPlayer = mb.vecToPlayer.Length()
if raycast := game.level.Raycast(obj.pos.Clone(), mb.vecToPlayer, SCR_HEIGHT); raycast != nil {
if raycast.distance >= mb.vecToPlayer.Length() {
mb.lastSeenPlayerPos = game.playerObj.pos.Clone()
mb.seesPlayer = true
mb.hunting = true
} else {
mb.seesPlayer = false
}
}
if mb.hurtTimer > 0.0 {
mb.hurtTimer -= game.deltaTime
if int(mb.hurtTimer/0.125)%2 == 0 {
obj.hidden = false
} else {
obj.hidden = true
}
if mb.hurtTimer < 0.0 {
obj.hidden = false
mb.hurtTimer = 0.0
}
}
if mb.currAnim != nil {
mb.currAnim.Update(game.deltaTime)
obj.sprites[0] = mb.currAnim.GetSprite()
}
}
func (mb *Mob) OnCollision(game *Game, obj *Object, other *Object) {
if mb.hurtTimer <= 0.0 && other.HasColType(CT_PLAYERSHOT|CT_EXPLOSION) {
mb.health--
if other.HasColType(CT_EXPLOSION) {
mb.health -= 9
} else if other.HasColType(CT_BOUNCYSHOT) { //Bouncy shots do double damage
mb.health--
}
if mb.health > 0 {
mb.hurtTimer = 0.5
audio.PlaySound("enemy_hurt")
}
}
if other.colType == obj.colType {
diff := obj.pos.Clone().Sub(other.pos)
diffL := diff.Length()
if diffL != 0.0 {
diff.Normalize()
mb.velocity.Add(diff.Scale(obj.radius + other.radius/game.deltaTime))
}
}
}
//Makes the monster travel around aimlessly
func (mb *Mob) Wander(game *Game, obj *Object, rayDist, turnSpeed float64) {
//Cast a ray in front of the mob's trajectory
res := game.level.Raycast(obj.pos.Clone(), mb.movement.Clone(), rayDist)
if res.hit {
mb.Turn(turnSpeed, game.deltaTime)
}
}