aboutsummaryrefslogtreecommitdiff
path: root/internal/game/action_toggle.go
blob: f20d03ca8bf57a0ee685bae81290f5280b7f0144 (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
package game

import (
	"fmt"

	"thirdcollapse/internal/net"
	"thirdcollapse/internal/object"
	"thirdcollapse/internal/player"
)

func (g *Game) startToggle(sess *net.Session, p *player.Player, obj *object.ObjectDef) {
	cfg, err := g.BehaviorStore.LoadToggle(obj.BehaviorID)
	if err != nil {
		sess.WriteLine("Nothing happens.")
		return
	}

	if cfg.Check != nil {
		val, ok := g.WorldFlags[cfg.Check.Flag]
		if cfg.Check.Not {
			if ok && val == cfg.Check.Value {
				sess.WriteLine("Nothing happens.")
				return
			}
		} else {
			if !ok || val != cfg.Check.Value {
				sess.WriteLine("Nothing happens.")
				return
			}
		}
	}

	for flag, val := range cfg.SetFlags {
		g.WorldFlags[flag] = val
	}

	p.ActionState = &ActionState{Type: ActionToggling, TargetName: obj.Name}

	sess.WriteLine(fmt.Sprintf("\n%s", cfg.Message))

	if g.Hub != nil {
		for _, other := range g.Hub.PlayersInRoom(p.RoomID) {
			if other != sess && other.Player != nil {
				other.WriteLine(fmt.Sprintf("\n%s uses the %s.", p.Name, obj.Name))
			}
		}
	}
}