-
Notifications
You must be signed in to change notification settings - Fork 30
/
levelstyle.go
80 lines (61 loc) · 2.61 KB
/
levelstyle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2017 Daniel Salvadori. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"github.com/g3n/engine/geometry"
"github.com/g3n/engine/graphic"
"github.com/g3n/engine/material"
"github.com/g3n/engine/math32"
"github.com/g3n/engine/texture"
)
// LevelStyle contains all the level styling information and functions
type LevelStyle struct {
boxLightColorOn *math32.Color
boxLightColorOff *math32.Color
blockMaterial *material.Standard
boxMaterialRed *material.Standard
boxMaterialGreen *material.Standard
padMaterial *material.Standard
elevatorMaterial *material.Standard
makeBlock func() *graphic.Mesh
makeRedBox func() *graphic.Mesh
makeGreenBox func() *graphic.Mesh
makeElevator func() *graphic.Mesh
}
// NewStandardStyle returns a pointer to a LevelStyle object with standard values
func NewStandardStyle() *LevelStyle {
s := new(LevelStyle)
s.boxLightColorOn = &math32.Color{0, 1, 0} // green
s.boxLightColorOff = &math32.Color{1, 0, 0} // red
// Helper function to load texture and handle errors
newTexture := func(path string) *texture.Texture2D {
tex, err := texture.NewTexture2DFromImage(path)
if err != nil {
log.Fatal("Error loading texture: %s", err)
}
return tex
}
// Load textures and create materials
s.blockMaterial = material.NewStandard(math32.NewColor("white"))
s.blockMaterial.AddTexture(newTexture("./img/floor.png"))
s.padMaterial = material.NewStandard(math32.NewColor("white"))
s.padMaterial.AddTexture(newTexture("./img/pad.png"))
s.padMaterial.SetTransparent(true) // Makes this material be displayed in front of blockMaterial
s.boxMaterialRed = material.NewStandard(math32.NewColor("white"))
s.boxMaterialRed.AddTexture(newTexture("./img/crate_red.png"))
s.boxMaterialGreen = material.NewStandard(math32.NewColor("white"))
s.boxMaterialGreen.AddTexture(newTexture("./img/crate_green2.png"))
s.elevatorMaterial = material.NewStandard(math32.NewColor("white"))
s.elevatorMaterial.AddTexture(newTexture("./img/metal_diffuse.png"))
// Create functions that return a cube mesh using the provided material, reusing the same cube geometry
sharedCubeGeom := geometry.NewCube(1)
makeCubeWithMaterial := func(mat *material.Standard) func() *graphic.Mesh {
return func() *graphic.Mesh { return graphic.NewMesh(sharedCubeGeom, mat) }
}
s.makeBlock = makeCubeWithMaterial(s.blockMaterial)
s.makeRedBox = makeCubeWithMaterial(s.boxMaterialRed)
s.makeGreenBox = makeCubeWithMaterial(s.boxMaterialGreen)
s.makeElevator = makeCubeWithMaterial(s.elevatorMaterial)
return s
}