2021-10-05 03:47:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-10-08 04:00:33 +00:00
|
|
|
"math"
|
2021-10-05 03:47:29 +00:00
|
|
|
"math/rand"
|
2021-10-30 22:25:11 +00:00
|
|
|
"time"
|
2021-10-22 01:07:26 +00:00
|
|
|
|
|
|
|
"github.com/Meshiest/go-dungeon/dungeon"
|
2021-10-05 03:47:29 +00:00
|
|
|
)
|
|
|
|
|
2021-10-22 01:07:26 +00:00
|
|
|
const dungeonScale = 4
|
|
|
|
|
2021-10-05 03:47:29 +00:00
|
|
|
// Level represents a game level.
|
|
|
|
type Level struct {
|
2021-10-31 18:30:42 +00:00
|
|
|
num int
|
|
|
|
|
2021-10-05 03:47:29 +00:00
|
|
|
w, h int
|
|
|
|
|
|
|
|
tiles [][]*Tile // (Y,X) array of tiles
|
|
|
|
tileSize int
|
2021-10-12 03:22:13 +00:00
|
|
|
|
|
|
|
items []*gameItem
|
|
|
|
|
2021-10-20 01:46:11 +00:00
|
|
|
creeps []*gameCreep
|
|
|
|
liveCreeps int
|
2021-10-12 03:22:13 +00:00
|
|
|
|
|
|
|
player *gamePlayer
|
2021-10-27 02:21:26 +00:00
|
|
|
|
|
|
|
torches []*gameCreep
|
2021-10-29 15:49:27 +00:00
|
|
|
|
|
|
|
enterX, enterY int
|
|
|
|
exitX, exitY int
|
|
|
|
|
2021-10-30 22:25:11 +00:00
|
|
|
exitOpenTime time.Time
|
2021-10-29 15:49:27 +00:00
|
|
|
|
|
|
|
requiredSouls int
|
2021-10-05 03:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tile returns the tile at the provided coordinates, or nil.
|
|
|
|
func (l *Level) Tile(x, y int) *Tile {
|
|
|
|
if x >= 0 && y >= 0 && x < l.w && y < l.h {
|
|
|
|
return l.tiles[y][x]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of the Level.
|
|
|
|
func (l *Level) Size() (width, height int) {
|
|
|
|
return l.w, l.h
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:11:07 +00:00
|
|
|
func (l *Level) isFloor(x float64, y float64) bool {
|
|
|
|
t := l.Tile(int(math.Floor(x+.5)), int(math.Floor(y+.5)))
|
2021-10-22 01:07:26 +00:00
|
|
|
if t == nil {
|
|
|
|
return false
|
2021-10-06 14:18:38 +00:00
|
|
|
}
|
2021-10-22 01:07:26 +00:00
|
|
|
if !t.floor {
|
|
|
|
return false
|
2021-10-06 14:18:38 +00:00
|
|
|
}
|
2021-10-27 03:11:07 +00:00
|
|
|
return true
|
2021-10-06 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 03:22:13 +00:00
|
|
|
func (l *Level) newSpawnLocation() (float64, float64) {
|
|
|
|
SPAWNLOCATION:
|
|
|
|
for {
|
2021-10-20 01:46:11 +00:00
|
|
|
x := float64(1 + rand.Intn(l.w-2))
|
|
|
|
y := float64(1 + rand.Intn(l.h-2))
|
2021-10-12 03:22:13 +00:00
|
|
|
|
2021-10-27 03:11:07 +00:00
|
|
|
if !l.isFloor(x, y) {
|
2021-10-22 01:07:26 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-12 03:22:13 +00:00
|
|
|
// Too close to player.
|
2021-10-20 01:46:11 +00:00
|
|
|
playerSafeSpace := 18.0
|
2021-10-12 03:22:13 +00:00
|
|
|
dx, dy := deltaXY(x, y, l.player.x, l.player.y)
|
|
|
|
if dx <= playerSafeSpace && dy <= playerSafeSpace {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-20 01:46:11 +00:00
|
|
|
// Too close to garlic or holy water.
|
2021-10-12 03:22:13 +00:00
|
|
|
garlicSafeSpace := 2.0
|
|
|
|
for _, item := range l.items {
|
|
|
|
if item.health == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dx, dy = deltaXY(x, y, item.x, item.y)
|
|
|
|
if dx <= garlicSafeSpace && dy <= garlicSafeSpace {
|
|
|
|
continue SPAWNLOCATION
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return x, y
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-05 03:47:29 +00:00
|
|
|
// NewLevel returns a new randomly generated Level.
|
2021-10-27 02:21:26 +00:00
|
|
|
func NewLevel(levelNum int, p *gamePlayer) (*Level, error) {
|
2021-10-22 08:31:30 +00:00
|
|
|
multiplier := levelNum
|
|
|
|
if multiplier > 2 {
|
|
|
|
multiplier = 2
|
|
|
|
}
|
2021-10-05 03:47:29 +00:00
|
|
|
l := &Level{
|
2021-10-31 18:30:42 +00:00
|
|
|
num: levelNum,
|
2021-10-22 08:31:30 +00:00
|
|
|
w: 336 * multiplier,
|
|
|
|
h: 336 * multiplier,
|
2021-10-05 03:47:29 +00:00
|
|
|
tileSize: 32,
|
2021-10-27 02:21:26 +00:00
|
|
|
player: p,
|
2021-10-05 03:47:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 15:49:27 +00:00
|
|
|
l.requiredSouls = 66
|
|
|
|
if levelNum == 2 {
|
|
|
|
l.requiredSouls = 666
|
|
|
|
} else if levelNum == 3 {
|
|
|
|
l.requiredSouls = 6666
|
|
|
|
}
|
|
|
|
|
2021-10-27 02:21:26 +00:00
|
|
|
var err error
|
|
|
|
sandstoneSS, err = LoadEnvironmentSpriteSheet()
|
2021-10-05 03:47:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load embedded spritesheet: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-10-22 08:31:30 +00:00
|
|
|
rooms := 33
|
|
|
|
if multiplier == 2 {
|
|
|
|
rooms = 66
|
|
|
|
}
|
|
|
|
d := dungeon.NewDungeon(l.w/dungeonScale, rooms)
|
2021-10-22 01:07:26 +00:00
|
|
|
dungeonFloor := 1
|
2021-10-05 03:47:29 +00:00
|
|
|
l.tiles = make([][]*Tile, l.h)
|
|
|
|
for y := 0; y < l.h; y++ {
|
|
|
|
l.tiles[y] = make([]*Tile, l.w)
|
|
|
|
for x := 0; x < l.w; x++ {
|
|
|
|
t := &Tile{}
|
2021-10-22 08:31:30 +00:00
|
|
|
if y < l.h-1 && d.Grid[x/dungeonScale][y/dungeonScale] == dungeonFloor {
|
2021-10-22 01:07:26 +00:00
|
|
|
if rand.Intn(13) == 0 {
|
|
|
|
t.AddSprite(sandstoneSS.FloorC)
|
2021-10-05 03:47:29 +00:00
|
|
|
} else {
|
2021-10-22 01:07:26 +00:00
|
|
|
t.AddSprite(sandstoneSS.FloorA)
|
2021-10-05 03:47:29 +00:00
|
|
|
}
|
2021-10-22 01:07:26 +00:00
|
|
|
t.floor = true
|
2021-10-05 03:47:29 +00:00
|
|
|
}
|
|
|
|
l.tiles[y][x] = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 01:07:26 +00:00
|
|
|
neighbors := func(x, y int) [][2]int {
|
|
|
|
return [][2]int{
|
|
|
|
{x - 1, y - 1},
|
|
|
|
{x, y - 1},
|
|
|
|
{x + 1, y - 1},
|
|
|
|
{x + 1, y},
|
|
|
|
{x + 1, y + 1},
|
|
|
|
{x, y + 1},
|
|
|
|
{x - 1, y + 1},
|
|
|
|
{x - 1, y},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
floorTile := func(x, y int) bool {
|
|
|
|
t := l.Tile(x, y)
|
|
|
|
if t == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return t.floor
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:49:27 +00:00
|
|
|
var topWalls [][2]int
|
|
|
|
|
2021-10-22 01:07:26 +00:00
|
|
|
// Add walls.
|
|
|
|
for x := 0; x < l.w; x++ {
|
|
|
|
for y := 0; y < l.h; y++ {
|
|
|
|
t := l.Tile(x, y)
|
|
|
|
if t == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !t.floor {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, n := range neighbors(x, y) {
|
|
|
|
nx, ny := n[0], n[1]
|
|
|
|
neighbor := l.Tile(nx, ny)
|
|
|
|
if neighbor == nil || neighbor.floor || neighbor.wall {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
neighbor.wall = true
|
|
|
|
|
|
|
|
// From perspective of neighbor tile.
|
|
|
|
bottom := floorTile(nx, ny+1)
|
|
|
|
top := floorTile(nx, ny-1)
|
|
|
|
right := floorTile(nx+1, ny)
|
|
|
|
left := floorTile(nx-1, ny)
|
|
|
|
topLeft := floorTile(nx-1, ny-1)
|
|
|
|
topRight := floorTile(nx+1, ny-1)
|
|
|
|
bottomLeft := floorTile(nx-1, ny+1)
|
|
|
|
bottomRight := floorTile(nx+1, ny+1)
|
|
|
|
|
|
|
|
// Determine which wall sprite to belongs here.
|
|
|
|
spriteTop := !top && bottom
|
|
|
|
spriteLeft := (left || bottomLeft) && !right && !bottomRight && !bottom
|
|
|
|
spriteRight := (right || bottomRight) && !left && !bottomLeft && !bottom
|
|
|
|
spriteBottomRight := !topLeft && !top && topRight && !bottomLeft && !bottom && !bottomRight
|
|
|
|
spriteBottomLeft := topLeft && !top && !topRight && !bottomLeft && !bottom && !bottomRight
|
|
|
|
spriteBottom := top && !bottom
|
|
|
|
|
|
|
|
// Add wall sprite.
|
|
|
|
switch {
|
|
|
|
case spriteTop:
|
|
|
|
if !bottomLeft || !bottomRight || left || right {
|
|
|
|
neighbor.AddSprite(sandstoneSS.WallPillar)
|
2021-10-27 02:21:26 +00:00
|
|
|
c := newCreep(TypeTorch, l, l.player)
|
|
|
|
c.x, c.y = float64(nx), float64(ny)
|
|
|
|
l.creeps = append(l.creeps, c)
|
|
|
|
l.torches = append(l.torches, c)
|
2021-10-22 01:07:26 +00:00
|
|
|
} else {
|
|
|
|
neighbor.AddSprite(sandstoneSS.WallTop)
|
2021-10-29 15:49:27 +00:00
|
|
|
topWalls = append(topWalls, [2]int{nx, ny})
|
2021-10-22 01:07:26 +00:00
|
|
|
}
|
|
|
|
case spriteLeft:
|
|
|
|
if spriteBottom {
|
|
|
|
neighbor.AddSprite(sandstoneSS.WallBottom)
|
|
|
|
}
|
|
|
|
neighbor.AddSprite(sandstoneSS.WallLeft)
|
|
|
|
case spriteRight:
|
|
|
|
if spriteBottom {
|
|
|
|
neighbor.AddSprite(sandstoneSS.WallBottom)
|
|
|
|
}
|
|
|
|
neighbor.AddSprite(sandstoneSS.WallRight)
|
|
|
|
case spriteBottomLeft:
|
|
|
|
neighbor.AddSprite(sandstoneSS.WallBottomLeft)
|
|
|
|
case spriteBottomRight:
|
|
|
|
neighbor.AddSprite(sandstoneSS.WallBottomRight)
|
|
|
|
case spriteBottom:
|
|
|
|
neighbor.AddSprite(sandstoneSS.WallBottom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:49:27 +00:00
|
|
|
entrance := topWalls[rand.Intn(len(topWalls))]
|
|
|
|
exit := entrance
|
|
|
|
for exit == entrance {
|
|
|
|
exit = topWalls[rand.Intn(len(topWalls))]
|
|
|
|
}
|
|
|
|
|
|
|
|
l.enterX, l.enterY = entrance[0], entrance[1]
|
|
|
|
l.exitX, l.exitY = exit[0], exit[1]
|
|
|
|
|
|
|
|
if levelNum > 1 {
|
|
|
|
l.Tile(l.enterX, l.enterY).sprites = nil
|
|
|
|
l.Tile(l.enterX, l.enterY).AddSprite(sandstoneSS.FloorA)
|
|
|
|
l.Tile(l.enterX, l.enterY).AddSprite(sandstoneSS.DoorClosed)
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Tile(l.exitX, l.exitY).sprites = nil
|
|
|
|
l.Tile(l.exitX, l.exitY).AddSprite(sandstoneSS.FloorA)
|
|
|
|
l.Tile(l.exitX, l.exitY).AddSprite(sandstoneSS.DoorClosed)
|
|
|
|
|
|
|
|
// TODO special door for final exit
|
|
|
|
|
2021-10-27 02:21:26 +00:00
|
|
|
l.bakeLightmap()
|
|
|
|
|
2021-10-05 03:47:29 +00:00
|
|
|
return l, nil
|
|
|
|
}
|
2021-10-08 04:00:33 +00:00
|
|
|
|
2021-10-27 02:21:26 +00:00
|
|
|
func (l *Level) bakeLightmap() {
|
|
|
|
for x := 0; x < l.w; x++ {
|
|
|
|
for y := 0; y < l.h; y++ {
|
|
|
|
t := l.tiles[y][x]
|
|
|
|
v := 0.0
|
|
|
|
for _, torch := range l.torches {
|
|
|
|
if torch.health == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
torchV := colorScaleValue(float64(x), float64(y), torch.x, torch.y)
|
|
|
|
v += torchV
|
|
|
|
}
|
|
|
|
t.colorScale = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Level) bakePartialLightmap(lx, ly int) {
|
|
|
|
radius := 16
|
|
|
|
for x := lx - radius; x < lx+radius; x++ {
|
|
|
|
for y := ly - radius; y < ly+radius; y++ {
|
|
|
|
t := l.Tile(x, y)
|
|
|
|
if t == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v := 0.0
|
|
|
|
for _, torch := range l.torches {
|
|
|
|
if torch.health == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
torchV := colorScaleValue(float64(x), float64(y), torch.x, torch.y)
|
|
|
|
v += torchV
|
|
|
|
}
|
|
|
|
t.colorScale = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:49:27 +00:00
|
|
|
func (l *Level) addCreep(creepType int) *gameCreep {
|
2021-10-27 02:21:26 +00:00
|
|
|
c := newCreep(creepType, l, l.player)
|
|
|
|
l.creeps = append(l.creeps, c)
|
2021-10-29 15:49:27 +00:00
|
|
|
return c
|
2021-10-27 02:21:26 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 04:00:33 +00:00
|
|
|
func angle(x1, y1, x2, y2 float64) float64 {
|
|
|
|
return math.Atan2(y1-y2, x1-x2)
|
|
|
|
}
|
2021-10-27 02:21:26 +00:00
|
|
|
|
|
|
|
func colorScaleValue(x, y, bx, by float64) float64 {
|
|
|
|
dx, dy := deltaXY(x, y, bx, by)
|
|
|
|
sD := 7 / (dx + dy)
|
|
|
|
if sD > 1 {
|
|
|
|
sD = 1
|
|
|
|
}
|
|
|
|
sDB := sD
|
|
|
|
if dx > 4 {
|
|
|
|
sDB *= 0.6 / (dx / 4)
|
|
|
|
}
|
|
|
|
if dy > 4 {
|
|
|
|
sDB *= 0.6 / (dy / 4)
|
|
|
|
}
|
|
|
|
sD = sD * 2 * sDB
|
|
|
|
if sD > 1 {
|
|
|
|
sD = 1
|
|
|
|
}
|
|
|
|
return sD
|
|
|
|
}
|