Added hover effect for terrain.

This commit is contained in:
Sander Schobers 2020-05-09 22:50:11 +02:00
parent c02fb342a7
commit 72da0ef462
4 changed files with 12 additions and 9 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@ -2,6 +2,7 @@
tile-dirt: images/tile_dirt.png
tile-grass: images/tile_grass.png
tile-snow: images/tile_snow.png
hover: images/hover.png
cactus-small-1: images/cactus_short_NE.png
cactus-small-2: images/cactus_short_NW.png

View File

@ -38,6 +38,8 @@ func (c *Context) Init(renderer *sdl.Renderer) {
c.Renderer = renderer
c.Fonts.Init(c.Resources.Copy())
c.Textures.Init(renderer, c.Resources.Copy())
c.Renderer.SetDrawBlendMode(sdl.BLENDMODE_BLEND)
}
func (c *Context) Quit() { c.ShouldQuit = true }

View File

@ -64,7 +64,9 @@ func (r *terrainRenderer) Handle(ctx *Context, event sdl.Event) {
}
func (r *terrainRenderer) Render(ctx *Context) {
toTileTexture := func(temp, humid float64) *Texture {
hover := Pt(int32(geom.Round32(r.hover.X)), int32(geom.Round32(r.hover.Y)))
toTileTexture := func(x, y int32) *Texture {
temp := r.terrain.Temp.Value(x, y)
if temp < .35 {
return ctx.Textures.Texture("tile-snow")
}
@ -141,16 +143,14 @@ func (r *terrainRenderer) Render(ctx *Context) {
// horizontal: [191, 321) = 130
// vertical: [267,332) = 65
hover := Pt(int32(geom.Round32(r.hover.X)), int32(geom.Round32(r.hover.Y)))
r.project.visibleTiles(func(x, y int32, pos Point) {
if x == hover.X && y == hover.Y {
return
}
temp := r.terrain.Temp.Value(x, y)
humid := r.terrain.Humid.Value(x, y)
text := toTileTexture(temp, humid)
text := toTileTexture(x, y)
rect := r.project.screenToTileRect(pos)
text.Copy(ctx.Renderer, rect)
if x == hover.X && y == hover.Y {
ctx.Textures.Texture("hover").Copy(ctx.Renderer, rect)
}
})
r.project.visibleTiles(func(x, y int32, pos Point) {
@ -160,7 +160,7 @@ func (r *terrainRenderer) Render(ctx *Context) {
}
placeX, placeY := r.terrain.PlaceX.Value(x, y), r.terrain.PlaceY.Value(x, y)
pos = r.project.mapToScreenF(float32(x)-.2+float32(.8*placeX-.4), float32(y)-.2+float32(.8*placeY-.4))
pos = r.project.mapToScreenF(float32(x)-.2+float32(.9*placeX-.45), float32(y)-.2+float32(.9*placeY-.45))
text.Copy(ctx.Renderer, r.project.screenToTileRect(pos))
})