Add power scan
Zones are now scanned for whether or not they are connected to a power plant.main
parent
69a012bedd
commit
6dbd53e9c4
Binary file not shown.
After Width: | Height: | Size: 8.1 KiB |
@ -1,100 +0,0 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"strings"
|
||||
|
||||
"code.rocketnine.space/tslocum/citylimits/component"
|
||||
"code.rocketnine.space/tslocum/citylimits/world"
|
||||
"code.rocketnine.space/tslocum/gohan"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
||||
)
|
||||
|
||||
type RenderMessageSystem struct {
|
||||
op *ebiten.DrawImageOptions
|
||||
logoImg *ebiten.Image
|
||||
msgImg *ebiten.Image
|
||||
tmpImg *ebiten.Image
|
||||
}
|
||||
|
||||
func NewRenderMessageSystem() *RenderMessageSystem {
|
||||
s := &RenderMessageSystem{
|
||||
op: &ebiten.DrawImageOptions{},
|
||||
logoImg: ebiten.NewImage(1, 1),
|
||||
msgImg: ebiten.NewImage(1, 1),
|
||||
tmpImg: ebiten.NewImage(200, 200),
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *RenderMessageSystem) Needs() []gohan.ComponentID {
|
||||
return []gohan.ComponentID{
|
||||
component.PositionComponentID,
|
||||
component.VelocityComponentID,
|
||||
component.WeaponComponentID,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *RenderMessageSystem) Uses() []gohan.ComponentID {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *RenderMessageSystem) Update(_ *gohan.Context) error {
|
||||
if !world.World.GameStarted || world.World.GameOver || !world.World.MessageVisible {
|
||||
return nil
|
||||
}
|
||||
|
||||
world.World.MessageTicks++
|
||||
if world.World.MessageTicks == world.World.MessageDuration {
|
||||
world.World.MessageVisible = false
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *RenderMessageSystem) Draw(_ *gohan.Context, screen *ebiten.Image) error {
|
||||
if !world.World.GameStarted || !world.World.MessageVisible {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Draw message.
|
||||
if world.World.MessageUpdated {
|
||||
s.drawMessage()
|
||||
}
|
||||
bounds := s.msgImg.Bounds()
|
||||
x := (float64(world.World.ScreenW) / 2) - (float64(bounds.Dx()) / 2)
|
||||
y := (float64(world.World.ScreenH) / 2) - (float64(bounds.Dy()) / 2)
|
||||
s.op.GeoM.Reset()
|
||||
s.op.GeoM.Translate(x, y)
|
||||
screen.DrawImage(s.msgImg, s.op)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *RenderMessageSystem) drawMessage() {
|
||||
split := strings.Split(world.World.MessageText, "\n")
|
||||
width := 0
|
||||
for _, line := range split {
|
||||
lineSize := len(line) * 12
|
||||
if lineSize > width {
|
||||
width = lineSize
|
||||
}
|
||||
}
|
||||
height := len(split) * 32
|
||||
|
||||
const padding = 8
|
||||
width, height = width+padding*2, height+padding*2
|
||||
|
||||
s.msgImg = ebiten.NewImage(width, height)
|
||||
s.msgImg.Fill(color.RGBA{17, 17, 17, 255})
|
||||
|
||||
s.tmpImg.Clear()
|
||||
s.tmpImg = ebiten.NewImage(width*2, height*2)
|
||||
s.op.GeoM.Reset()
|
||||
s.op.GeoM.Scale(2, 2)
|
||||
s.op.GeoM.Translate(float64(padding), float64(padding))
|
||||
ebitenutil.DebugPrint(s.tmpImg, world.World.MessageText)
|
||||
s.msgImg.DrawImage(s.tmpImg, s.op)
|
||||
s.op.ColorM.Reset()
|
||||
}
|
@ -0,0 +1,153 @@
|
||||
package world
|
||||
|
||||
import (
|
||||
"github.com/beefsack/go-astar"
|
||||
)
|
||||
|
||||
const (
|
||||
powerEmptyTile = iota
|
||||
powerSourceTile
|
||||
powerDestinationTile
|
||||
)
|
||||
|
||||
type PowerMapTile struct {
|
||||
X int
|
||||
Y int
|
||||
CarriesPower bool // Set to true for roads and all building tiles (even power plants)
|
||||
}
|
||||
|
||||
func (t *PowerMapTile) Up() *PowerMapTile {
|
||||
tx, ty := t.X, t.Y-1
|
||||
if !ValidXY(tx, ty) {
|
||||
return nil
|
||||
}
|
||||
n := World.Power[tx][ty]
|
||||
if !n.CarriesPower {
|
||||
return nil
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (t *PowerMapTile) Down() *PowerMapTile {
|
||||
tx, ty := t.X, t.Y+1
|
||||
if !ValidXY(tx, ty) {
|
||||
return nil
|
||||
}
|
||||
n := World.Power[tx][ty]
|
||||
if !n.CarriesPower {
|
||||
return nil
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (t *PowerMapTile) Left() *PowerMapTile {
|
||||
tx, ty := t.X-1, t.Y
|
||||
if !ValidXY(tx, ty) {
|
||||
return nil
|
||||
}
|
||||
n := World.Power[tx][ty]
|
||||
if !n.CarriesPower {
|
||||
return nil
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (t *PowerMapTile) Right() *PowerMapTile {
|
||||
tx, ty := t.X+1, t.Y
|
||||
if !ValidXY(tx, ty) {
|
||||
return nil
|
||||
}
|
||||
n := World.Power[tx][ty]
|
||||
if !n.CarriesPower {
|
||||
return nil
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
type PowerMap [][]*PowerMapTile
|
||||
|
||||
func newPowerMap() PowerMap {
|
||||
m := make(PowerMap, 256)
|
||||
for x := 0; x < 256; x++ {
|
||||
m[x] = make([]*PowerMapTile, 256)
|
||||
for y := 0; y < 256; y++ {
|
||||
m[x][y] = &PowerMapTile{
|
||||
X: x,
|
||||
Y: y,
|
||||
}
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func newPowerOuts() [][]bool {
|
||||
m := make([][]bool, 256)
|
||||
for x := 0; x < 256; x++ {
|
||||
m[x] = make([]bool, 256)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func ResetPowerOuts() {
|
||||
for x := 0; x < 256; x++ {
|
||||
for y := 0; y < 256; y++ {
|
||||
World.PowerOuts[x][y] = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m PowerMap) GetTile(x, y int) *PowerMapTile {
|
||||
if !ValidXY(x, y) {
|
||||
return nil
|
||||
}
|
||||
return m[x][y]
|
||||
}
|
||||
|
||||
func (m PowerMap) SetTile(x, y int, carriesPower bool) {
|
||||
t := m[x][y]
|
||||
t.CarriesPower = carriesPower
|
||||
|
||||
World.PowerUpdated = true
|
||||
}
|
||||
|
||||
func (t *PowerMapTile) PathNeighbors() []astar.Pather {
|
||||
var neighbors []astar.Pather
|
||||
n := t.Up()
|
||||
if n != nil {
|
||||
neighbors = append(neighbors, n)
|
||||
}
|
||||
n = t.Down()
|
||||
if n != nil {
|
||||
neighbors = append(neighbors, n)
|
||||
}
|
||||
n = t.Left()
|
||||
if n != nil {
|
||||
neighbors = append(neighbors, n)
|
||||
}
|
||||
n = t.Right()
|
||||
if n != nil {
|
||||
neighbors = append(neighbors, n)
|
||||
}
|
||||
return neighbors
|
||||
}
|
||||
|
||||
func (t *PowerMapTile) PathNeighborCost(to astar.Pather) float64 {
|
||||
toT := to.(*PowerMapTile)
|
||||
if !toT.CarriesPower {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
func (t *PowerMapTile) PathEstimatedCost(to astar.Pather) float64 {
|
||||
toT := to.(*PowerMapTile)
|
||||
absX := toT.X - t.X
|
||||
if absX < 0 {
|
||||
absX = -absX
|
||||
}
|
||||
absY := toT.Y - t.Y
|
||||
if absY < 0 {
|
||||
absY = -absY
|
||||
}
|
||||
return float64(absX + absY)
|
||||
}
|
Loading…
Reference in New Issue