diff --git a/.gitignore b/.gitignore index 59b1ad7..da23e2c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ p.debug mapmaker testing /bin +/maps diff --git a/cmd/aerpg/main.go b/cmd/aerpg/main.go index 0a34e00..65eb27f 100644 --- a/cmd/aerpg/main.go +++ b/cmd/aerpg/main.go @@ -124,7 +124,7 @@ func run() { //world.Char.Rect = world.Char.Rect.Moved(V(33, 33)) // load world // worldbounds = pixel.R(float64(-4000), float64(-4000), float64(4000), float64(4000)) - cursorsprite := rpg.GetCursor(1) + cursorsprite := common.GetCursor(1) // world generate world := rpg.NewWorld(*flaglevel, *flagenemies, *flagseed) @@ -139,7 +139,7 @@ func run() { animbatch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet) // load loot sprite - goldsheet, err := rpg.LoadPicture("sprites/loot.png") + goldsheet, err := common.LoadPicture("sprites/loot.png") if err != nil { panic("need sprites/loot.png") } diff --git a/cmd/mapmaker/main.go b/cmd/mapmaker/main.go index ee95647..baf0e1e 100644 --- a/cmd/mapmaker/main.go +++ b/cmd/mapmaker/main.go @@ -42,7 +42,7 @@ var ( var helpText = "ENTER=save LEFT=block RIGHT=tile SHIFT=batch SPACE=del CAPS=highlight U=undo R=redo 4=turbo B=dontreplace" func loadSpriteSheet() (pixel.Picture, []*pixel.Sprite) { - spritesheet, err := rpg.LoadPicture("sprites/tileset.png") + spritesheet, err := common.LoadPicture("sprites/tileset.png") /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 * 1 * 2 @@ -124,7 +124,7 @@ func run() { currentThing := 20 // 20 is grass, 0 should be transparent sprite text := rpg.NewTextSmooth(14) fmt.Fprint(text, helpText) - cursor := rpg.GetCursor(2) + cursor := common.GetCursor(2) undobuffer := []common.Object{} var turbo = false var highlight = true diff --git a/librpg/character.go b/librpg/character.go index 0edef0e..4b4c9f9 100644 --- a/librpg/character.go +++ b/librpg/character.go @@ -4,7 +4,6 @@ import ( "fmt" "log" "math" - "math/rand" "strconv" "time" @@ -13,10 +12,6 @@ import ( "github.com/faiface/pixel/text" ) -func init() { - rand.Seed(time.Now().UnixNano()) -} - type Character struct { Phys charPhys // properties Stats Stats diff --git a/librpg/cursor.go b/librpg/common/cursor.go similarity index 94% rename from librpg/cursor.go rename to librpg/common/cursor.go index 67e4557..43a8003 100644 --- a/librpg/cursor.go +++ b/librpg/common/cursor.go @@ -1,4 +1,4 @@ -package rpg +package common import ( "strconv" diff --git a/librpg/common/loadpic.go b/librpg/common/loadpic.go new file mode 100644 index 0000000..ca899f8 --- /dev/null +++ b/librpg/common/loadpic.go @@ -0,0 +1,23 @@ +package common + +import ( + "bytes" + "image" + + "github.com/aerth/rpc/assets" + "github.com/faiface/pixel" +) + +// loadPicture from assets +func LoadPicture(path string) (pixel.Picture, error) { + b, err := assets.Asset(path) + if err != nil { + return nil, err + } + file := bytes.NewReader(b) + img, _, err := image.Decode(file) + if err != nil { + return nil, err + } + return pixel.PictureDataFromImage(img), nil +} diff --git a/librpg/living.go b/librpg/living.go index 6743624..6796459 100644 --- a/librpg/living.go +++ b/librpg/living.go @@ -366,59 +366,6 @@ func (e *Entity) Update(dt float64) { } -// loadCharacterSheet returns an animated spritesheet -// 13W 21H -func LoadEntitySheet(sheetPath string, framesx, framesy uint8) (sheet pixel.Picture, anims map[EntityState]map[Direction][]pixel.Rect, err error) { - sheet, err = LoadPicture(sheetPath) - frameWidth := float64(int(sheet.Bounds().Max.X / float64(framesx))) - frameHeight := float64(int(sheet.Bounds().Max.Y / float64(framesy))) - //log.Println(frameWidth, "width", frameHeight, "height") - // create a array of frames inside the spritesheet - var frames = []pixel.Rect{} - for y := 0.00; y+frameHeight <= sheet.Bounds().Max.Y; y = y + frameHeight { - for x := 0.00; x+float64(frameWidth) <= sheet.Bounds().Max.X; x = x + float64(frameWidth) { - frames = append(frames, pixel.R( - x, - y, - x+frameWidth, - y+frameHeight, - )) - } - } - - //log.Println("total skeleton frames", len(frames)) - - // 0-5 die - // BLANK 6-12 - // 13-25 shoot right - // 26-39 shoot down - // 6-76 shoot left - // 7-25 shoot up - anims = make(map[EntityState]map[Direction][]pixel.Rect) - anims[S_IDLE] = make(map[Direction][]pixel.Rect) - anims[S_WANDER] = make(map[Direction][]pixel.Rect) - anims[S_RUN] = make(map[Direction][]pixel.Rect) - anims[S_GUARD] = make(map[Direction][]pixel.Rect) - anims[S_SUSPECT] = make(map[Direction][]pixel.Rect) - anims[S_HUNT] = make(map[Direction][]pixel.Rect) - anims[S_DEAD] = make(map[Direction][]pixel.Rect) - - // spritesheet is right down left up - anims[S_DEAD][LEFT] = frames[0:5] - anims[S_DEAD][RIGHT] = frames[0:5] - anims[S_DEAD][UP] = frames[0:5] - anims[S_DEAD][DOWN] = frames[0:5] - anims[S_IDLE][LEFT] = frames[143:144] - anims[S_IDLE][UP] = frames[156:157] - anims[S_IDLE][RIGHT] = frames[169:170] - anims[S_IDLE][DOWN] = frames[182:183] - anims[S_RUN][LEFT] = frames[143:152] - anims[S_RUN][UP] = frames[156:165] - anims[S_RUN][RIGHT] = frames[169:178] - anims[S_RUN][DOWN] = frames[182:191] - return sheet, anims, nil -} - func (w *World) NewMobs(n int) { if w.Settings.NumEnemy == 0 { w.Settings.NumEnemy = n diff --git a/librpg/load.go b/librpg/load.go index 9d55710..391b272 100644 --- a/librpg/load.go +++ b/librpg/load.go @@ -3,14 +3,12 @@ package rpg import ( - "bytes" - "image" "math/rand" "time" _ "image/png" - "github.com/aerth/rpg/assets" + "github.com/aerth/rpc/librpg/common" "github.com/faiface/pixel" ) @@ -18,23 +16,62 @@ func init() { rand.Seed(time.Now().UnixNano()) } -// loadPicture from assets -func LoadPicture(path string) (pixel.Picture, error) { - b, err := assets.Asset(path) - if err != nil { - return nil, err - } - file := bytes.NewReader(b) - img, _, err := image.Decode(file) - if err != nil { - return nil, err +// loadCharacterSheet returns an animated spritesheet +// 13W 21H +func LoadEntitySheet(sheetPath string, framesx, framesy uint8) (sheet pixel.Picture, anims map[EntityState]map[Direction][]pixel.Rect, err error) { + sheet, err = common.LoadPicture(sheetPath) + frameWidth := float64(int(sheet.Bounds().Max.X / float64(framesx))) + frameHeight := float64(int(sheet.Bounds().Max.Y / float64(framesy))) + //log.Println(frameWidth, "width", frameHeight, "height") + // create a array of frames inside the spritesheet + var frames = []pixel.Rect{} + for y := 0.00; y+frameHeight <= sheet.Bounds().Max.Y; y = y + frameHeight { + for x := 0.00; x+float64(frameWidth) <= sheet.Bounds().Max.X; x = x + float64(frameWidth) { + frames = append(frames, pixel.R( + x, + y, + x+frameWidth, + y+frameHeight, + )) + } } - return pixel.PictureDataFromImage(img), nil + + //log.Println("total skeleton frames", len(frames)) + + // 0-5 die + // BLANK 6-12 + // 13-25 shoot right + // 26-39 shoot down + // 6-76 shoot left + // 7-25 shoot up + anims = make(map[EntityState]map[Direction][]pixel.Rect) + anims[S_IDLE] = make(map[Direction][]pixel.Rect) + anims[S_WANDER] = make(map[Direction][]pixel.Rect) + anims[S_RUN] = make(map[Direction][]pixel.Rect) + anims[S_GUARD] = make(map[Direction][]pixel.Rect) + anims[S_SUSPECT] = make(map[Direction][]pixel.Rect) + anims[S_HUNT] = make(map[Direction][]pixel.Rect) + anims[S_DEAD] = make(map[Direction][]pixel.Rect) + + // spritesheet is right down left up + anims[S_DEAD][LEFT] = frames[0:5] + anims[S_DEAD][RIGHT] = frames[0:5] + anims[S_DEAD][UP] = frames[0:5] + anims[S_DEAD][DOWN] = frames[0:5] + anims[S_IDLE][LEFT] = frames[143:144] + anims[S_IDLE][UP] = frames[156:157] + anims[S_IDLE][RIGHT] = frames[169:170] + anims[S_IDLE][DOWN] = frames[182:183] + anims[S_RUN][LEFT] = frames[143:152] + anims[S_RUN][UP] = frames[156:165] + anims[S_RUN][RIGHT] = frames[169:178] + anims[S_RUN][DOWN] = frames[182:191] + return sheet, anims, nil } // loadCharacterSheet returns an animated spritesheet func LoadCharacterSheet(sheetPath string, numframes uint8) (sheet pixel.Picture, anims map[Direction][]pixel.Rect, err error) { - sheet, err = LoadPicture("sprites/char.png") + sheet, err = common.LoadPicture("sprites/char.png") if err != nil { panic(err) } @@ -69,7 +106,7 @@ func LoadCharacterSheet(sheetPath string, numframes uint8) (sheet pixel.Picture, func LoadNewCharacterSheet(sheetPath string) (sheet pixel.Picture, anims map[Direction][]pixel.Rect, err error) { numframes := 16.00 - sheet, err = LoadPicture(sheetPath) + sheet, err = common.LoadPicture(sheetPath) if err != nil { panic(err) } @@ -99,3 +136,29 @@ func LoadNewCharacterSheet(sheetPath string) (sheet pixel.Picture, anims map[Dir return sheet, anims, nil } + +func LoadSpriteSheet(path string) (pixel.Picture, []*pixel.Sprite) { + spritesheet, err := common.LoadPicture("sprites/" + path) + /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + * 1 + * 2 + * ... + * 16 + */ + if err != nil { + panic(err) + } + var sheetFrames []pixel.Rect + for x := spritesheet.Bounds().Min.X; x < spritesheet.Bounds().Max.X; x += 32 { + for y := spritesheet.Bounds().Min.Y; y < spritesheet.Bounds().Max.Y; y += 32 { + sheetFrames = append(sheetFrames, pixel.R(x, y, x+32, y+32)) + } + } + var spritemap = []*pixel.Sprite{} + for i := 0; i < len(sheetFrames); i++ { + x := i + spritemap = append(spritemap, pixel.NewSprite(spritesheet, sheetFrames[x])) + } + //log.Println(len(spritemap), "sprites loaded") + return spritesheet, spritemap +} diff --git a/librpg/effects.go b/librpg/statuseffects.go similarity index 100% rename from librpg/effects.go rename to librpg/statuseffects.go diff --git a/librpg/util.go b/librpg/util.go index aadb9ee..75f6361 100644 --- a/librpg/util.go +++ b/librpg/util.go @@ -162,31 +162,6 @@ func LoadTTF(path string, size float64) (font.Face, error) { GlyphCacheEntries: 1, }), nil } -func LoadSpriteSheet(path string) (pixel.Picture, []*pixel.Sprite) { - spritesheet, err := LoadPicture("sprites/" + path) - /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 - * 1 - * 2 - * ... - * 16 - */ - if err != nil { - panic(err) - } - var sheetFrames []pixel.Rect - for x := spritesheet.Bounds().Min.X; x < spritesheet.Bounds().Max.X; x += 32 { - for y := spritesheet.Bounds().Min.Y; y < spritesheet.Bounds().Max.Y; y += 32 { - sheetFrames = append(sheetFrames, pixel.R(x, y, x+32, y+32)) - } - } - var spritemap = []*pixel.Sprite{} - for i := 0; i < len(sheetFrames); i++ { - x := i - spritemap = append(spritemap, pixel.NewSprite(spritesheet, sheetFrames[x])) - } - //log.Println(len(spritemap), "sprites loaded") - return spritesheet, spritemap -} // Distance between two vectors func Distance(v1, v2 pixel.Vec) float64 {