Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
aerth committed Jul 9, 2022
1 parent f2178fe commit b5591ec
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 104 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ p.debug
mapmaker
testing
/bin
/maps
4 changes: 2 additions & 2 deletions cmd/aerpg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/mapmaker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions librpg/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"
"math"
"math/rand"
"strconv"
"time"

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion librpg/cursor.go → librpg/common/cursor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rpg
package common

import (
"strconv"
Expand Down
23 changes: 23 additions & 0 deletions librpg/common/loadpic.go
Original file line number Diff line number Diff line change
@@ -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
}
53 changes: 0 additions & 53 deletions librpg/living.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
95 changes: 79 additions & 16 deletions librpg/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,75 @@
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"
)

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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
File renamed without changes.
25 changes: 0 additions & 25 deletions librpg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b5591ec

Please sign in to comment.