-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_world.go
84 lines (77 loc) · 1.51 KB
/
game_world.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
package main
import (
"fmt"
"strings"
)
type (
FightEvent struct {
Won bool
EnemyName string
Event
}
GameWorld struct {
Hero *HeroSheet
SendEvent chan string
SendLog chan LogEvent
}
)
func NewGameWorld(hero *HeroSheet) *GameWorld {
return &GameWorld{hero, make(chan string, 10), make(chan LogEvent, 100)}
}
func (g *GameWorld) initStorage(events chan string) {
for event := range events {
if strings.HasPrefix(event, "You Won") {
g.Hero.Xp = g.Hero.Xp + 10
log := LogEvent{
"Fight: " + event,
int(g.Hero.Xp),
int(g.Hero.Life),
int(g.Hero.Speed),
int(g.Hero.Power),
int(g.Hero.Ancestry),
}
g.SendLog <- log
} else {
log := LogEvent{
"Fight: " + event,
int(g.Hero.Xp),
int(g.Hero.Life),
int(g.Hero.Speed),
int(g.Hero.Power),
int(g.Hero.Ancestry),
}
g.SendLog <- log
}
}
}
func (g *GameWorld) addChannel(events chan interface{}) {
go func() {
for {
e := <-events
switch event := e.(type) {
case FightEvent:
message := fmt.Sprintf("Fight: %s\n", event.String())
g.SendEvent <- event.String()
if event.Won {
g.Hero.Xp = g.Hero.Xp + 10
}
log := LogEvent{
message,
int(g.Hero.Xp),
int(g.Hero.Life),
int(g.Hero.Speed),
int(g.Hero.Power),
int(g.Hero.Ancestry),
}
g.SendLog <- log
}
}
}()
}
func (f *FightEvent) String() string {
if f.Won {
return "You Won a fight with a " + f.EnemyName
} else {
return "You Lost a fight with a " + f.EnemyName
}
}