This repository has been archived by the owner on Oct 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonsterid.go
83 lines (75 loc) · 1.57 KB
/
monsterid.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
package monsterid // import "src.techknowlogick.com/monster-id"
import (
"bytes"
"crypto/sha512"
"encoding/binary"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"math/rand"
)
var (
legs = 5
hair = 5
arms = 5
body = 15
eyes = 15
mouth = 10
)
var bodyParts = []string{"legs", "hair", "arms", "body", "eyes", "mouth"}
type monsterid struct {
legs int
hair int
arms int
body int
eyes int
mouth int
}
func New(seed []byte) image.Image {
buf := sha512.Sum512(seed)
seed_int := binary.BigEndian.Uint64(buf[56:])
rand.Seed(int64(seed_int))
mid := &monsterid{}
mid.legs = rand.Intn(legs) + 1
mid.hair = rand.Intn(hair) + 1
mid.arms = rand.Intn(arms) + 1
mid.body = rand.Intn(body) + 1
mid.eyes = rand.Intn(eyes) + 1
mid.mouth = rand.Intn(mouth) + 1
img := image.NewRGBA(image.Rect(0, 0, 120, 120))
white := color.RGBA{255, 255, 255, 255}
draw.Draw(img, img.Bounds(), &image.Uniform{white}, image.ZP, draw.Src)
for _, part := range bodyParts {
fileName := fmt.Sprintf("parts/%s_%d.png", part, getPartNumber(mid, part))
asset, err := Asset(fileName)
if err != nil {
log.Fatal(err)
}
assetFull, err := png.Decode(bytes.NewReader(asset))
if err != nil {
log.Fatal(err)
}
draw.Draw(img, img.Bounds(), assetFull, image.ZP, draw.Over)
}
return img
}
func getPartNumber(mid *monsterid, part string) int {
switch part {
case "legs":
return mid.legs
case "hair":
return mid.hair
case "arms":
return mid.arms
case "body":
return mid.body
case "eyes":
return mid.eyes
case "mouth":
return mid.mouth
}
return 0
}