2021-08-09 01:08:28 +00:00
|
|
|
package simulation
|
2021-08-09 01:06:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-10-23 05:10:40 +00:00
|
|
|
"github.com/charmbracelet/harmonica"
|
2021-08-09 01:06:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type System struct {
|
|
|
|
Frame Frame
|
|
|
|
Particles []Particle
|
|
|
|
}
|
|
|
|
|
|
|
|
type Particle struct {
|
|
|
|
Char string
|
2021-10-23 05:10:40 +00:00
|
|
|
Physics *harmonica.Projectile
|
2021-08-09 01:06:10 +00:00
|
|
|
Hidden bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Frame struct {
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
}
|
|
|
|
|
2021-10-31 17:58:01 +00:00
|
|
|
func RemoveParticleFromArray(s []Particle, i int) []Particle {
|
|
|
|
s[i] = s[len(s)-1]
|
|
|
|
return s[:len(s)-1]
|
|
|
|
}
|
|
|
|
|
2021-08-09 01:06:10 +00:00
|
|
|
func (s *System) Update() {
|
|
|
|
for _, p := range s.Particles {
|
|
|
|
p.Physics.Update()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *System) Visible(p Particle) bool {
|
2021-10-23 05:10:40 +00:00
|
|
|
y := int(p.Physics.Position().Y)
|
|
|
|
x := int(p.Physics.Position().X)
|
2021-08-09 01:06:10 +00:00
|
|
|
return y >= 0 && y < s.Frame.Height-1 && x >= 0 && x < s.Frame.Width-1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *System) Render() string {
|
|
|
|
var out strings.Builder
|
|
|
|
plane := make([][]string, s.Frame.Height)
|
|
|
|
for i := range plane {
|
|
|
|
plane[i] = make([]string, s.Frame.Width)
|
|
|
|
}
|
|
|
|
for _, p := range s.Particles {
|
|
|
|
if s.Visible(p) {
|
2021-10-23 05:10:40 +00:00
|
|
|
plane[int(p.Physics.Position().Y)][int(p.Physics.Position().X)] = p.Char
|
2021-08-09 01:06:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := range plane {
|
|
|
|
for _, col := range plane[i] {
|
|
|
|
if col == "" {
|
|
|
|
fmt.Fprint(&out, " ")
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(&out, col)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprint(&out, "\n")
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|