...
This commit is contained in:
parent
b853924e19
commit
e1268acd93
6 changed files with 115 additions and 7 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Player struct {
|
type Player struct {
|
||||||
|
@ -65,6 +66,7 @@ func NewPlayer() *Player {
|
||||||
},
|
},
|
||||||
Visible: true,
|
Visible: true,
|
||||||
Shader: gx.SolidWhiteColorShader,
|
Shader: gx.SolidWhiteColorShader,
|
||||||
|
Uniforms: make(map[string] any),
|
||||||
},
|
},
|
||||||
MoveSpeed: 90.,
|
MoveSpeed: 90.,
|
||||||
ScaleSpeed: .2,
|
ScaleSpeed: .2,
|
||||||
|
@ -86,6 +88,7 @@ func (p *Player) Update(e *gx.Engine) error {
|
||||||
c := e.Camera()
|
c := e.Camera()
|
||||||
keys := e.Keys()
|
keys := e.Keys()
|
||||||
|
|
||||||
|
p.Uniforms["Random"] = any(rand.Float32())
|
||||||
for _, v := range keys {switch v {
|
for _, v := range keys {switch v {
|
||||||
case ebiten.KeyArrowUp :
|
case ebiten.KeyArrowUp :
|
||||||
c.T.P.Y += p.MoveSpeed * dt
|
c.T.P.Y += p.MoveSpeed * dt
|
||||||
|
|
|
@ -1,4 +1,17 @@
|
||||||
package gx
|
package gx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
type LineSegment [2]Point
|
type LineSegment [2]Point
|
||||||
|
|
||||||
|
func (ls LineSegment) LenSqr() Float {
|
||||||
|
return Sqr(ls[0].X - ls[1].X) +
|
||||||
|
Sqr(ls[0].Y - ls[1].Y)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ls LineSegment) Len() Float {
|
||||||
|
return Sqrt(ls.LenSqr())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,10 +10,6 @@ import (
|
||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The type is used in all Engine interactions
|
|
||||||
// where you need floating values.
|
|
||||||
type Float = float64
|
|
||||||
|
|
||||||
// The type represents order of drawing.
|
// The type represents order of drawing.
|
||||||
type Layer int
|
type Layer int
|
||||||
|
|
||||||
|
|
15
src/gx/math.go
Normal file
15
src/gx/math.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package gx
|
||||||
|
|
||||||
|
// The type is used in all Engine interactions
|
||||||
|
// where you need floating values.
|
||||||
|
type Float = float64
|
||||||
|
|
||||||
|
const (
|
||||||
|
MaxFloat = math.MaxFloat64
|
||||||
|
)
|
||||||
|
|
||||||
|
// Returns square of the value.
|
||||||
|
func Sqr(v Float) Float {
|
||||||
|
return v * v
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
|
|
||||||
type Shader = ebiten.Shader
|
type Shader = ebiten.Shader
|
||||||
type ShaderOptions struct {
|
type ShaderOptions struct {
|
||||||
|
Shader *Shader
|
||||||
Uniforms map[string] any
|
Uniforms map[string] any
|
||||||
Images [4]*Image
|
Images [4]*Image
|
||||||
}
|
}
|
||||||
|
@ -16,6 +17,8 @@ var (
|
||||||
SolidWhiteColorShader = MustNewShader([]byte(`
|
SolidWhiteColorShader = MustNewShader([]byte(`
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
var Random float
|
||||||
|
|
||||||
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
||||||
//ts := imageSrcTextureSize()
|
//ts := imageSrcTextureSize()
|
||||||
|
|
||||||
|
@ -37,14 +40,16 @@ var (
|
||||||
)
|
)
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
ret := vec4(
|
/*ret := vec4(
|
||||||
0,
|
0,
|
||||||
sin(position.x),
|
sin(position.x),
|
||||||
sin(position.y),
|
sin(position.y),
|
||||||
1,
|
1,
|
||||||
)
|
)
|
||||||
|
|
||||||
return imageSrc0UnsafeAt(texCoord) * (ret)
|
return imageSrc0UnsafeAt(texCoord) * (ret)*/
|
||||||
|
return imageSrc0UnsafeAt(texCoord) *
|
||||||
|
vec4(1, 1, Random, Random)
|
||||||
}
|
}
|
||||||
`))
|
`))
|
||||||
)
|
)
|
||||||
|
|
|
@ -7,6 +7,10 @@ import (
|
||||||
// The structure of a triangle. What more you want to hear?
|
// The structure of a triangle. What more you want to hear?
|
||||||
type Triangle [3]Point
|
type Triangle [3]Point
|
||||||
type Triangles []Triangle
|
type Triangles []Triangle
|
||||||
|
type DrawableTriangle struct {
|
||||||
|
Triangle
|
||||||
|
ShaderOptions
|
||||||
|
}
|
||||||
|
|
||||||
// Returns the area of the triangle.
|
// Returns the area of the triangle.
|
||||||
func (t Triangle) Area() Float {
|
func (t Triangle) Area() Float {
|
||||||
|
@ -21,4 +25,76 @@ func (t Triangle) Area() Float {
|
||||||
|
|
||||||
return math.Abs( (x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2)
|
return math.Abs( (x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2)
|
||||||
}
|
}
|
||||||
|
func sqr(v Float) Float {
|
||||||
|
return v * v
|
||||||
|
}
|
||||||
|
// Return squares of lengths of sides of the triangle.
|
||||||
|
func (t Triangle) SideLengthSquares() ([3]Float) {
|
||||||
|
|
||||||
|
l1 := LineSegment{t[0], t[1]}.LenSqr()
|
||||||
|
l2 := LineSegment{t[1], t[2]}.LenSqr()
|
||||||
|
l2 := LineSegment{t[2], t[0]}.LenSqr()
|
||||||
|
|
||||||
|
return [3]Float{l1, l2, l3}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether the point is in the triangle.
|
||||||
|
func (t Triangle) PointIsIn(p Point) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
func (r *DrawableRectangle) Draw(
|
||||||
|
e *Engine,
|
||||||
|
i *Image,
|
||||||
|
) {
|
||||||
|
t := r.T
|
||||||
|
|
||||||
|
// Draw solid color if no shader.
|
||||||
|
if r.Shader == nil {
|
||||||
|
t.S.X *= r.W
|
||||||
|
t.S.Y *= r.H
|
||||||
|
|
||||||
|
m := t.Matrix(e)
|
||||||
|
rm := e.Camera().RealMatrix(e, true)
|
||||||
|
|
||||||
|
m.Concat(rm)
|
||||||
|
|
||||||
|
opts := &ebiten.DrawImageOptions{
|
||||||
|
GeoM: m,
|
||||||
|
}
|
||||||
|
i.DrawTriangles(img, opts)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the Color as base image if no is provided.
|
||||||
|
var did bool
|
||||||
|
if r.Images[0] == nil {
|
||||||
|
r.Images[0] = NewImage(1, 1)
|
||||||
|
r.Images[0].Set(0, 0, r.Color)
|
||||||
|
did = true
|
||||||
|
}
|
||||||
|
|
||||||
|
w, h := r.Images[0].Size()
|
||||||
|
if !did {
|
||||||
|
t.S.X /= Float(w)
|
||||||
|
t.S.Y /= Float(h)
|
||||||
|
|
||||||
|
t.S.X *= r.W
|
||||||
|
t.S.Y *= r.H
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
rm := e.Camera().RealMatrix(e, true)
|
||||||
|
m := t.Matrix(e)
|
||||||
|
m.Concat(rm)
|
||||||
|
|
||||||
|
// Drawing with shader.
|
||||||
|
opts := &ebiten.DrawRectShaderOptions{
|
||||||
|
GeoM: m,
|
||||||
|
Images: r.Images,
|
||||||
|
Uniforms: r.Uniforms,
|
||||||
|
}
|
||||||
|
i.DrawRectShader(w, h, r.Shader, opts)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue