A Go version of the https://lcthw.dev/learn-code-the-hard-way/curseyou-python-rogue that makes a tiny Rogue in Go.
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
1.8 KiB
101 lines
1.8 KiB
|
3 weeks ago
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"log"
|
||
|
|
"os"
|
||
|
|
"math/rand"
|
||
|
|
"fmt"
|
||
|
|
)
|
||
|
|
|
||
|
|
func NewGame(width int, height int) (*Game) {
|
||
|
|
var game Game
|
||
|
|
|
||
|
|
if width % 2 == 0 { log.Fatal("NewGame: width must be odd") }
|
||
|
|
if height % 2 == 0 {
|
||
|
|
log.Fatal("NewGame: height must be odd")
|
||
|
|
}
|
||
|
|
|
||
|
|
game.Width = width
|
||
|
|
game.Height = height
|
||
|
|
game.Enemies = make(map[Position]*Thing)
|
||
|
|
game.Loot = make(map[Position]*Thing)
|
||
|
|
game.Level = make(Map, height, height)
|
||
|
|
game.Paths = make(Paths, height, height)
|
||
|
|
game.Player = Thing{20, Position{1,1}, 4, 0}
|
||
|
|
|
||
|
|
return &game
|
||
|
|
}
|
||
|
|
|
||
|
|
func (game *Game) Exit() {
|
||
|
|
if RENDER {
|
||
|
|
game.Screen.Fini()
|
||
|
|
}
|
||
|
|
|
||
|
|
os.Exit(0)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (game *Game) PlaceEnemies() {
|
||
|
|
for _, pos := range game.DeadEnds {
|
||
|
|
if rand.Int() % 2 == 0 {
|
||
|
|
enemy := new(Thing)
|
||
|
|
enemy.HP = 10
|
||
|
|
enemy.Pos = pos
|
||
|
|
enemy.Damage = 4
|
||
|
|
game.Enemies[pos] = enemy
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (game *Game) PlaceLoot() {
|
||
|
|
for _, pos := range game.DeadEnds {
|
||
|
|
if !game.Occupied(pos) {
|
||
|
|
loot := new(Thing)
|
||
|
|
loot.Healing = 10
|
||
|
|
game.Loot[pos] = loot
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (game *Game) Restart() {
|
||
|
|
game.SetStatus("YOU DIED! Try again.")
|
||
|
|
game.Player.HP = 20
|
||
|
|
game.Player.Pos = Position{1,1}
|
||
|
|
clear(game.Enemies)
|
||
|
|
|
||
|
|
game.FillPaths(game.Paths, PATH_LIMIT)
|
||
|
|
game.NewMap()
|
||
|
|
game.Render()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (game *Game) WorldGenerator() {
|
||
|
|
// clear the map to all '#'
|
||
|
|
game.NewMap()
|
||
|
|
|
||
|
|
// first generate a maze to get dead ends
|
||
|
|
game.NewMaze()
|
||
|
|
|
||
|
|
// this clears the previous map
|
||
|
|
game.NewMap()
|
||
|
|
|
||
|
|
game.SetDeadArea(3)
|
||
|
|
|
||
|
|
// create random rooms
|
||
|
|
game.RandomizeRooms(ROOM_SIZE)
|
||
|
|
|
||
|
|
// then use the dead_ends to create rooms
|
||
|
|
game.PlaceRooms()
|
||
|
|
|
||
|
|
// re-run the maze gen, and it'll work around the rooms
|
||
|
|
game.NewMaze()
|
||
|
|
|
||
|
|
// validate the map
|
||
|
|
if !game.Validate() {
|
||
|
|
fmt.Println("BAD MAP")
|
||
|
|
os.Exit(1)
|
||
|
|
}
|
||
|
|
|
||
|
|
// finally place enemies inside the rooms
|
||
|
|
game.PlaceEnemies()
|
||
|
|
game.PlaceLoot()
|
||
|
|
}
|