...
This commit is contained in:
parent
b853924e19
commit
e1268acd93
6 changed files with 115 additions and 7 deletions
|
@ -8,6 +8,7 @@ import (
|
|||
"log"
|
||||
"strings"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
|
@ -65,6 +66,7 @@ func NewPlayer() *Player {
|
|||
},
|
||||
Visible: true,
|
||||
Shader: gx.SolidWhiteColorShader,
|
||||
Uniforms: make(map[string] any),
|
||||
},
|
||||
MoveSpeed: 90.,
|
||||
ScaleSpeed: .2,
|
||||
|
@ -85,7 +87,8 @@ func (p *Player) Update(e *gx.Engine) error {
|
|||
dt := e.DT()
|
||||
c := e.Camera()
|
||||
keys := e.Keys()
|
||||
|
||||
|
||||
p.Uniforms["Random"] = any(rand.Float32())
|
||||
for _, v := range keys {switch v {
|
||||
case ebiten.KeyArrowUp :
|
||||
c.T.P.Y += p.MoveSpeed * dt
|
||||
|
|
|
@ -1,4 +1,17 @@
|
|||
package gx
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// The type is used in all Engine interactions
|
||||
// where you need floating values.
|
||||
type Float = float64
|
||||
|
||||
// The type represents order of drawing.
|
||||
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 ShaderOptions struct {
|
||||
Shader *Shader
|
||||
Uniforms map[string] any
|
||||
Images [4]*Image
|
||||
}
|
||||
|
@ -16,6 +17,8 @@ var (
|
|||
SolidWhiteColorShader = MustNewShader([]byte(`
|
||||
package main
|
||||
|
||||
var Random float
|
||||
|
||||
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
||||
//ts := imageSrcTextureSize()
|
||||
|
||||
|
@ -37,14 +40,16 @@ var (
|
|||
)
|
||||
}*/
|
||||
|
||||
ret := vec4(
|
||||
/*ret := vec4(
|
||||
0,
|
||||
sin(position.x),
|
||||
sin(position.y),
|
||||
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?
|
||||
type Triangle [3]Point
|
||||
type Triangles []Triangle
|
||||
type DrawableTriangle struct {
|
||||
Triangle
|
||||
ShaderOptions
|
||||
}
|
||||
|
||||
// Returns the area of the triangle.
|
||||
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)
|
||||
}
|
||||
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