56 main.go
@@ -1,43 +1,31 @@
package main

import (
"github.com/go-gl/glfw/v3.2/glfw"
"runtime"
"engo.io/engo"
"github.com/Yautil/openorbit/scenes"
)

const (
width = 500
height = 500
var (
GlobalOpts = engo.RunOptions{
NoRun: false,
Title: "MyOpenOrbit",
HeadlessMode: false,
Fullscreen: false,
Width: 1000,
Height: 600,
VSync: false,
NotResizable: false,
ScaleOnResize: false,
FPSLimit: 0,
OverrideCloseAction: false,
StandardInputs: false,
MSAA: 1,
AssetsRoot: "",
MobileWidth: 0,
MobileHeight: 0,
}
)

func main() {
runtime.LockOSThread()

window := initGlfw()
defer glfw.Terminate()

for !window.ShouldClose() {
// TODO
}
}

// initGlfw initializes glfw and returns a Window to use.
func initGlfw() *glfw.Window {
if err := glfw.Init(); err != nil {
panic(err)
}

glfw.WindowHint(glfw.Resizable, glfw.False)
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)

window, err := glfw.CreateWindow(width, height, "title", nil, nil)
if err != nil {
panic(err)
}
window.MakeContextCurrent()

return window
engo.Run(GlobalOpts, &scenes.TestScene{})
}
@@ -0,0 +1,73 @@
package scenes

import (
"engo.io/ecs"
"engo.io/engo"
"engo.io/engo/common"
"github.com/Yautil/engoBox2dSystem"
"github.com/Yautil/openorbit/entities"
"github.com/Yautil/openorbit/systems"
"image/color"
)

type TestScene struct {
}

// Type uniquely defines your game type
func (*TestScene) Type() string { return "TestScene" }

// Preload is called before loading any assets from the disk,
// to allow you to register / queue them
func (*TestScene) Preload() {
engo.Files.Load("textures/asteroid.png")
engo.Files.Load("textures/spaceship.png")
engo.Files.Load("textures/star.png")
}

// Setup is called before the main loop starts. It allows you
// to add entities and systems to your Scene.
func (*TestScene) Setup(u engo.Updater) {
// Config Scene World
world, _ := u.(*ecs.World)
common.SetBackground(color.Black)

world.AddSystem(&common.RenderSystem{})
world.AddSystem(&systems.SpeedInterpolationSystem{})
world.AddSystem(&systems.KeyboardMovementSystem{})
world.AddSystem(&systems.LocalPlayerSystem{})
world.AddSystem(&engoBox2dSystem.CollisionSystem{})
world.AddSystem(&engoBox2dSystem.PhysicsSystem{})

// Config Asteroid
asteroid := entities.Star32x32Test{
Name: "Asteroid",
Description: "uff",
Texture: "asteroid.png",
Width: 64,
Height: 64,
World: world,
BasicEntity: ecs.NewBasic(),
}
asteroid.New(engo.Point{100, 200})

star := entities.Star32x32Test{
Name: "Star",
Description: "",
Texture: "star.png",
Width: 32,
Height: 32,
World: world,
}
star.New(engo.Point{300, 300})

player := entities.LocalPlayer64x64Test{
Name: "Player 1",
Description: "",
Texture: "spaceship.png",
Width: 64,
Height: 64,
World: world,
BasicEntity: ecs.NewBasic(),
}
player.New(engo.Point{engo.GameWidth()/2 - float32(entities.LocalPlayer64x64Test{}.Width/2), engo.GameHeight()/2 - float32(entities.LocalPlayer64x64Test{}.Height/2)})
}
@@ -0,0 +1 @@
package systems
@@ -0,0 +1,84 @@
package systems

import (
"engo.io/ecs"
"engo.io/engo"
"engo.io/engo/common"
"github.com/ByteArena/box2d"
"github.com/Yautil/engoBox2dSystem"
"github.com/Yautil/openorbit/components"
)

type KeyboardMovementEntity struct {
*ecs.BasicEntity
*common.SpaceComponent
*engoBox2dSystem.Box2dComponent
*components.SpeedInterpolationComponent
}

type KeyboardMovementSystem struct {
entities []KeyboardMovementEntity
world *ecs.World
}

func (s *KeyboardMovementSystem) Add(basic *ecs.BasicEntity, space *common.SpaceComponent, box *engoBox2dSystem.Box2dComponent, speed *components.SpeedInterpolationComponent) {
s.entities = append(s.entities, KeyboardMovementEntity{basic, space, box, speed})
}

// Remove removes the entity from the system
func (s *KeyboardMovementSystem) Remove(basic ecs.BasicEntity) {
delete := -1
for index, e := range s.entities {
if e.BasicEntity.ID() == basic.ID() {
delete = index
break
}
}
if delete >= 0 {
s.entities = append(s.entities[:delete], s.entities[delete+1:]...)
}
}

func (s *KeyboardMovementSystem) New(w *ecs.World) {
engo.Input.RegisterAxis(
"KeyboardMovementSystemVerticalAxis",
engo.AxisKeyPair{engo.KeyArrowUp, engo.KeyArrowDown},
engo.AxisKeyPair{engo.KeyW, engo.KeyS},
)

engo.Input.RegisterAxis(
"KeyboardMovementSystemHorizontalAxis",
engo.AxisKeyPair{engo.KeyArrowLeft, engo.KeyArrowRight},
engo.AxisKeyPair{engo.KeyA, engo.KeyD},
)
}

func (s *KeyboardMovementSystem) Update(dt float32) {
for _, e := range s.entities {
if e.SpaceComponent.Position.Y+e.SpaceComponent.Height > engo.GameHeight() {
if engo.Input.Axis("KeyboardMovementSystemVerticalAxis").Value() > 0 {
return
}
}

if e.SpaceComponent.Position.X+e.SpaceComponent.Width > engo.GameWidth() {
if engo.Input.Axis("KeyboardMovementSystemHorizontalAxis").Value() > 0 {
return
}
}
hori := engo.Input.Axis("KeyboardMovementSystemHorizontalAxis")
//e.SpaceComponent.Position.X += e.Speed * hori.Value()

vert := engo.Input.Axis("KeyboardMovementSystemVerticalAxis")
//e.SpaceComponent.Position.Y += e.Speed * vert.Value()
if hori.Value() != 0 || vert.Value() != 0 {
e.SpeedInterpolationComponent.Moving = true
} else {
e.SpeedInterpolationComponent.Moving = false
//continue
}
//e.Body.ApplyLinearImpulseToCenter(box2d.B2Vec2{X: float64(e.SpeedInterpolationComponent.CurSpeed * hori.Value()), Y: float64(e.SpeedInterpolationComponent.CurSpeed * vert.Value())}, true)
e.Body.SetLinearVelocity(box2d.B2Vec2{X: float64(e.SpeedInterpolationComponent.CurSpeed * hori.Value()), Y: float64(e.SpeedInterpolationComponent.CurSpeed * vert.Value())})

}
}
@@ -0,0 +1,66 @@
package systems

import (
"engo.io/ecs"
"engo.io/engo"
"engo.io/engo/common"
"github.com/Yautil/engoBox2dSystem"
"log"
)

type LocalPlayerEntity struct {
*ecs.BasicEntity
*common.SpaceComponent
name string
}

type LocalPlayerSystem struct {
entities []LocalPlayerEntity
world *ecs.World
score int
}

func (s *LocalPlayerSystem) Add(basic *ecs.BasicEntity, space *common.SpaceComponent, name string) {
s.entities = append(s.entities, LocalPlayerEntity{basic, space, name})
}

// Remove removes the entity from the system
func (s *LocalPlayerSystem) Remove(basic ecs.BasicEntity) {
delete := -1
for index, e := range s.entities {
if e.BasicEntity.ID() == basic.ID() {
delete = index
break
}
}
if delete >= 0 {
s.entities = append(s.entities[:delete], s.entities[delete+1:]...)
}
}

func (s *LocalPlayerSystem) New(w *ecs.World) {

engo.Mailbox.Listen("CollisionStartMessage", func(message engo.Message) {
for _, e := range s.entities {
c, isCollision := message.(engoBox2dSystem.CollisionStartMessage)
if isCollision {
if c.Contact.IsTouching() {
a := c.Contact.GetFixtureA().GetBody().M_userData
b := c.Contact.GetFixtureB().GetBody().GetUserData()
log.Println(e.name)
if a == e.name || b == e.name {
if a == "Asteroid" || b == "Asteroid" {
log.Println("DIED")
}
if a == "Star" || b == "Star" {
log.Println("SCORED")
}
}
}
}
}
})
}

func (s *LocalPlayerSystem) Update(dt float32) {
}
@@ -0,0 +1 @@
package systems
@@ -0,0 +1,56 @@
package systems

import (
"engo.io/ecs"
"github.com/Yautil/openorbit/components"
)

type SpeedInterpolationEntity struct {
*ecs.BasicEntity
*components.SpeedInterpolationComponent
}

type SpeedInterpolationSystem struct {
entities []SpeedInterpolationEntity
world *ecs.World
}

func (s *SpeedInterpolationSystem) Add(basic *ecs.BasicEntity, speed *components.SpeedInterpolationComponent) {
s.entities = append(s.entities, SpeedInterpolationEntity{basic, speed})
}

// Remove removes the entity from the system
func (s *SpeedInterpolationSystem) Remove(basic ecs.BasicEntity) {
delete := -1
for index, e := range s.entities {
if e.BasicEntity.ID() == basic.ID() {
delete = index
break
}
}
if delete >= 0 {
s.entities = append(s.entities[:delete], s.entities[delete+1:]...)
}
}

func (s *SpeedInterpolationSystem) New(w *ecs.World) {

}

func (s *SpeedInterpolationSystem) Update(dt float32) {
for _, e := range s.entities {
if e.SpeedInterpolationComponent.Moving {
if e.SpeedInterpolationComponent.CurSpeed < e.SpeedInterpolationComponent.MaxSpeed {
e.SpeedInterpolationComponent.CurSpeed = e.SpeedInterpolationComponent.CurSpeed + e.SpeedInterpolationComponent.InterpolationDelta
}
if e.SpeedInterpolationComponent.CurSpeed > e.SpeedInterpolationComponent.MaxSpeed {
e.SpeedInterpolationComponent.CurSpeed = e.SpeedInterpolationComponent.MaxSpeed
}
} else if e.SpeedInterpolationComponent.CurSpeed > 0 {
e.SpeedInterpolationComponent.CurSpeed = e.SpeedInterpolationComponent.CurSpeed - e.SpeedInterpolationComponent.InterpolationDelta*e.SpeedInterpolationComponent.SlowdownMultiplier
}
if e.SpeedInterpolationComponent.CurSpeed < 0 {
e.SpeedInterpolationComponent.CurSpeed = 0
}
}
}