-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.go
169 lines (154 loc) · 4.08 KB
/
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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"log"
"github.com/bytearena/ecs"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
var position *ecs.Component
var renderable *ecs.Component
var monster *ecs.Component
var health *ecs.Component
var meleeWeapon *ecs.Component
var armor *ecs.Component
var name *ecs.Component
var userMessage *ecs.Component
func InitializeWorld(startingLevel Level) (*ecs.Manager, map[string]ecs.Tag) {
tags := make(map[string]ecs.Tag)
manager := ecs.NewManager()
player := manager.NewComponent()
position = manager.NewComponent()
renderable = manager.NewComponent()
movable := manager.NewComponent()
monster = manager.NewComponent()
health = manager.NewComponent()
meleeWeapon = manager.NewComponent()
armor = manager.NewComponent()
name = manager.NewComponent()
userMessage = manager.NewComponent()
playerImg, _, err := ebitenutil.NewImageFromFile("assets/player.png")
if err != nil {
log.Fatal(err)
}
skellyImg, _, err := ebitenutil.NewImageFromFile("assets/skelly.png")
if err != nil {
log.Fatal(err)
}
orcImg, _, err := ebitenutil.NewImageFromFile("assets/orc.png")
if err != nil {
log.Fatal(err)
}
//Get First Room
startingRoom := startingLevel.Rooms[0]
x, y := startingRoom.Center()
manager.NewEntity().
AddComponent(player, Player{}).
AddComponent(renderable, &Renderable{
Image: playerImg,
}).
AddComponent(movable, Movable{}).
AddComponent(position, &Position{
X: x,
Y: y,
}).
AddComponent(health, &Health{
MaxHealth: 30,
CurrentHealth: 30,
}).
AddComponent(meleeWeapon, &MeleeWeapon{
Name: "Battle Axe",
MinimumDamage: 10,
MaximumDamage: 20,
ToHitBonus: 3,
}).
AddComponent(armor, &Armor{
Name: "Plate Armor",
Defense: 15,
ArmorClass: 18,
}).
AddComponent(name, &Name{Label: "Player"}).
AddComponent(userMessage, &UserMessage{
AttackMessage: "",
DeadMessage: "",
GameStateMessage: "",
})
//Add a Monster in each room except the player's room
for _, room := range startingLevel.Rooms {
if room.X1 != startingRoom.X1 {
mX, mY := room.Center()
//Flip a coin to see what to add...
mobSpawn := GetDiceRoll(2)
if mobSpawn == 1 {
manager.NewEntity().
AddComponent(monster, &Monster{}).
AddComponent(renderable, &Renderable{
Image: orcImg,
}).
AddComponent(position, &Position{
X: mX,
Y: mY,
}).
AddComponent(health, &Health{
MaxHealth: 30,
CurrentHealth: 30,
}).
AddComponent(meleeWeapon, &MeleeWeapon{
Name: "Machete",
MinimumDamage: 4,
MaximumDamage: 8,
ToHitBonus: 1,
}).
AddComponent(armor, &Armor{
Name: "Leather",
Defense: 5,
ArmorClass: 6,
}).
AddComponent(name, &Name{Label: "Orc"}).
AddComponent(userMessage, &UserMessage{
AttackMessage: "",
DeadMessage: "",
GameStateMessage: "",
})
} else {
manager.NewEntity().
AddComponent(monster, &Monster{}).
AddComponent(renderable, &Renderable{
Image: skellyImg,
}).
AddComponent(position, &Position{
X: mX,
Y: mY,
}).
AddComponent(health, &Health{
MaxHealth: 10,
CurrentHealth: 10,
}).
AddComponent(meleeWeapon, &MeleeWeapon{
Name: "Short Sword",
MinimumDamage: 2,
MaximumDamage: 6,
ToHitBonus: 0,
}).
AddComponent(armor, &Armor{
Name: "Bone",
Defense: 3,
ArmorClass: 4,
}).
AddComponent(name, &Name{Label: "Skeleton"}).
AddComponent(userMessage, &UserMessage{
AttackMessage: "",
DeadMessage: "",
GameStateMessage: "",
})
}
}
}
players := ecs.BuildTag(player, position, health, meleeWeapon, armor, name, userMessage)
tags["players"] = players
renderables := ecs.BuildTag(renderable, position)
tags["renderables"] = renderables
monsters := ecs.BuildTag(monster, position, health, meleeWeapon, armor, name, userMessage)
tags["monsters"] = monsters
messengers := ecs.BuildTag(userMessage)
tags["messengers"] = messengers
return manager, tags
}