-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsingleton.go
More file actions
153 lines (134 loc) · 3.81 KB
/
singleton.go
File metadata and controls
153 lines (134 loc) · 3.81 KB
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
package kar
import (
"image"
"image/color"
"log"
"math"
"math/rand/v2"
"time"
"github.com/setanarut/kar/items"
"github.com/setanarut/kar/tilemap"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/colorm"
"github.com/hajimehoshi/ebiten/v2/text/v2"
arkserde "github.com/mlange-42/ark-serde"
"github.com/mlange-42/ark/ecs"
"github.com/quasilyte/gdata"
"github.com/setanarut/kamera/v2"
"github.com/setanarut/v"
)
type Vec = v.Vec
const (
SnowballGravity float64 = 0.5
SnowballSpeedX float64 = 3.5
SnowballMaxFallVelocity float64 = 2.5
SnowballBounceHeight float64 = 9.0
ItemGravity float64 = 3.0
PlayerBestToolDamage float64 = 5.0
PlayerDefaultDamage float64 = 1.0
RaycastDist int = 4 // block unit
Tick time.Duration = time.Second / 60
ItemCollisionDelay time.Duration = time.Second / 2
)
// MOB ID
const (
CrabID MobileID = 1
)
var debugEnabled bool = false
var (
Screen *ebiten.Image
ScreenSize = Vec{500, 340}
WindowScale = 2.0
)
var (
currentGameState = "mainmenu"
)
var (
ceilBlockCoord image.Point
ceilBlockTick float64
dropItemAABB = &AABB{Half: Vec{4, 4}}
)
var (
world ecs.World = ecs.NewWorld(100)
currentPlayer ecs.Entity
renderArea = image.Point{(int(ScreenSize.X) / 20) + 3, (int(ScreenSize.Y) / 20) + 3}
dataManager *gdata.Manager
sinspaceOffsets []float64 = sinspace(0, 2*math.Pi, 3, 60)
backgroundColor color.RGBA = color.RGBA{36, 36, 39, 255}
gameTileMapGenerator tilemap.Generator
colorMDIO *colorm.DrawImageOptions = &colorm.DrawImageOptions{}
colorM colorm.ColorM = colorm.ColorM{}
textDO *text.DrawOptions = &text.DrawOptions{
DrawImageOptions: ebiten.DrawImageOptions{},
LayoutOptions: text.LayoutOptions{
LineSpacing: 10,
},
}
tileCollider = Collider{
TileMap: tileMapRes.Grid,
TileSize: image.Point{tileMapRes.TileW, tileMapRes.TileH},
}
)
func init() {
var err error
dataManager, err = gdata.Open(gdata.Config{AppName: "kar"})
if err != nil {
panic(err)
}
gameTileMapGenerator = tilemap.NewGenerator(tileMapRes)
}
func NewGame() {
world.Reset()
inventoryRes.Reset()
gameDataRes = gameData{}
animPlayer.Data = animDefaultPlaybackData
mapResInventory.Add(inventoryRes)
mapResCraftingtable.Add(craftingTableRes)
mapResCamera.Add(cameraRes)
mapResGameData.Add(&gameDataRes)
mapResAnimPlaybackData.Add(&animPlayer.Data)
mapResTilemap.Add(&tileMapRes)
gameTileMapGenerator.SetSeed(rand.Int())
gameTileMapGenerator.Generate()
spawnCoord := tileMapRes.FindSpawnPosition()
SpawnPos := tileMapRes.TileToWorld(spawnCoord)
currentPlayer = SpawnPlayer(SpawnPos)
box := mapAABB.Get(currentPlayer)
box.SetBottom(tileMapRes.GetTileBottom(spawnCoord.X, spawnCoord.Y))
cameraRes.SmoothType = kamera.SmoothDamp
cameraRes.SetCenter(box.Pos.X, box.Pos.Y)
inventoryRes.SetSlot(8, items.Snowball, 64, 0)
}
func SaveGame() {
jsonData, err := arkserde.Serialize(&world, arkserde.Opts.Compress())
if err != nil {
log.Fatal(err)
}
dataManager.SaveItem("01save", jsonData)
}
func LoadGame() {
if dataManager.ItemExists("01save") {
world.Reset()
mapResInventory.Add(inventoryRes)
mapResCraftingtable.Add(craftingTableRes)
mapResCamera.Add(cameraRes)
mapResGameData.Add(&gameDataRes)
mapResAnimPlaybackData.Add(&animPlayer.Data)
mapResTilemap.Add(&tileMapRes)
jsonData, err := dataManager.LoadItem("01save")
if err != nil {
log.Fatal(err)
}
err = arkserde.Deserialize(jsonData, &world, arkserde.Opts.Compress())
if err != nil {
log.Fatal(err)
}
if !world.Alive(currentPlayer) {
q := filterPlayer.Query()
q.Next()
currentPlayer = q.Entity()
q.Close()
}
animPlayer.Update()
}
}