-
Notifications
You must be signed in to change notification settings - Fork 1
/
crane.go
170 lines (144 loc) · 3.98 KB
/
crane.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"github.com/ByteArena/box2d"
"github.com/hajimehoshi/ebiten/v2"
"time"
)
type CraneDef struct {
Dir Direction
}
type Crane struct {
*GameObj
chain []*GameObj
jaws *CraneJaws
chainLastControlled time.Time
chainElSize box2d.B2Vec2
}
func (d CraneDef) Construct(
world *box2d.B2World,
_ Tank,
_ *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))
crane := &Crane{
GameObj: NewGameObj(
world,
craneSprite,
worldPos,
d.Dir.GetAng(), 0,
box2d.B2Vec2_zero,
DefaultFriction, DefaultFixtureDensity, DefaultFixtureRestitution, true),
chainElSize: getShapeSize(chainElSprite.vertsSet[0]),
}
crane.GetBody().SetUserData(crane)
crane.unwind()
crane.jaws = NewCraneJaws(crane)
return crane
}
func (c *Crane) Draw(screen *ebiten.Image, cam Cam) {
c.jaws.Draw(screen, cam)
c.GameObj.Draw(screen, cam)
for _, chainEl := range c.chain {
chainEl.Draw(screen, cam)
}
}
func (c *Crane) GetBody() *box2d.B2Body {
return c.body
}
func (c *Crane) Update(keys Keys) {
c.jaws.Update()
if keys.IsPressed(ebiten.KeyTab) {
c.jaws.OpenClose()
}
if keys.IsPressed(ebiten.KeyQ) {
if c.chainLastControlled.Add(time.Second / 5).After(time.Now()) {
return
}
c.chainLastControlled = time.Now()
c.windup()
}
if keys.IsPressed(ebiten.KeyA) {
if c.chainLastControlled.Add(time.Second / 5).After(time.Now()) {
return
}
c.chainLastControlled = time.Now()
c.unwind()
}
}
func (c *Crane) windup() {
if len(c.chain) <= 1 {
return
}
c.world.DestroyBody(c.chain[0].body)
c.chain = c.chain[1:]
f := box2d.MakeB2Vec2(0, 100)
c.jaws.upper.body.ApplyForce(f, c.jaws.upper.body.GetPosition(), true)
c.jaws.lower.body.ApplyForce(f, c.jaws.upper.body.GetPosition(), true)
if len(c.chain) > 0 {
// TODO: check if previous join destroyed by destroying its body
// TODO: use part rotation. now it is hardcoded
c.createChainJoint(c.body, box2d.B2Vec2_zero, c.chain[0].body, box2d.MakeB2Vec2(0, -c.chainElSize.Y/2))
}
}
func (c *Crane) unwind() {
// TODO: use angle (see engine)
pos := box2d.B2Vec2Add(c.body.GetPosition(), box2d.MakeB2Vec2(0, 0.5+c.chainElSize.Y/2))
// Chain must be massive (see density) to joint work well
chainEl := NewGameObj(
c.world,
chainElSprite,
pos, 0, 0,
box2d.B2Vec2_zero,
DefaultFriction, 100, DefaultFixtureRestitution, true)
chainEl.body.SetGravityScale(0.1)
if len(c.chain) > 0 {
// TODO: apply additional force jaws
prevBody := c.chain[0].body
c.destroyCrainJoints(prevBody)
c.createChainJoint(prevBody, box2d.MakeB2Vec2(0, -c.chainElSize.Y/2), chainEl.body, box2d.MakeB2Vec2(0, c.chainElSize.Y/2))
}
// TODO: use rotation. now its hardcoded
c.createChainJoint(c.body, box2d.B2Vec2_zero, chainEl.body, box2d.MakeB2Vec2(0, -c.chainElSize.Y/2))
// TODO: use linked list?
c.chain = append([]*GameObj{chainEl}, c.chain...)
}
func (c *Crane) createChainJoint(
bodyA *box2d.B2Body,
lpA box2d.B2Vec2,
bodyB *box2d.B2Body,
lpB box2d.B2Vec2) {
//// TODO: try ropeJoint
rjd := box2d.MakeB2RevoluteJointDef()
rjd.BodyA = bodyA
rjd.LocalAnchorA = lpA
rjd.BodyB = bodyB
rjd.LocalAnchorB = lpB
rjd.CollideConnected = false
c.world.CreateJoint(&rjd)
//djd := box2d.MakeB2DistanceJointDef()
//djd.BodyA = bodyA
//djd.LocalAnchorA = lpA
//djd.BodyB = bodyB
//djd.LocalAnchorB = lpB
//djd.CollideConnected = false
//djd.Length = chainElLen
//c.world.CreateJoint(&djd)
}
func (c *Crane) destroyCrainJoints(body *box2d.B2Body) {
type IHaveBodyA interface {
GetBodyA() *box2d.B2Body
}
for joint := body.GetJointList(); joint != nil; joint = joint.Next {
ja, ok := joint.Joint.(IHaveBodyA)
if ok && ja.GetBodyA() == c.GetBody() {
c.world.DestroyJoint(joint.Joint)
continue
}
}
}