This commit is contained in:
silva guimaraes 2023-09-13 23:07:09 -03:00
parent 397136b3da
commit be34596f1d

112
main.go
View file

@ -17,25 +17,21 @@ const (
maxSpeed = 0.1
)
type animation int
const (
none animation = iota
shake
)
type enemy struct {
pos rl.Vector3
speed float32
}
type remainingFrames int
type animationFunc func()remainingFrames
type building struct {
pos rl.Vector3
size rl.Vector3
color rl.Color
life int
boundingBox rl.BoundingBox
animation animation
animate animationFunc
}
func draw_plane() {
@ -80,45 +76,79 @@ func newBuilding(x, z float32) *building {
}
}
func draw_buildings(buildings []*building, selected *building) {
for _, building := range buildings {
color := building.color
func drawWireframe(building building) {
increasedSize := rl.Vector3Scale(building.size, 1.05)
if building == selected {
color = rl.RayWhite
invertedColor := building.color
invertedColor.R += 127
invertedColor.G += 127
invertedColor.B += 127
rl.DrawCubeWiresV(
building.pos,
increasedSize,
invertedColor,
)
}
func shakeBuilding(b building) animationFunc {
var frame remainingFrames = 60 * 0.7
return func() remainingFrames {
building := b
if frame % 4 == 0 {
building.color = rl.RayWhite
}
increasedSize := building.size
increasedSize.X += 0.1
increasedSize.Y += 0.1
increasedSize.Z += 0.1
drawWireframe(building)
invertedColor := color
invertedColor.R += 127
invertedColor.G += 127
invertedColor.B += 127
rl.DrawCubeWiresV(
building.pos,
increasedSize,
invertedColor,
)
shake := rl.NewVector3(
rand.Float32() * .1 - .05,
rand.Float32() * .01 - .005,
rand.Float32() * .1 - .05,
rand.Float32() * .2 - .1,
rand.Float32() * .1 - .05,
rand.Float32() * .2 - .1,
)
shake = rl.Vector3Scale(shake, float32(math.Sin(rl.GetTime()) + 1)/3)
// shake = rl.Vector3Scale(shake, float32(math.Sin(rl.GetTime()) + 1)/3)
rl.DrawCubeV(
rl.Vector3Add(building.pos, shake),
building.size,
color,
building.color,
)
frame -= 1
return frame
}
}
func mouse_select_building(camera rl.Camera, buildings []*building) *building {
func draw_buildings(buildings []*building, selected *building) {
for _, building := range buildings {
hasAnimation := building.animate != nil
hit := building == selected && !hasAnimation
if !hit {
building.animate = shakeBuilding(*building)
}
if hasAnimation {
remainingFrames := building.animate()
if remainingFrames == 0 {
building.animate = nil
}
continue
}
drawWireframe(*building)
rl.DrawCubeV(building.pos, building.size, building.color)
}
}
func demage_building(camera rl.Camera, buildings []*building) *building {
if !rl.IsMouseButtonPressed(rl.MouseLeftButton) {
return nil
}
@ -187,13 +217,13 @@ func main() {
for !rl.WindowShouldClose() {
shake := rl.NewVector3(
rand.Float32() * .4 - .2,
rand.Float32() * .4 - .2,
rand.Float32() * .4 - .2,
)
shake = rl.Vector3Scale(shake, float32(math.Sin(rl.GetTime()) + 1)/3)
camera.Target = rl.Vector3Add(camera.Target, shake)
// shake := rl.NewVector3(
// rand.Float32() * .4 - .2,
// rand.Float32() * .4 - .2,
// rand.Float32() * .4 - .2,
// )
// shake = rl.Vector3Scale(shake, float32(math.Sin(rl.GetTime()) + 1)/3)
// camera.Target = rl.Vector3Add(camera.Target, shake)
rl.UpdateCamera(&camera, rl.CameraFirstPerson)
rl.BeginDrawing()
@ -202,7 +232,7 @@ func main() {
rl.BeginMode3D(camera)
{
selected := mouse_select_building(camera, buildings)
selected := demage_building(camera, buildings)
draw_plane()