Skip to content

Commit

Permalink
Mobile movement
Browse files Browse the repository at this point in the history
  • Loading branch information
m110 committed Nov 2, 2024
1 parent 8dd1df4 commit 2e197b2
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tasks:
mobile-deploy:
deps:
- mobile
dir: ./mobile
dir: ./mobile/Airplanes
cmds:
- xcodebuild -scheme Airplanes -configuration Debug -sdk iphoneos -derivedDataPath build
- ios-deploy --bundle build/Build/Products/Debug-iphoneos/Airplanes.app --nostart --no-wifi
6 changes: 5 additions & 1 deletion archetype/joystick.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"github.com/m110/airplanes/component"
)

func NewJoystick(w donburi.World, pos math.Vec2) {
func NewJoystick(w donburi.World, pos math.Vec2) *donburi.Entry {
joystick := w.Entry(w.Create(
transform.Transform,
component.Joystick,
component.Sprite,
component.UI,
))
component.Sprite.SetValue(joystick, component.SpriteData{
Image: assets.JoystickBase,
Expand All @@ -28,6 +29,7 @@ func NewJoystick(w donburi.World, pos math.Vec2) {
transform.Transform,
component.Joystick,
component.Sprite,
component.UI,
))

component.Sprite.SetValue(knob, component.SpriteData{
Expand All @@ -36,4 +38,6 @@ func NewJoystick(w donburi.World, pos math.Vec2) {
Pivot: component.SpritePivotCenter,
})
transform.AppendChild(joystick, knob, false)

return joystick
}
10 changes: 5 additions & 5 deletions archetype/wreck.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
func NewAirplaneWreck(w donburi.World, parent *donburi.Entry, sprite *component.SpriteData) {
widthInt, heightInt := sprite.Image.Size()
width, height := float64(widthInt), float64(heightInt)
cutpointX := float64(int(width * engine.RandomRange(0.3, 0.7)))
cutpointY := float64(int(height * engine.RandomRange(0.3, 0.7)))
cutpointX := float64(int(width * engine.RandomFloatRange(0.3, 0.7)))
cutpointY := float64(int(height * engine.RandomFloatRange(0.3, 0.7)))

pieces := []engine.Rect{
{
Expand Down Expand Up @@ -86,12 +86,12 @@ func NewAirplaneWreck(w donburi.World, parent *donburi.Entry, sprite *component.
})

velocity := transform.Right(parent)
velocity.X *= engine.RandomRange(0.5, 0.8)
velocity.Y *= engine.RandomRange(0.5, 0.8)
velocity.X *= engine.RandomFloatRange(0.5, 0.8)
velocity.Y *= engine.RandomFloatRange(0.5, 0.8)

component.Velocity.SetValue(wreck, component.VelocityData{
Velocity: velocity,
RotationVelocity: engine.RandomRange(-2, 2),
RotationVelocity: engine.RandomFloatRange(-2, 2),
})

component.Altitude.SetValue(wreck, component.AltitudeData{
Expand Down
2 changes: 2 additions & 0 deletions component/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package component
import "github.com/yohamta/donburi"

var (
UI = donburi.NewTag()

ShadowTag = donburi.NewTag()

CurrentEvolutionTag = donburi.NewTag()
Expand Down
51 changes: 51 additions & 0 deletions engine/find.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package engine

import (
"fmt"
"github.com/yohamta/donburi"
"github.com/yohamta/donburi/component"
"github.com/yohamta/donburi/features/transform"
"github.com/yohamta/donburi/filter"
)

func MustGetParent(entry *donburi.Entry) *donburi.Entry {
parent, ok := transform.GetParent(entry)
if !ok {
panic("parent not found")
}
return parent
}

func MustFindChildWithComponent(parent *donburi.Entry, componentType component.IComponentType) *donburi.Entry {
entry, ok := transform.FindChildWithComponent(parent, componentType)
if !ok {
panic(fmt.Sprintf("entry not found with component %T", componentType))
}
return entry
}

func FindWithComponent(w donburi.World, componentType component.IComponentType) (*donburi.Entry, bool) {
return donburi.NewQuery(filter.Contains(componentType)).First(w)
}

func MustFindWithComponent(w donburi.World, componentType component.IComponentType) *donburi.Entry {
entry, ok := FindWithComponent(w, componentType)
if !ok {
panic(fmt.Sprintf("entry not found with component %T", componentType))
}
return entry
}

type Component[T any] interface {
donburi.IComponentType
Get(entry *donburi.Entry) *T
}

func MustFindComponent[T any](w donburi.World, c Component[T]) *T {
entry, ok := donburi.NewQuery(filter.Contains(c)).First(w)
if !ok {
panic("MustFindComponent: entry not found")
}

return c.Get(entry)
}
33 changes: 33 additions & 0 deletions engine/hierarchy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package engine

import (
"github.com/yohamta/donburi"
"github.com/yohamta/donburi/component"
"github.com/yohamta/donburi/features/transform"
)

func FindChildrenWithComponent(e *donburi.Entry, c component.IComponentType) []*donburi.Entry {
if !e.Valid() {
return nil
}

children, ok := transform.GetChildren(e)
if !ok {
return nil
}

var result []*donburi.Entry
for _, child := range children {
if !child.Valid() {
continue
}

if child.HasComponent(c) {
result = append(result, child)
}

result = append(result, FindChildrenWithComponent(child, c)...)
}

return result
}
19 changes: 18 additions & 1 deletion engine/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@ package engine

import "math/rand"

func RandomRange(min, max float64) float64 {
func RandomFloatRange(min, max float64) float64 {
return min + rand.Float64()*(max-min)
}

func RandomIntRange(min, max int) int {
return min + rand.Intn(max-min+1)
}

func RandomFrom[T comparable](list []T) T {
index := rand.Intn(len(list))
return list[index]
}

func RandomFromOrEmpty[T comparable](list []T) *T {
index := rand.Intn(len(list) + 1)
if index == len(list) {
return nil
}
return &list[index]
}
30 changes: 30 additions & 0 deletions engine/range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package engine

import "time"

type IntRange struct {
Min int
Max int
}

func (r IntRange) Random() int {
return RandomIntRange(r.Min, r.Max)
}

type FloatRange struct {
Min float64
Max float64
}

func (r FloatRange) Random() float64 {
return RandomFloatRange(r.Min, r.Max)
}

type DurationRange struct {
Min time.Duration
Max time.Duration
}

func (r DurationRange) Random() time.Duration {
return time.Duration(RandomIntRange(int(r.Min), int(r.Max)))
}
5 changes: 2 additions & 3 deletions game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func (g *Game) Draw(screen *ebiten.Image) {

func (g *Game) Layout(width, height int) (int, int) {
if g.screenWidth == 0 || g.screenHeight == 0 {
g.screenWidth = width
g.screenHeight = height
return width, height
}
return width, height
return g.screenWidth, g.screenHeight
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
config := game.Config{
Quick: *quickFlag,
ScreenWidth: 480,
ScreenHeight: 640,
ScreenHeight: 800,
}

ebiten.SetWindowSize(config.ScreenWidth, config.ScreenHeight)
Expand Down
2 changes: 2 additions & 0 deletions mobile/Airplanes/Airplanes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@
INFOPLIST_KEY_UIStatusBarHidden = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
IPHONEOS_DEPLOYMENT_TARGET = 18.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -445,6 +446,7 @@
INFOPLIST_KEY_UIStatusBarHidden = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
IPHONEOS_DEPLOYMENT_TARGET = 18.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
Binary file not shown.
6 changes: 5 additions & 1 deletion mobile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
)

func init() {
mobile.SetGame(game.NewGame(game.Config{}))
mobile.SetGame(game.NewGame(game.Config{
Quick: true,
ScreenWidth: 480,
ScreenHeight: 1040,
}))
}

func Dummy() {}
Loading

0 comments on commit 2e197b2

Please sign in to comment.