Add random colored buildings
This commit is contained in:
parent
d910555d36
commit
42704e2423
2 changed files with 206 additions and 43 deletions
28
asset/asset.go
Normal file
28
asset/asset.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package asset
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"image"
|
||||
|
||||
_ "image/jpeg"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
//go:embed image
|
||||
var FS embed.FS
|
||||
|
||||
func LoadImage(p string) *ebiten.Image {
|
||||
f, err := FS.Open(p)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
baseImg, _, err := image.Decode(f)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return ebiten.NewImageFromImage(baseImg)
|
||||
}
|
221
main.go
221
main.go
|
@ -1,10 +1,15 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"math"
|
||||
"math/rand"
|
||||
"os"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
||||
|
||||
_ "embed"
|
||||
|
||||
"github.com/solarlune/tetra3d"
|
||||
|
@ -16,6 +21,10 @@ type Game struct {
|
|||
Width, Height int
|
||||
Scene *tetra3d.Scene
|
||||
Camera *tetra3d.Camera
|
||||
|
||||
playerModel *tetra3d.Model
|
||||
|
||||
debug int
|
||||
}
|
||||
|
||||
func NewGame() *Game {
|
||||
|
@ -32,6 +41,7 @@ func NewGame() *Game {
|
|||
// In this example, we will simply create a cube and place it in the scene.
|
||||
|
||||
func (g *Game) Init() {
|
||||
const mapSize = 128
|
||||
|
||||
// Create a new Scene and name it.
|
||||
g.Scene = tetra3d.NewScene("mainScene")
|
||||
|
@ -47,9 +57,16 @@ func (g *Game) Init() {
|
|||
light.On = true
|
||||
g.Scene.Root.AddChildren(light)
|
||||
|
||||
const (
|
||||
sideBrightness = 0.8
|
||||
backBrightness = 0.6
|
||||
frontBrightness = 1.0
|
||||
)
|
||||
|
||||
{
|
||||
// Left side
|
||||
l := tetra3d.NewDirectionalLight("", 1, 1, 1, 0.6)
|
||||
//l := tetra3d.NewDirectionalLight("", 1, 1, 1, 0.6)
|
||||
l := tetra3d.NewDirectionalLight("", 1, 1, 1, sideBrightness)
|
||||
l.Move(0, 14, 0)
|
||||
l.Rotate(1, 0, 0, -math.Pi/2)
|
||||
l.Rotate(0, 1, 0, -math.Pi/2)
|
||||
|
@ -59,7 +76,7 @@ func (g *Game) Init() {
|
|||
}
|
||||
{
|
||||
// Right side
|
||||
l := tetra3d.NewDirectionalLight("", 1, 1, 1, 0.6)
|
||||
l := tetra3d.NewDirectionalLight("", 1, 1, 1, sideBrightness)
|
||||
l.Move(0, 14, 0)
|
||||
l.Rotate(1, 0, 0, -math.Pi/2)
|
||||
l.Rotate(0, 1, 0, math.Pi/2)
|
||||
|
@ -69,7 +86,7 @@ func (g *Game) Init() {
|
|||
}
|
||||
{
|
||||
// Back side
|
||||
l := tetra3d.NewDirectionalLight("", 1, 1, 1, 0.8)
|
||||
l := tetra3d.NewDirectionalLight("", 1, 1, 1, backBrightness)
|
||||
l.Move(0, 14, 0)
|
||||
l.Rotate(1, 0, 0, -math.Pi/2)
|
||||
l.Rotate(1, 0, 0, -math.Pi/2)
|
||||
|
@ -79,7 +96,7 @@ func (g *Game) Init() {
|
|||
}
|
||||
{
|
||||
// Front side
|
||||
l := tetra3d.NewDirectionalLight("", 1, 1, 1, 0.8)
|
||||
l := tetra3d.NewDirectionalLight("", 1, 1, 1, frontBrightness)
|
||||
l.Move(0, 14, 0)
|
||||
l.Rotate(1, 0, 0, -math.Pi/2)
|
||||
l.Rotate(1, 0, 0, math.Pi/2)
|
||||
|
@ -87,41 +104,155 @@ func (g *Game) Init() {
|
|||
l.On = true
|
||||
g.Scene.Root.AddChildren(l)
|
||||
}
|
||||
// Front side
|
||||
/*l = tetra3d.NewDirectionalLight("", 1, 1, 1, 0.5)
|
||||
l.Move(0, 14, 0)
|
||||
l.Rotate(1, 0, 0, -math.Pi/2)
|
||||
l.Rotate(1, 1, 0, math.Pi/2)
|
||||
//light.Move(0, 1, -2)
|
||||
l.On = true
|
||||
g.Scene.Root.AddChildren(l)*/
|
||||
|
||||
plane := tetra3d.NewModel(tetra3d.NewPlaneMesh(2, 2), "plane")
|
||||
plane.Color.Set(40.0/255.0, 40.0/255.0, 40.0/255.0, 1)
|
||||
plane.Grow(10000, 0, 10000)
|
||||
//plane.Rotate(0, 0, 1, -math.Pi/2)
|
||||
g.Scene.Root.AddChildren(plane)
|
||||
const planeSize = 10000
|
||||
|
||||
{
|
||||
plane := tetra3d.NewModel(tetra3d.NewPlaneMesh(2, 2), "plane")
|
||||
plane.Color.Set(40.0/255.0, 40.0/255.0, 40.0/255.0, 1)
|
||||
plane.Grow(planeSize, 0, planeSize)
|
||||
g.Scene.Root.AddChildren(plane)
|
||||
}
|
||||
|
||||
// Create a cube, set the color, add it to the scene.
|
||||
buildingSpacing := 12.0
|
||||
buildingScale := 2.5
|
||||
buildingSize := 7.0
|
||||
|
||||
for x := 0; x < 10; x++ {
|
||||
for y := 0; y < 10; y++ {
|
||||
cube := tetra3d.NewModel(tetra3d.NewCubeMesh(), "")
|
||||
cube.Color.Set(117.0/255.0, 79.0/255.0, 8.0/255.0, 1)
|
||||
cube.Move(float64(x)*buildingSpacing, 0, float64(y)*buildingSpacing)
|
||||
cube.Grow(2, 8, 2)
|
||||
g.Scene.Root.AddChildren(cube)
|
||||
addPlane := func(x float64, y float64, z float64, colorR float32, colorG float32, colorB float32) *tetra3d.Model {
|
||||
if colorR == -1 {
|
||||
for {
|
||||
colorR, colorG, colorB = float32(rand.Intn(256))/255.0, float32(rand.Intn(256))/255.0, float32(rand.Intn(256))/255.0
|
||||
if colorR >= 0.5 || colorG >= 0.5 || colorB >= 0.5 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p := tetra3d.NewPlaneMesh(4, 4)
|
||||
plane := tetra3d.NewModel(p, "")
|
||||
plane.Color.Set(colorR, colorG, colorB, 1)
|
||||
plane.Move(x, y, z)
|
||||
//plane.Move(0, 0, +buildingSize/2)
|
||||
|
||||
g.Scene.Root.AddChildren(plane)
|
||||
return plane
|
||||
}
|
||||
|
||||
addCube := func(x float64, y float64, z float64, colorR float32, colorG float32, colorB float32) {
|
||||
if colorR == -1 {
|
||||
colorR, colorG, colorB = float32(rand.Intn(256))/255.0, float32(rand.Intn(256))/255.0, float32(rand.Intn(256))/255.0
|
||||
}
|
||||
|
||||
c := tetra3d.NewCubeMesh()
|
||||
cube := tetra3d.NewModel(c, "")
|
||||
|
||||
cube.Color.Set(colorR, colorG, colorB, 1)
|
||||
|
||||
//cube.Color.Set(117.0/255.0, 79.0/255.0, 8.0/255.0, 1)
|
||||
|
||||
cube.Move(x, y, z)
|
||||
cube.Grow(buildingScale, 7, buildingScale)
|
||||
//cube.SetWorldScale(2, 3.5, 2)
|
||||
cube.Move(0, 12, 0)
|
||||
|
||||
g.Scene.Root.AddChildren(cube)
|
||||
}
|
||||
|
||||
// Add bounding area walls.
|
||||
for x := 0.0; x < 10; x++ {
|
||||
{
|
||||
// Front.
|
||||
plane := addPlane(x*buildingSize, 0, 0, -1, -1, -1)
|
||||
plane.Grow(buildingScale*2.4, 0, buildingScale*10)
|
||||
plane.Move(buildingSize/2, 0, 0)
|
||||
plane.Rotate(1, 0, 0, math.Pi/2)
|
||||
}
|
||||
|
||||
{
|
||||
// Back.
|
||||
//plane := addPlane(x*buildingSize, 0, mapSize*buildingSize, -1, -1, -1)
|
||||
//plane.Rotate(1, 0, 0, -math.Pi/2)
|
||||
}
|
||||
}
|
||||
for y := 0.0; y < 10; y++ {
|
||||
{
|
||||
// Left.
|
||||
plane := addPlane(0, 0, y*buildingSize, -1, -1, -1)
|
||||
plane.Grow(buildingScale*10, 0, buildingScale*2.4)
|
||||
plane.Move(0, 0, buildingSize/2)
|
||||
plane.Rotate(0, 0, 1, -math.Pi/2)
|
||||
}
|
||||
|
||||
{
|
||||
// Right.
|
||||
//plane := addPlane(x*buildingSize, 0, mapSize*buildingSize, -1, -1, -1)
|
||||
//plane.Rotate(1, 0, 0, -math.Pi/2)
|
||||
}
|
||||
}
|
||||
|
||||
buildingSpacing := 10.5
|
||||
_ = buildingSpacing
|
||||
|
||||
// Second set.
|
||||
for y := 0.0; y < 10; y++ {
|
||||
{
|
||||
// Left.
|
||||
}
|
||||
|
||||
{
|
||||
// Right.
|
||||
plane := addPlane(buildingSize*1, 0, (y+1)*buildingSize, -1, -1, -1)
|
||||
plane.Grow(buildingScale*10, 0, buildingScale*2.4)
|
||||
plane.Move(0, 0, buildingSize/2)
|
||||
plane.Rotate(0, 0, 1, math.Pi/2)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
plane := addPlane(buildingSize*1, 0, buildingSize*1, -1, -1, -1)
|
||||
plane.Grow(buildingScale*2.4, 0, buildingScale*10)
|
||||
plane.Move(buildingSize/2, 0, 0)
|
||||
plane.Rotate(1, 0, 0, -math.Pi/2)
|
||||
}
|
||||
{
|
||||
plane := addPlane(buildingSize*2, 0, buildingSize*1, -1, -1, -1)
|
||||
plane.Grow(buildingScale*2.4, 0, buildingScale*10)
|
||||
plane.Move(buildingSize/2, 0, 0)
|
||||
plane.Rotate(1, 0, 0, -math.Pi/2)
|
||||
}
|
||||
{
|
||||
plane := addPlane(buildingSize*3, 0, buildingSize*1, -1, -1, -1)
|
||||
plane.Grow(buildingScale*2.4, 0, buildingScale*10)
|
||||
plane.Move(buildingSize/2, 0, 0)
|
||||
plane.Rotate(1, 0, 0, -math.Pi/2)
|
||||
}
|
||||
|
||||
_ = addCube
|
||||
/* for y := 0.0; y < 10; y++ {
|
||||
addCube(buildingSpacing*1, 0, buildingSpacing*1+y*buildingSize, -1, -1, -1)
|
||||
}
|
||||
*/
|
||||
// Add inner-city buildings.
|
||||
/*for x := 0; x < 10; x++ {
|
||||
for y := 0; y < 10; y++ {
|
||||
addCube(float64(x)*buildingSize, 0, float64(y)*buildingSize, -1, -1, -1)
|
||||
}
|
||||
|
||||
}*/
|
||||
|
||||
// Garbage truck.
|
||||
{
|
||||
mesh := tetra3d.NewCubeMesh()
|
||||
g.playerModel = tetra3d.NewModel(mesh, "")
|
||||
g.playerModel.Color = tetra3d.NewColor(0.12, 0.44, 0.24, 1)
|
||||
g.playerModel.Grow(-0.75, -0.25, -0.3)
|
||||
g.Scene.Root.AddChildren(g.playerModel)
|
||||
}
|
||||
|
||||
// Create a camera, move it back.
|
||||
g.Camera = tetra3d.NewCamera(g.Width, g.Height)
|
||||
g.Camera.Move(0, 14, 0)
|
||||
g.Camera.Move(0, 12, 0)
|
||||
g.Camera.Rotate(1, 0, 0, -math.Pi/2)
|
||||
|
||||
// Again, we don't need to actually add the camera to the scenegraph, but we'll do it anyway because why not.
|
||||
g.Scene.Root.AddChildren(g.Camera)
|
||||
|
||||
}
|
||||
|
@ -136,29 +267,22 @@ func (g *Game) Update() error {
|
|||
//cube := g.Scene.Root.Get("cube")
|
||||
//cube.SetLocalRotation(cube.LocalRotation().Rotated(0, 1, 0, 0.05))
|
||||
|
||||
moveSize := 0.05
|
||||
|
||||
var moving bool
|
||||
const moveSize = 0.02
|
||||
moveX, moveY := 0.0, 0.0
|
||||
if ebiten.IsKeyPressed(ebiten.KeyLeft) || ebiten.IsKeyPressed(ebiten.KeyA) {
|
||||
g.Camera.Move(-moveSize, 0, 0)
|
||||
moving = true
|
||||
moveX -= moveSize
|
||||
}
|
||||
if ebiten.IsKeyPressed(ebiten.KeyRight) || ebiten.IsKeyPressed(ebiten.KeyD) {
|
||||
g.Camera.Move(moveSize, 0, 0)
|
||||
moving = true
|
||||
moveX += moveSize
|
||||
}
|
||||
if ebiten.IsKeyPressed(ebiten.KeyUp) || ebiten.IsKeyPressed(ebiten.KeyW) {
|
||||
g.Camera.Move(0, 0, -moveSize)
|
||||
moving = true
|
||||
moveY -= moveSize
|
||||
}
|
||||
if ebiten.IsKeyPressed(ebiten.KeyDown) || ebiten.IsKeyPressed(ebiten.KeyS) {
|
||||
g.Camera.Move(0, 0, moveSize)
|
||||
moving = true
|
||||
moveY += moveSize
|
||||
}
|
||||
if moving {
|
||||
return nil
|
||||
}
|
||||
|
||||
g.playerModel.Move(moveX, 0, moveY)
|
||||
g.Camera.Move(moveX, 0, moveY)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -175,6 +299,13 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
|||
|
||||
// Draw the resulting color texture.
|
||||
screen.DrawImage(g.Camera.ColorTexture(), nil)
|
||||
|
||||
if g.debug > 0 {
|
||||
// Print twice to increase shadow.
|
||||
for i := 0; i < 2; i++ {
|
||||
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("FPS %.0f\nTPS %.0f", ebiten.ActualFPS(), ebiten.ActualTPS()), 1, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) Layout(w, h int) (int, int) {
|
||||
|
@ -195,6 +326,10 @@ func main() {
|
|||
ebiten.SetCursorMode(ebiten.CursorModeHidden)
|
||||
|
||||
game := NewGame()
|
||||
|
||||
flag.IntVar(&game.debug, "debug", 0, "print debug information")
|
||||
flag.Parse()
|
||||
|
||||
if err := ebiten.RunGame(game); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue