2020-05-09 11:55:37 +00:00
|
|
|
package tins2020
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
)
|
|
|
|
|
|
|
|
type terrainRenderer struct {
|
2020-05-10 18:44:20 +00:00
|
|
|
game *Game
|
2020-05-09 22:19:44 +00:00
|
|
|
hover *Point
|
2020-05-09 11:55:37 +00:00
|
|
|
project projection
|
|
|
|
|
2020-05-10 18:44:20 +00:00
|
|
|
drag Drageable
|
2020-05-09 11:55:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 18:44:20 +00:00
|
|
|
func NewTerrainRenderer(game *Game) Control {
|
2020-05-11 01:09:01 +00:00
|
|
|
return &terrainRenderer{game: game, project: newProjection()}
|
2020-05-09 11:55:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 15:16:18 +00:00
|
|
|
func (r *terrainRenderer) Arrange(ctx *Context, _ Rectangle) {
|
|
|
|
r.project.update(ctx.Renderer)
|
|
|
|
}
|
|
|
|
|
2020-05-09 11:55:37 +00:00
|
|
|
func (r *terrainRenderer) Init(ctx *Context) error {
|
2020-05-11 01:09:01 +00:00
|
|
|
r.game.CenterChanged().RegisterItf(func(state interface{}) {
|
|
|
|
center := state.(Point)
|
|
|
|
r.project.center = center.ToPtF()
|
|
|
|
r.project.update(ctx.Renderer)
|
|
|
|
})
|
2020-05-09 11:55:37 +00:00
|
|
|
r.project.update(ctx.Renderer)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-10 23:15:48 +00:00
|
|
|
func isControlKeyDown() bool {
|
|
|
|
state := sdl.GetKeyboardState()
|
2020-05-11 12:41:42 +00:00
|
|
|
return state[sdl.SCANCODE_LCTRL] == 1 || state[sdl.SCANCODE_RCTRL] == 1 || state[sdl.SCANCODE_LGUI] == 1 || state[sdl.SCANCODE_RGUI] == 1
|
2020-05-10 23:15:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 09:44:50 +00:00
|
|
|
func (r *terrainRenderer) Handle(ctx *Context, event sdl.Event) bool {
|
2020-05-09 11:55:37 +00:00
|
|
|
switch e := event.(type) {
|
|
|
|
case *sdl.MouseButtonEvent:
|
2020-05-10 15:16:18 +00:00
|
|
|
if r.project.windowInteractRect.IsPointInside(e.X, e.Y) {
|
2020-05-10 23:15:48 +00:00
|
|
|
if e.Type == sdl.MOUSEBUTTONDOWN {
|
2020-05-11 12:41:42 +00:00
|
|
|
controlKeyDown := isControlKeyDown()
|
|
|
|
if e.Button == sdl.BUTTON_MIDDLE || (e.Button == sdl.BUTTON_LEFT && controlKeyDown) {
|
2020-05-10 23:15:48 +00:00
|
|
|
if !r.drag.IsDragging() {
|
|
|
|
r.drag.Start(Pt(e.X, e.Y))
|
|
|
|
}
|
2020-05-10 18:44:20 +00:00
|
|
|
}
|
2020-05-11 12:41:42 +00:00
|
|
|
if e.Button == sdl.BUTTON_LEFT && !controlKeyDown {
|
2020-05-10 18:44:20 +00:00
|
|
|
pos := r.project.screenToMapInt(e.X, e.Y)
|
|
|
|
r.game.UserClickedTile(pos)
|
|
|
|
}
|
2020-05-10 23:15:48 +00:00
|
|
|
if e.Button == sdl.BUTTON_RIGHT {
|
|
|
|
if e.Type == sdl.MOUSEBUTTONDOWN {
|
|
|
|
r.game.CancelTool()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e.Type == sdl.MOUSEBUTTONUP {
|
|
|
|
if r.drag.IsDragging() {
|
2020-05-11 01:09:01 +00:00
|
|
|
r.game.Terrain.Center = mapToTile(r.project.center)
|
2020-05-10 23:15:48 +00:00
|
|
|
r.drag.Cancel()
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
2020-05-09 11:55:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case *sdl.MouseMotionEvent:
|
2020-05-10 15:16:18 +00:00
|
|
|
if r.project.windowInteractRect.IsPointInside(e.X, e.Y) {
|
2020-05-10 18:44:20 +00:00
|
|
|
hover := r.project.screenToMapInt(e.X, e.Y)
|
|
|
|
r.hover = &hover
|
2020-05-09 22:19:44 +00:00
|
|
|
} else {
|
|
|
|
r.hover = nil
|
|
|
|
}
|
2020-05-10 18:44:20 +00:00
|
|
|
if r.drag.IsDragging() {
|
|
|
|
delta := r.drag.Move(Pt(e.X, e.Y))
|
|
|
|
r.project.center = r.project.center.Sub(r.project.screenToMapRel(delta.X, delta.Y))
|
2020-05-09 11:55:37 +00:00
|
|
|
r.project.update(ctx.Renderer)
|
|
|
|
}
|
|
|
|
case *sdl.MouseWheelEvent:
|
2020-05-10 10:40:44 +00:00
|
|
|
if r.hover != nil {
|
2020-05-11 10:05:54 +00:00
|
|
|
if e.Y < 0 {
|
|
|
|
r.project.ZoomOut(ctx, r.hover.ToPtF())
|
|
|
|
} else {
|
|
|
|
r.project.ZoomIn(ctx, r.hover.ToPtF())
|
2020-05-10 10:21:51 +00:00
|
|
|
}
|
2020-05-09 11:55:37 +00:00
|
|
|
}
|
2020-05-11 06:03:33 +00:00
|
|
|
case *sdl.WindowEvent:
|
|
|
|
if e.Event == sdl.WINDOWEVENT_LEAVE {
|
|
|
|
r.hover = nil
|
|
|
|
r.project.update(ctx.Renderer)
|
|
|
|
}
|
2020-05-11 10:05:54 +00:00
|
|
|
case *sdl.KeyboardEvent:
|
|
|
|
if e.Type == sdl.KEYDOWN {
|
|
|
|
switch e.Keysym.Sym {
|
|
|
|
case sdl.K_PLUS:
|
|
|
|
r.project.ZoomIn(ctx, r.project.center)
|
|
|
|
case sdl.K_KP_PLUS:
|
|
|
|
r.project.ZoomIn(ctx, r.project.center)
|
|
|
|
case sdl.K_MINUS:
|
|
|
|
r.project.ZoomOut(ctx, r.project.center)
|
|
|
|
case sdl.K_KP_MINUS:
|
|
|
|
r.project.ZoomOut(ctx, r.project.center)
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 11:55:37 +00:00
|
|
|
}
|
2020-05-11 09:44:50 +00:00
|
|
|
return false
|
2020-05-09 11:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *terrainRenderer) Render(ctx *Context) {
|
2020-05-11 01:09:01 +00:00
|
|
|
terrain := r.game.Terrain
|
2020-05-09 20:50:11 +00:00
|
|
|
toTileTexture := func(x, y int32) *Texture {
|
2020-05-11 01:09:01 +00:00
|
|
|
temp := terrain.Temp.Value(x, y)
|
2020-05-09 11:55:37 +00:00
|
|
|
if temp < .35 {
|
|
|
|
return ctx.Textures.Texture("tile-snow")
|
|
|
|
}
|
|
|
|
if temp > .65 {
|
|
|
|
return ctx.Textures.Texture("tile-dirt")
|
|
|
|
}
|
|
|
|
return ctx.Textures.Texture("tile-grass")
|
|
|
|
}
|
|
|
|
|
|
|
|
variantToInt := func(variant float64) int {
|
|
|
|
if variant < .25 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if variant < .5 {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
if variant < .75 {
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
if variant < 1 {
|
|
|
|
return 4
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
variantToTexture := func(format string, variant float64) *Texture {
|
|
|
|
textName := fmt.Sprintf(format, variantToInt(variant))
|
|
|
|
return ctx.Textures.Texture(textName)
|
|
|
|
}
|
|
|
|
|
|
|
|
stretch := func(x, from, to float64) float64 { return (x - from) * 1 / (to - from) }
|
|
|
|
|
|
|
|
toPropTexture := func(temp, humid, variant float64) *Texture {
|
|
|
|
if temp < .35 {
|
|
|
|
if humid < .2 {
|
|
|
|
return nil
|
|
|
|
} else if humid < .7 {
|
|
|
|
return variantToTexture("bush-small-%d", variant*5)
|
|
|
|
}
|
|
|
|
return variantToTexture("tree-pine-%d", variant*5)
|
|
|
|
}
|
|
|
|
if temp > .65 {
|
|
|
|
if humid < .2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if humid < .7 {
|
|
|
|
return variantToTexture("cactus-short-%d", variant*7)
|
|
|
|
}
|
|
|
|
return variantToTexture("cactus-tall-%d", variant*2)
|
|
|
|
}
|
|
|
|
if humid < .2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
multiplier := 1 - stretch(humid, 0.2, 1)
|
|
|
|
if variant < .5 {
|
|
|
|
return variantToTexture("tree-fat-%d", stretch(variant, 0, .5)*multiplier*3)
|
|
|
|
} else if variant < .8 {
|
|
|
|
return variantToTexture("grass-small-%d", stretch(variant, .5, .8)*multiplier*2)
|
|
|
|
}
|
|
|
|
return variantToTexture("bush-large-%d", stretch(variant, .8, 1)*multiplier)
|
|
|
|
}
|
|
|
|
|
2020-05-09 17:34:43 +00:00
|
|
|
toItemTexture := func(x, y int32) *Texture {
|
2020-05-11 01:09:01 +00:00
|
|
|
variant := terrain.Variant.Value(x, y)
|
|
|
|
flower, ok := terrain.Flowers[Pt(x, y)]
|
2020-05-09 17:34:43 +00:00
|
|
|
if ok {
|
2020-05-10 22:00:35 +00:00
|
|
|
desc, _ := r.game.Herbarium.Find(flower.ID)
|
|
|
|
return ctx.Textures.Texture(desc.IconTemplate.Variant(variantToInt(variant)))
|
2020-05-09 17:34:43 +00:00
|
|
|
}
|
2020-05-11 01:09:01 +00:00
|
|
|
temp := terrain.Temp.Value(x, y)
|
|
|
|
humid := terrain.Humid.Value(x, y)
|
2020-05-09 17:34:43 +00:00
|
|
|
return toPropTexture(temp, humid, variant)
|
|
|
|
}
|
|
|
|
|
2020-05-10 08:56:20 +00:00
|
|
|
// horizontal: [0, 128) = 128
|
|
|
|
// vertical (tile): [96,160) = 64
|
|
|
|
// vertical (total): [0,160) = 160
|
2020-05-09 11:55:37 +00:00
|
|
|
|
2020-05-09 14:48:39 +00:00
|
|
|
r.project.visibleTiles(func(x, y int32, pos Point) {
|
2020-05-09 20:50:11 +00:00
|
|
|
text := toTileTexture(x, y)
|
2020-05-09 14:48:39 +00:00
|
|
|
rect := r.project.screenToTileRect(pos)
|
2020-05-10 15:16:18 +00:00
|
|
|
text.CopyResize(ctx.Renderer, rect)
|
2020-05-09 20:50:11 +00:00
|
|
|
|
2020-05-09 22:19:44 +00:00
|
|
|
if r.hover != nil && x == r.hover.X && y == r.hover.Y {
|
2020-05-10 15:16:18 +00:00
|
|
|
ctx.Textures.Texture("tile-hover").CopyResize(ctx.Renderer, rect)
|
2020-05-09 20:50:11 +00:00
|
|
|
}
|
2020-05-09 14:48:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
r.project.visibleTiles(func(x, y int32, pos Point) {
|
2020-05-09 17:34:43 +00:00
|
|
|
text := toItemTexture(x, y)
|
2020-05-09 14:48:39 +00:00
|
|
|
if text == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-11 01:09:01 +00:00
|
|
|
placeX, placeY := terrain.PlaceX.Value(x, y), terrain.PlaceY.Value(x, y)
|
2020-05-09 20:50:11 +00:00
|
|
|
pos = r.project.mapToScreenF(float32(x)-.2+float32(.9*placeX-.45), float32(y)-.2+float32(.9*placeY-.45))
|
2020-05-10 15:16:18 +00:00
|
|
|
text.CopyResize(ctx.Renderer, r.project.screenToTileRect(pos))
|
2020-05-09 14:48:39 +00:00
|
|
|
})
|
2020-05-09 11:55:37 +00:00
|
|
|
}
|