-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.go
106 lines (90 loc) · 2.01 KB
/
engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"github.com/ByteArena/box2d"
"github.com/hajimehoshi/ebiten/v2"
"math"
)
type EngineDef struct {
Dir Direction
Power float64
Keys Keys
Size float64
}
type Engine struct {
*GameObj
tanker Tank
ps *ParticleSystem
power float64
keys Keys
isActive bool
}
func (d EngineDef) Construct(
world *box2d.B2World,
tanker Tank,
ps *ParticleSystem,
shipPos box2d.B2Vec2,
shipSize box2d.B2Vec2,
pos box2d.B2Vec2) Part {
// TODO: duplicate in basic_part
shipHalfSize := box2d.B2Vec2MulScalar(0.5, shipSize)
worldPos := box2d.B2Vec2Add(shipPos, pos)
worldPos = box2d.B2Vec2Add(worldPos, shipHalfSize.OperatorNegate())
worldPos = box2d.B2Vec2Add(worldPos, box2d.MakeB2Vec2(0.5, 0.5))
sprite := engineSprite.Scale(d.Size, d.Dir)
gameObj := NewGameObj(
world,
sprite,
worldPos,
d.Dir.GetAng(),
0,
box2d.B2Vec2_zero,
DefaultFriction, DefaultFixtureDensity, DefaultFixtureRestitution, true)
engine := &Engine{
GameObj: gameObj,
tanker: tanker,
power: d.Power,
ps: ps,
keys: d.Keys,
}
engine.GetBody().SetUserData(engine)
return engine
}
func (e *Engine) Draw(screen *ebiten.Image, cam Cam) {
e.GameObj.Draw(screen, cam)
if !e.isActive {
return
}
// TODO: fix emit for small engines
// Flame particles
pos := box2d.B2Vec2Add(
e.GetPos(),
box2d.B2RotVec2Mul(*box2d.NewB2RotFromAngle(e.GetAng()), box2d.MakeB2Vec2(0.5, 0)))
e.ps.
Emit(pos, e.GetAng(), math.Pi/4)
}
func (e *Engine) GetBody() *box2d.B2Body {
return e.body
}
func (e *Engine) Update(keys Keys) {
e.isActive = false
if e.tanker.GetFuel() <= 0 {
return
}
// TODO: to func
keyFound := false
for key := range keys {
if e.keys.IsPressed(key) {
keyFound = true
break
}
}
if !keyFound {
return
}
e.isActive = true
rot := box2d.NewB2RotFromAngle(e.GetAng())
force := box2d.B2RotVec2Mul(*rot, box2d.MakeB2Vec2(e.power, 0))
force = force.OperatorNegate()
e.body.ApplyForce(force, e.body.GetPosition(), true)
e.tanker.ReduceFuel(e.power * EngineFuelConsumption)
}