aboutsummaryrefslogtreecommitdiff
path: root/internal/combat/state.go
blob: 43b693bbbb24a7b3d37763abedaf8a795114fb0e (plain)
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
package combat

import "sync"

type State struct {
	PlayerName  string
	MobID       string
	Active      bool
	LockedTicks int
}

var (
	mu          sync.Mutex
	combatants  = make(map[string]*State) // player name -> state
	mobTargets  = make(map[string]string) // mob ID -> player name
)

func EnterCombat(playerName, mobID string) {
	mu.Lock()
	defer mu.Unlock()
	combatants[playerName] = &State{
		PlayerName:  playerName,
		MobID:       mobID,
		Active:      true,
		LockedTicks: 15,
	}
	mobTargets[mobID] = playerName
}

func LeaveCombat(playerName string) {
	mu.Lock()
	defer mu.Unlock()
	state, ok := combatants[playerName]
	if !ok {
		return
	}
	delete(mobTargets, state.MobID)
	delete(combatants, playerName)
}

func GetCombat(playerName string) *State {
	mu.Lock()
	defer mu.Unlock()
	return combatants[playerName]
}

func IsMobInCombat(mobID string) bool {
	mu.Lock()
	defer mu.Unlock()
	_, ok := mobTargets[mobID]
	return ok
}

func GetMobTarget(mobID string) string {
	mu.Lock()
	defer mu.Unlock()
	return mobTargets[mobID]
}

func TickCombat() {
	mu.Lock()
	defer mu.Unlock()
	for _, state := range combatants {
		if state.LockedTicks > 0 {
			state.LockedTicks--
		}
	}
}