|
|
@ -24,70 +24,74 @@ const ( |
|
|
|
HEARING_DISTANCE = 6 |
|
|
|
HEARING_DISTANCE = 6 |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DATA
|
|
|
|
|
|
|
|
|
|
|
|
type Map [][]rune |
|
|
|
type Map [][]rune |
|
|
|
type Paths [][]int |
|
|
|
type Paths [][]int |
|
|
|
|
|
|
|
|
|
|
|
type Position struct { |
|
|
|
type Position struct { |
|
|
|
x int |
|
|
|
X int |
|
|
|
y int |
|
|
|
Y int |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type Enemy struct { |
|
|
|
type Enemy struct { |
|
|
|
hp int |
|
|
|
HP int |
|
|
|
pos Position |
|
|
|
Pos Position |
|
|
|
damage int |
|
|
|
Damage int |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type Game struct { |
|
|
|
type Game struct { |
|
|
|
screen tcell.Screen |
|
|
|
Screen tcell.Screen |
|
|
|
level Map |
|
|
|
Level Map |
|
|
|
paths Paths |
|
|
|
Paths Paths |
|
|
|
player Enemy |
|
|
|
Player Enemy |
|
|
|
status string |
|
|
|
Status string |
|
|
|
width int |
|
|
|
Width int |
|
|
|
height int |
|
|
|
Height int |
|
|
|
enemies map[Position]*Enemy |
|
|
|
Enemies map[Position]*Enemy |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//// DRAWING
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) DrawText(x int, y int, text string) { |
|
|
|
func (game *Game) DrawText(x int, y int, text string) { |
|
|
|
for i, cell := range text { |
|
|
|
for i, cell := range text { |
|
|
|
game.screen.SetContent(x+i, y, cell, nil, tcell.StyleDefault) |
|
|
|
game.Screen.SetContent(x+i, y, cell, nil, tcell.StyleDefault) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) DrawStatus() { |
|
|
|
func (game *Game) DrawStatus() { |
|
|
|
game.DrawText(0, game.height, game.status) |
|
|
|
game.DrawText(0, game.Height, game.Status) |
|
|
|
|
|
|
|
|
|
|
|
hp := fmt.Sprintf("HP: %d", game.player.hp) |
|
|
|
hp := fmt.Sprintf("HP: %d", game.Player.HP) |
|
|
|
|
|
|
|
|
|
|
|
game.DrawText(game.width - len(hp), game.height, hp) |
|
|
|
game.DrawText(game.Width - len(hp), game.Height, hp) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Status(msg string) { |
|
|
|
func (game *Game) SetStatus(msg string) { |
|
|
|
game.status = msg |
|
|
|
game.Status = msg |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) DrawEntity(symbol rune, pos Position, color tcell.Color) { |
|
|
|
func (game *Game) DrawEntity(symbol rune, pos Position, color tcell.Color) { |
|
|
|
style := tcell.StyleDefault.Bold(true).Foreground(color) |
|
|
|
style := tcell.StyleDefault.Bold(true).Foreground(color) |
|
|
|
game.screen.SetContent(pos.x, pos.y, symbol, nil, style) |
|
|
|
game.Screen.SetContent(pos.X, pos.Y, symbol, nil, style) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) DrawMap() { |
|
|
|
func (game *Game) DrawMap() { |
|
|
|
gray := tcell.StyleDefault.Foreground(tcell.ColorGray) |
|
|
|
gray := tcell.StyleDefault.Foreground(tcell.ColorGray) |
|
|
|
|
|
|
|
|
|
|
|
for y, line := range game.level { |
|
|
|
for y, line := range game.Level { |
|
|
|
for x, cell := range line { |
|
|
|
for x, cell := range line { |
|
|
|
if cell == SPACE { |
|
|
|
if cell == SPACE { |
|
|
|
game.screen.SetContent(x, y, cell, nil, gray) |
|
|
|
game.Screen.SetContent(x, y, cell, nil, gray) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
game.screen.SetContent(x, y, cell, nil, tcell.StyleDefault) |
|
|
|
game.Screen.SetContent(x, y, cell, nil, tcell.StyleDefault) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) DrawPaths() { |
|
|
|
func (game *Game) DrawPaths() { |
|
|
|
for y, row := range game.paths { |
|
|
|
for y, row := range game.Paths { |
|
|
|
for x, path_num := range row { |
|
|
|
for x, path_num := range row { |
|
|
|
if path_num == PATH_LIMIT { continue } |
|
|
|
if path_num == PATH_LIMIT { continue } |
|
|
|
|
|
|
|
|
|
|
@ -98,15 +102,28 @@ func (game *Game) DrawPaths() { |
|
|
|
style = style.Reverse(true) |
|
|
|
style = style.Reverse(true) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
game.screen.SetContent(x, y, rune(as_str[0]), nil, style) |
|
|
|
game.Screen.SetContent(x, y, rune(as_str[0]), nil, style) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///// RENDERING
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) InitScreen() { |
|
|
|
|
|
|
|
encoding.Register() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
game.Screen, err = tcell.NewScreen() |
|
|
|
|
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = game.Screen.Init() |
|
|
|
|
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Render() { |
|
|
|
func (game *Game) Render() { |
|
|
|
if !RENDER { return } |
|
|
|
if !RENDER { return } |
|
|
|
|
|
|
|
|
|
|
|
game.screen.Clear() |
|
|
|
game.Screen.Clear() |
|
|
|
|
|
|
|
|
|
|
|
game.DrawMap() |
|
|
|
game.DrawMap() |
|
|
|
|
|
|
|
|
|
|
@ -114,84 +131,18 @@ func (game *Game) Render() { |
|
|
|
game.DrawPaths() |
|
|
|
game.DrawPaths() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
game.DrawEntity('@', game.player.pos, tcell.ColorYellow) |
|
|
|
game.DrawEntity('@', game.Player.Pos, tcell.ColorYellow) |
|
|
|
|
|
|
|
|
|
|
|
for pos, _ := range game.enemies { |
|
|
|
for pos, _ := range game.Enemies { |
|
|
|
game.DrawEntity('G', pos, tcell.ColorRed) |
|
|
|
game.DrawEntity('G', pos, tcell.ColorRed) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
game.DrawStatus() |
|
|
|
game.DrawStatus() |
|
|
|
|
|
|
|
|
|
|
|
game.screen.Show() |
|
|
|
game.Screen.Show() |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Exit() { |
|
|
|
|
|
|
|
if RENDER { |
|
|
|
|
|
|
|
game.screen.Fini() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
os.Exit(0) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Occupied(x int, y int) bool { |
|
|
|
|
|
|
|
pos := Position{x, y} |
|
|
|
|
|
|
|
_, is_enemy := game.enemies[pos] |
|
|
|
|
|
|
|
is_player := pos == game.player.pos |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Inbounds comes first to prevent accessing level with bad x,y
|
|
|
|
|
|
|
|
return !game.Inbounds(pos, 1) || |
|
|
|
|
|
|
|
game.level[y][x] == WALL || |
|
|
|
|
|
|
|
is_enemy || |
|
|
|
|
|
|
|
is_player |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) EnemyDeath() { |
|
|
|
//// EVENTS
|
|
|
|
is_dead := make([]Position, 0, len(game.enemies)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for pos, enemy := range game.enemies { |
|
|
|
|
|
|
|
if enemy.hp < 0 { |
|
|
|
|
|
|
|
is_dead = append(is_dead, pos) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, pos := range is_dead { |
|
|
|
|
|
|
|
delete(game.enemies, pos) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) ApplyDamage(attacker *Enemy, defender *Enemy) { |
|
|
|
|
|
|
|
damage := rand.Int() % attacker.damage |
|
|
|
|
|
|
|
defender.hp -= damage |
|
|
|
|
|
|
|
if damage == 0 { |
|
|
|
|
|
|
|
game.Status("MISSED!") |
|
|
|
|
|
|
|
} else if defender.hp > 0 { |
|
|
|
|
|
|
|
game.Status(fmt.Sprintf("HIT %d damage", damage)) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
game.Status("DEAD!") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Attack(target Position) { |
|
|
|
|
|
|
|
enemy, hit_enemy := game.enemies[target] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if hit_enemy { |
|
|
|
|
|
|
|
game.ApplyDamage(&game.player, enemy) |
|
|
|
|
|
|
|
game.ApplyDamage(enemy, &game.player) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) MovePlayer(x_delta int, y_delta int) { |
|
|
|
|
|
|
|
target := Position{ |
|
|
|
|
|
|
|
game.player.pos.x + x_delta, |
|
|
|
|
|
|
|
game.player.pos.y + y_delta, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if game.Occupied(target.x, target.y) { |
|
|
|
|
|
|
|
game.Attack(target) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
game.player.pos = target |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) HandleKeys(ev *tcell.EventKey) bool { |
|
|
|
func (game *Game) HandleKeys(ev *tcell.EventKey) bool { |
|
|
|
switch ev.Key() { |
|
|
|
switch ev.Key() { |
|
|
@ -218,9 +169,9 @@ func (game *Game) HandleKeys(ev *tcell.EventKey) bool { |
|
|
|
func (game *Game) HandleEvents() bool { |
|
|
|
func (game *Game) HandleEvents() bool { |
|
|
|
if !RENDER { return false } |
|
|
|
if !RENDER { return false } |
|
|
|
|
|
|
|
|
|
|
|
switch ev := game.screen.PollEvent().(type) { |
|
|
|
switch ev := game.Screen.PollEvent().(type) { |
|
|
|
case *tcell.EventResize: |
|
|
|
case *tcell.EventResize: |
|
|
|
game.screen.Sync() |
|
|
|
game.Screen.Sync() |
|
|
|
case *tcell.EventKey: |
|
|
|
case *tcell.EventKey: |
|
|
|
return game.HandleKeys(ev) |
|
|
|
return game.HandleKeys(ev) |
|
|
|
} |
|
|
|
} |
|
|
@ -228,188 +179,204 @@ func (game *Game) HandleEvents() bool { |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) InitScreen() { |
|
|
|
////// GAME
|
|
|
|
encoding.Register() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
game.screen, err = tcell.NewScreen() |
|
|
|
|
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = game.screen.Init() |
|
|
|
|
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func NewGame(width int, height int) (*Game) { |
|
|
|
func NewGame(width int, height int) (*Game) { |
|
|
|
var game Game |
|
|
|
var game Game |
|
|
|
|
|
|
|
|
|
|
|
game.width = width |
|
|
|
game.Width = width |
|
|
|
game.height = height |
|
|
|
game.Height = height |
|
|
|
game.enemies = make(map[Position]*Enemy) |
|
|
|
game.Enemies = make(map[Position]*Enemy) |
|
|
|
|
|
|
|
|
|
|
|
game.level = make(Map, height, height) |
|
|
|
game.Level = make(Map, height, height) |
|
|
|
game.paths = make(Paths, height, height) |
|
|
|
game.Paths = make(Paths, height, height) |
|
|
|
|
|
|
|
|
|
|
|
game.player = Enemy{20, Position{1,1}, 4} |
|
|
|
game.Player = Enemy{20, Position{1,1}, 4} |
|
|
|
|
|
|
|
|
|
|
|
return &game |
|
|
|
return &game |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func compass(x int, y int, offset int) []Position { |
|
|
|
|
|
|
|
return []Position{ |
|
|
|
func (game *Game) Exit() { |
|
|
|
Position{x, y - offset}, |
|
|
|
if RENDER { |
|
|
|
Position{x, y + offset}, |
|
|
|
game.Screen.Fini() |
|
|
|
Position{x + offset, y}, |
|
|
|
|
|
|
|
Position{ x - offset, y}, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Inbounds(pos Position, offset int) bool { |
|
|
|
os.Exit(0) |
|
|
|
return pos.x >= offset && |
|
|
|
|
|
|
|
pos.x < game.width - offset && |
|
|
|
|
|
|
|
pos.y >= offset && |
|
|
|
|
|
|
|
pos.y < game.height - offset |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Neighbors(near Position) []Position { |
|
|
|
func (game *Game) PlaceEnemies(places []Position) { |
|
|
|
result := make([]Position, 0, 4) |
|
|
|
for _, pos := range places { |
|
|
|
points := compass(near.x, near.y, 2) |
|
|
|
if rand.Int() % 2 == 0 { |
|
|
|
|
|
|
|
game.Enemies[pos] = &Enemy{10, pos, 4} |
|
|
|
for _, pos := range points { |
|
|
|
|
|
|
|
if game.Inbounds(pos, 0) { |
|
|
|
|
|
|
|
result = append(result, pos) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return result |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) NeighborWalls(pos Position) []Position { |
|
|
|
func (game *Game) Restart() { |
|
|
|
neighbors := game.Neighbors(pos) |
|
|
|
game.SetStatus("YOU DIED! Try again.") |
|
|
|
result := make([]Position, 0) |
|
|
|
game.Player.HP = 20 |
|
|
|
|
|
|
|
game.Player.Pos = Position{1,1} |
|
|
|
|
|
|
|
clear(game.Enemies) |
|
|
|
|
|
|
|
game.FillPaths(game.Paths, PATH_LIMIT) |
|
|
|
|
|
|
|
game.FillMap(game.Level, '#') |
|
|
|
|
|
|
|
|
|
|
|
for _, at := range neighbors { |
|
|
|
game.Render() |
|
|
|
cell := game.level[at.y][at.x] |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if cell == WALL { |
|
|
|
////// MAP
|
|
|
|
result = append(result, at) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result |
|
|
|
func compass(near Position, offset int) []Position { |
|
|
|
|
|
|
|
return []Position{ |
|
|
|
|
|
|
|
Position{near.X, near.Y - offset}, |
|
|
|
|
|
|
|
Position{near.X, near.Y + offset}, |
|
|
|
|
|
|
|
Position{near.X + offset, near.Y}, |
|
|
|
|
|
|
|
Position{near.X - offset, near.Y}, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) FindCoord(on *Position, found *Position) bool { |
|
|
|
func (game *Game) CloneMap() Map { |
|
|
|
for y := 1; y < game.height ; y += 2 { |
|
|
|
// this is a shallow copy though
|
|
|
|
for x := 1; x < game.width ; x += 2 { |
|
|
|
new_map := slices.Clone(game.Level) |
|
|
|
if game.level[y][x] != WALL { |
|
|
|
|
|
|
|
continue |
|
|
|
for i, row := range new_map { |
|
|
|
|
|
|
|
// this makes sure the row is an actual copy
|
|
|
|
|
|
|
|
new_map[i] = slices.Clone(row) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
neighbors := game.Neighbors(Position{x, y}) |
|
|
|
return new_map |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for _, pos := range neighbors { |
|
|
|
func (game *Game) Inbounds(pos Position, offset int) bool { |
|
|
|
if game.level[pos.y][pos.x] == SPACE { |
|
|
|
return pos.X >= offset && |
|
|
|
*on = Position{x, y} |
|
|
|
pos.X < game.Width - offset && |
|
|
|
*found = pos |
|
|
|
pos.Y >= offset && |
|
|
|
return true |
|
|
|
pos.Y < game.Height - offset |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false |
|
|
|
func (game *Game) Occupied(pos Position) bool { |
|
|
|
|
|
|
|
_, is_enemy := game.Enemies[pos] |
|
|
|
|
|
|
|
is_player := pos == game.Player.Pos |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Inbounds comes first to prevent accessing level with bad x,y
|
|
|
|
|
|
|
|
return !game.Inbounds(pos, 1) || |
|
|
|
|
|
|
|
game.Level[pos.Y][pos.X] == WALL || |
|
|
|
|
|
|
|
is_enemy || |
|
|
|
|
|
|
|
is_player |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) HAKStep(from Position, to Position) { |
|
|
|
func (game *Game) FillMap(target Map, setting rune) { |
|
|
|
game.level[from.y][from.x] = SPACE |
|
|
|
for y := 0 ; y < game.Height; y++ { |
|
|
|
row := (from.y + to.y) / 2 |
|
|
|
target[y] = slices.Repeat([]rune{setting}, game.Width) |
|
|
|
col := (from.x + to.x) / 2 |
|
|
|
} |
|
|
|
game.level[row][col] = SPACE |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) HuntAndKill() []Position { |
|
|
|
|
|
|
|
on := Position{1, 1} |
|
|
|
|
|
|
|
found := Position{1,1} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dead_ends := make([]Position, 0) |
|
|
|
///// MOVEMENT
|
|
|
|
|
|
|
|
|
|
|
|
for { |
|
|
|
func (game *Game) MoveEnemy(from Position, to Position) { |
|
|
|
neighbors := game.NeighborWalls(on) |
|
|
|
enemy, ok := game.Enemies[from] |
|
|
|
|
|
|
|
if !ok { log.Fatal("no enemy at", from, "wtf") } |
|
|
|
|
|
|
|
|
|
|
|
if len(neighbors) == 0 { |
|
|
|
delete(game.Enemies, from) |
|
|
|
dead_ends = append(dead_ends, on) |
|
|
|
game.Enemies[to] = enemy |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !game.FindCoord(&on, &found) { |
|
|
|
|
|
|
|
break |
|
|
|
func (game *Game) MovePlayer(x_delta int, y_delta int) { |
|
|
|
|
|
|
|
target := Position{ |
|
|
|
|
|
|
|
game.Player.Pos.X + x_delta, |
|
|
|
|
|
|
|
game.Player.Pos.Y + y_delta, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
game.HAKStep(on, found) |
|
|
|
if game.Occupied(target) { |
|
|
|
|
|
|
|
game.Attack(target) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
rand_neighbor := rand.Int() % len(neighbors) |
|
|
|
game.Player.Pos = target |
|
|
|
nb := neighbors[rand_neighbor] |
|
|
|
|
|
|
|
game.HAKStep(nb, on) |
|
|
|
|
|
|
|
on = nb |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if SHOW_RENDER { |
|
|
|
///// COMBAT
|
|
|
|
game.Render() |
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond) |
|
|
|
func (game *Game) EnemyDeath() { |
|
|
|
|
|
|
|
is_dead := make([]Position, 0, len(game.Enemies)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for pos, enemy := range game.Enemies { |
|
|
|
|
|
|
|
if enemy.HP < 0 { |
|
|
|
|
|
|
|
is_dead = append(is_dead, pos) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return dead_ends |
|
|
|
for _, pos := range is_dead { |
|
|
|
|
|
|
|
delete(game.Enemies, pos) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) FillMap(target Map, setting rune) { |
|
|
|
func (game *Game) ApplyDamage(attacker *Enemy, defender *Enemy) { |
|
|
|
for y := 0 ; y < game.height; y++ { |
|
|
|
damage := rand.Int() % attacker.Damage |
|
|
|
target[y] = slices.Repeat([]rune{setting}, game.width) |
|
|
|
defender.HP -= damage |
|
|
|
|
|
|
|
if damage == 0 { |
|
|
|
|
|
|
|
game.SetStatus("MISSED!") |
|
|
|
|
|
|
|
} else if defender.HP > 0 { |
|
|
|
|
|
|
|
game.SetStatus(fmt.Sprintf("HIT %d damage", damage)) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
game.SetStatus("DEAD!") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) FillPaths(target Paths, setting int) { |
|
|
|
func (game *Game) Attack(target Position) { |
|
|
|
for y := 0 ; y < game.height; y++ { |
|
|
|
enemy, hit_enemy := game.Enemies[target] |
|
|
|
target[y] = slices.Repeat([]int{setting}, game.width) |
|
|
|
|
|
|
|
|
|
|
|
if hit_enemy { |
|
|
|
|
|
|
|
game.ApplyDamage(&game.Player, enemy) |
|
|
|
|
|
|
|
game.ApplyDamage(enemy, &game.Player) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) CarveRoom(pos Position, size int) { |
|
|
|
|
|
|
|
// only use ones far enough inside
|
|
|
|
///// PATHING
|
|
|
|
for y := pos.y - size; y < pos.y + size; y++ { |
|
|
|
|
|
|
|
for x := pos.x - size; x < pos.x + size; x++ { |
|
|
|
func (game *Game) FillPaths(target Paths, setting int) { |
|
|
|
if game.Inbounds(Position{x, y}, 1) { |
|
|
|
for y := 0 ; y < game.Height; y++ { |
|
|
|
game.level[y][x] = SPACE |
|
|
|
target[y] = slices.Repeat([]int{setting}, game.Width) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) MoveEnemy(from Position, to Position) { |
|
|
|
func (game *Game) Neighbors(near Position) []Position { |
|
|
|
enemy, ok := game.enemies[from] |
|
|
|
result := make([]Position, 0, 4) |
|
|
|
if !ok { log.Fatal("no enemy at", from, "wtf") } |
|
|
|
points := compass(near, 2) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, pos := range points { |
|
|
|
|
|
|
|
if game.Inbounds(pos, 0) { |
|
|
|
|
|
|
|
result = append(result, pos) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
delete(game.enemies, from) |
|
|
|
return result |
|
|
|
game.enemies[to] = enemy |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) CloneMap() Map { |
|
|
|
func (game *Game) NeighborWalls(pos Position) []Position { |
|
|
|
// this is a shallow copy though
|
|
|
|
neighbors := game.Neighbors(pos) |
|
|
|
new_map := slices.Clone(game.level) |
|
|
|
result := make([]Position, 0) |
|
|
|
|
|
|
|
|
|
|
|
for i, row := range new_map { |
|
|
|
for _, at := range neighbors { |
|
|
|
// this makes sure the row is an actual copy
|
|
|
|
cell := game.Level[at.Y][at.X] |
|
|
|
new_map[i] = slices.Clone(row) |
|
|
|
|
|
|
|
|
|
|
|
if cell == WALL { |
|
|
|
|
|
|
|
result = append(result, at) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return new_map |
|
|
|
return result |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) PathAddNeighbors(neighbors []Position, closed Map, near Position) []Position { |
|
|
|
func (game *Game) PathAddNeighbors(neighbors []Position, closed Map, near Position) []Position { |
|
|
|
points := compass(near.x, near.y, 1) |
|
|
|
points := compass(near, 1) |
|
|
|
|
|
|
|
|
|
|
|
for _, pos := range points { |
|
|
|
for _, pos := range points { |
|
|
|
// NOTE: if you also add !game.Occupied(pos.x, pos.y) it ????
|
|
|
|
// NOTE: if you also add !game.Occupied(pos.x, pos.y) it ????
|
|
|
|
if closed[pos.y][pos.x] == SPACE { |
|
|
|
if closed[pos.Y][pos.X] == SPACE { |
|
|
|
closed[pos.y][pos.x] = WALL |
|
|
|
closed[pos.Y][pos.X] = WALL |
|
|
|
neighbors = append(neighbors, pos) |
|
|
|
neighbors = append(neighbors, pos) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -417,24 +384,24 @@ func (game *Game) PathAddNeighbors(neighbors []Position, closed Map, near Positi |
|
|
|
return neighbors |
|
|
|
return neighbors |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) PathEnemies() { |
|
|
|
func (game *Game) CalculatePaths() { |
|
|
|
in_grid := make([][]int, game.height, game.height) |
|
|
|
in_grid := make([][]int, game.Height, game.Height) |
|
|
|
game.FillPaths(in_grid, 1) |
|
|
|
game.FillPaths(in_grid, 1) |
|
|
|
in_grid[game.player.pos.y][game.player.pos.x] = 0 |
|
|
|
in_grid[game.Player.Pos.Y][game.Player.Pos.X] = 0 |
|
|
|
|
|
|
|
|
|
|
|
game.FillPaths(game.paths, PATH_LIMIT) |
|
|
|
game.FillPaths(game.Paths, PATH_LIMIT) |
|
|
|
closed := game.CloneMap() |
|
|
|
closed := game.CloneMap() |
|
|
|
starting_pixels := make([]Position, 0, 10) |
|
|
|
starting_pixels := make([]Position, 0, 10) |
|
|
|
open_pixels := make([]Position, 0, 10) |
|
|
|
open_pixels := make([]Position, 0, 10) |
|
|
|
|
|
|
|
|
|
|
|
counter := 0 |
|
|
|
counter := 0 |
|
|
|
|
|
|
|
|
|
|
|
for counter < game.height * game.width { |
|
|
|
for counter < game.Height * game.Width { |
|
|
|
x := counter % game.width |
|
|
|
x := counter % game.Width |
|
|
|
y := counter / game.width |
|
|
|
y := counter / game.Width |
|
|
|
|
|
|
|
|
|
|
|
if in_grid[y][x] == 0 { |
|
|
|
if in_grid[y][x] == 0 { |
|
|
|
game.paths[y][x] = 0 |
|
|
|
game.Paths[y][x] = 0 |
|
|
|
closed[y][x] = WALL |
|
|
|
closed[y][x] = WALL |
|
|
|
starting_pixels = append(starting_pixels, Position{x, y}) |
|
|
|
starting_pixels = append(starting_pixels, Position{x, y}) |
|
|
|
} |
|
|
|
} |
|
|
@ -450,7 +417,7 @@ func (game *Game) PathEnemies() { |
|
|
|
for counter < PATH_LIMIT && len(open_pixels) > 0 { |
|
|
|
for counter < PATH_LIMIT && len(open_pixels) > 0 { |
|
|
|
next_open := make([]Position, 0, 10) |
|
|
|
next_open := make([]Position, 0, 10) |
|
|
|
for _, pos := range open_pixels { |
|
|
|
for _, pos := range open_pixels { |
|
|
|
game.paths[pos.y][pos.x] = counter |
|
|
|
game.Paths[pos.Y][pos.X] = counter |
|
|
|
next_open = game.PathAddNeighbors(next_open, closed, pos) |
|
|
|
next_open = game.PathAddNeighbors(next_open, closed, pos) |
|
|
|
} |
|
|
|
} |
|
|
|
open_pixels = next_open |
|
|
|
open_pixels = next_open |
|
|
@ -458,26 +425,26 @@ func (game *Game) PathEnemies() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for _, pos := range open_pixels { |
|
|
|
for _, pos := range open_pixels { |
|
|
|
game.paths[pos.y][pos.x] = counter |
|
|
|
game.Paths[pos.Y][pos.X] = counter |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) EnemyMovement() { |
|
|
|
func (game *Game) EnemyPathing() { |
|
|
|
for enemy_at, _ := range game.enemies { |
|
|
|
for enemy_at, _ := range game.Enemies { |
|
|
|
// get the four directions
|
|
|
|
// get the four directions
|
|
|
|
dirs := compass(enemy_at.x, enemy_at.y, 1) |
|
|
|
dirs := compass(enemy_at, 1) |
|
|
|
|
|
|
|
|
|
|
|
// sort by closest path number
|
|
|
|
// sort by closest path number
|
|
|
|
slices.SortFunc(dirs, func(a Position, b Position) int { |
|
|
|
slices.SortFunc(dirs, func(a Position, b Position) int { |
|
|
|
return game.paths[a.y][a.x] - game.paths[b.y][b.x] |
|
|
|
return game.Paths[a.Y][a.X] - game.Paths[b.Y][b.X] |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
// 0 dir is now the best direction
|
|
|
|
// 0 dir is now the best direction
|
|
|
|
move_to := dirs[0] |
|
|
|
move_to := dirs[0] |
|
|
|
|
|
|
|
|
|
|
|
// can we hear the player? occupied?
|
|
|
|
// can we hear the player? occupied?
|
|
|
|
can_hear := game.paths[move_to.y][move_to.x] < HEARING_DISTANCE |
|
|
|
can_hear := game.Paths[move_to.Y][move_to.X] < HEARING_DISTANCE |
|
|
|
occupied := game.Occupied(move_to.x, move_to.y) |
|
|
|
occupied := game.Occupied(move_to) |
|
|
|
|
|
|
|
|
|
|
|
if can_hear && !occupied { |
|
|
|
if can_hear && !occupied { |
|
|
|
// move the enemy in the best direction
|
|
|
|
// move the enemy in the best direction
|
|
|
@ -486,45 +453,78 @@ func (game *Game) EnemyMovement() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) AddRooms(dead_ends []Position, size int) { |
|
|
|
|
|
|
|
rand.Shuffle(len(dead_ends), func(i, j int) { |
|
|
|
|
|
|
|
dead_ends[i], dead_ends[j] = dead_ends[j], dead_ends[i] |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, pos := range dead_ends[0:4] { |
|
|
|
|
|
|
|
rs := rand.Int() % size + 1 |
|
|
|
//// MAZE GENERATION
|
|
|
|
game.CarveRoom(pos, rs) |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) HuntNext(on *Position, found *Position) bool { |
|
|
|
|
|
|
|
for y := 1; y < game.Height ; y += 2 { |
|
|
|
|
|
|
|
for x := 1; x < game.Width ; x += 2 { |
|
|
|
|
|
|
|
if game.Level[y][x] != WALL { |
|
|
|
|
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) NewMap() []Position { |
|
|
|
neighbors := game.Neighbors(Position{x, y}) |
|
|
|
game.FillMap(game.level, '#') |
|
|
|
|
|
|
|
dead_ends := game.HuntAndKill() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
game.FillMap(game.level, '#') |
|
|
|
for _, pos := range neighbors { |
|
|
|
game.AddRooms(dead_ends, game.height / 8) |
|
|
|
if game.Level[pos.Y][pos.X] == SPACE { |
|
|
|
|
|
|
|
*on = Position{x, y} |
|
|
|
|
|
|
|
*found = pos |
|
|
|
|
|
|
|
return true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
dead_ends = game.HuntAndKill() |
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return dead_ends |
|
|
|
func (game *Game) HAKStep(from Position, to Position) { |
|
|
|
|
|
|
|
game.Level[from.Y][from.X] = SPACE |
|
|
|
|
|
|
|
row := (from.Y + to.Y) / 2 |
|
|
|
|
|
|
|
col := (from.X + to.X) / 2 |
|
|
|
|
|
|
|
game.Level[row][col] = SPACE |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) PlaceEnemies(places []Position) { |
|
|
|
func (game *Game) HuntAndKill() []Position { |
|
|
|
for _, pos := range places { |
|
|
|
on := Position{1, 1} |
|
|
|
if rand.Int() % 2 == 0 { |
|
|
|
found := Position{1,1} |
|
|
|
game.enemies[pos] = &Enemy{10, pos, 4} |
|
|
|
|
|
|
|
|
|
|
|
dead_ends := make([]Position, 0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for { |
|
|
|
|
|
|
|
neighbors := game.NeighborWalls(on) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(neighbors) == 0 { |
|
|
|
|
|
|
|
dead_ends = append(dead_ends, on) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !game.HuntNext(&on, &found) { |
|
|
|
|
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
game.HAKStep(on, found) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
rand_neighbor := rand.Int() % len(neighbors) |
|
|
|
|
|
|
|
nb := neighbors[rand_neighbor] |
|
|
|
|
|
|
|
game.HAKStep(nb, on) |
|
|
|
|
|
|
|
on = nb |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if SHOW_RENDER { |
|
|
|
|
|
|
|
game.Render() |
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return dead_ends |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Restart() { |
|
|
|
func (game *Game) NewMap() []Position { |
|
|
|
game.Status("YOU DIED! Try again.") |
|
|
|
game.FillMap(game.Level, '#') |
|
|
|
game.player.hp = 20 |
|
|
|
return game.HuntAndKill() |
|
|
|
game.player.pos = Position{1,1} |
|
|
|
|
|
|
|
game.screen.Clear() |
|
|
|
|
|
|
|
clear(game.enemies) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
|
out, err := os.Create("debug.log") |
|
|
|
out, err := os.Create("debug.log") |
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
if err != nil { log.Fatal(err) } |
|
|
@ -538,14 +538,14 @@ func main() { |
|
|
|
game.PlaceEnemies(dead_ends) |
|
|
|
game.PlaceEnemies(dead_ends) |
|
|
|
game.Render() |
|
|
|
game.Render() |
|
|
|
|
|
|
|
|
|
|
|
for game.HandleEvents() && game.player.hp > 0 { |
|
|
|
for game.HandleEvents() && game.Player.HP > 0 { |
|
|
|
game.EnemyDeath() |
|
|
|
game.EnemyDeath() |
|
|
|
game.PathEnemies() |
|
|
|
game.CalculatePaths() |
|
|
|
game.EnemyMovement() |
|
|
|
game.EnemyPathing() |
|
|
|
game.Render() |
|
|
|
game.Render() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if game.player.hp <= 0 { |
|
|
|
if game.Player.HP <= 0 { |
|
|
|
game.Restart() |
|
|
|
game.Restart() |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
game.Exit() |
|
|
|
game.Exit() |
|
|
|