-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnewworld.go
117 lines (106 loc) · 2.5 KB
/
newworld.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
package rpg
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"golang.org/x/image/colornames"
"github.com/aerth/rpc/librpg/common"
"github.com/aerth/rpg/assets"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
func NewGame(win *pixelgl.Window, difficulty int, leveltest string, worldseed string) {
world := NewWorld(leveltest, difficulty, worldseed)
if world == nil {
log.Println("bad world")
os.Exit(111)
}
// have window, have world
if err := world.drawTiles("tileset.png"); err != nil {
log.Println("bad tiles", err)
os.Exit(111)
}
if TitleMenu(win) {
os.Exit(0)
}
var camPos = pixel.V(0, 0)
for !win.Closed() {
if win.Pressed(pixelgl.KeyLeft) {
camPos.X--
log.Println(camPos)
}
if win.Pressed(pixelgl.KeyRight) {
camPos.X++
log.Println(camPos)
}
if win.Pressed(pixelgl.KeyUp) {
camPos.Y++
log.Println(camPos)
}
if win.Pressed(pixelgl.KeyDown) {
camPos.Y--
log.Println(camPos)
}
win.SetMatrix(pixel.IM.Moved(camPos))
win.Clear(colornames.Green)
world.Draw(win)
win.Update()
}
os.Exit(111)
}
// LoadMapFile from disk
func (w *World) LoadMapFile(path string) error {
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return w.loadmap(b)
}
// LoadMap from embedded assets
func (w *World) LoadMap(path string) error {
b, err := assets.Asset(path)
if err != nil {
return err
}
return w.loadmap(b)
}
func (w *World) loadmap(b []byte) error {
var things = []common.Object{}
err := json.Unmarshal(b, &things)
if err != nil {
return fmt.Errorf("invalid map: %v", err)
}
return w.injectMap(things)
}
func (w *World) InjectMap(things []common.Object) error {
return w.injectMap(things)
}
func (w *World) injectMap(things []common.Object) error {
total := len(things)
for i, t := range things {
t.W = w
t.Rect = common.DefaultSpriteRectangle.Moved(t.Loc)
switch t.SpriteNum {
case 53: // water
t.Type = common.O_BLOCK
default:
}
switch t.Type {
case common.O_BLOCK:
//log.Printf("%v/%v block object: %s %v %s", i, total, t.Loc, t.SpriteNum, t.Type)
w.Blocks = append(w.Blocks, t)
case common.O_TILE:
//log.Printf("%v/%v tile object: %s %v %s", i, total, t.Loc, t.SpriteNum, t.Type)
w.Tiles = append(w.Tiles, t)
default: //
log.Printf("%v/%v skipping bad object: %s %v %s", i, total, t.Loc, t.SpriteNum, t.Type)
}
}
log.Printf("map has %v blocks, %v tiles", len(w.Blocks), len(w.Tiles))
if len(w.Blocks) == 0 && len(w.Tiles) == 0 {
return fmt.Errorf("invalid map")
}
return nil
}