-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibrationObject.go
149 lines (127 loc) · 3.62 KB
/
calibrationObject.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
package main
import (
"GoConsoleBT/collider"
"GoConsoleBT/controller"
"math"
"time"
)
const CALIBRATION_COMPLETE = 600
var CalibrationCompleteEvent = Event{
EType: CALIBRATION_COMPLETE,
Object: nil,
Payload: nil,
}
//receiver vs opposite
//bottom to top
//left to right
//top to bottom
//right to left
//borders
//left top
//right top
//right bottom
//left bottom
type CalibrationObject struct {
*MotionObject
*ControlledObject
*ObservableObject
opposite *CalibrationObject
Probe []*Point
}
func (receiver *CalibrationObject) Update(timeLeft time.Duration) error {
if receiver.moving {
receiver.GetClBody().RelativeMove(
receiver.Moving.Direction.X*receiver.Moving.Speed.X*(float64(timeLeft)/float64(time.Second)),
receiver.Moving.Direction.Y*receiver.Moving.Speed.Y*(float64(timeLeft)/float64(time.Second)),
)
}
receiver.moving = false
return nil
}
func (receiver *CalibrationObject) Execute(command controller.Command) error {
if command.CType == controller.CTYPE_DIRECTION || command.CType == controller.CTYPE_MOVE {
receiver.moving = command.Action
}
if command.Pos.X != command.Pos.Y {
receiver.Moving.Direction.X = command.Pos.X
receiver.Moving.Direction.Y = command.Pos.Y
receiver.Moving.Direction.X = math.Max(math.Min(receiver.Moving.Direction.X, 1), -1)
receiver.Moving.Direction.Y = math.Max(math.Min(receiver.Moving.Direction.Y, 1), -1)
} else {
//invalid direction
}
if command.CType == controller.CTYPE_FIRE && command.Action {
receiver.moving = false
receiver.Calibrate()
}
return nil
}
func (receiver *CalibrationObject) Calibrate() {
index := len(receiver.Probe)
x, y := receiver.GetClBody().GetXY()
w, h := receiver.GetClBody().GetWH()
if index < 4 {
opX, opY := receiver.opposite.GetClBody().GetXY()
receiver.Probe = append(receiver.Probe, &Point{
X: math.Abs(x-opX) / w,
Y: math.Abs(y-opY) / h,
})
} else if index < 8 {
receiver.Probe = append(receiver.Probe, &Point{
X: x,
Y: y,
})
}
if index == 7 {
receiver.Trigger(CalibrationCompleteEvent, receiver, receiver.Probe)
}
}
func (receiver *CalibrationObject) Reset() error {
receiver.ControlledObject.Activate()
receiver.MotionObject.Reset()
receiver.moving = false
return nil
}
func NewCalibrationObject(control *controller.Control, chanel EventChanel, x, y float64) (*CalibrationObject, error) {
sprite := NewSprite()
sprite.Write([]byte("****\n****\n****\n****"))
sprite.CalculateSize()
collision := collider.NewPenetrateCollision(x, y, 4, 4)
obj, _ := NewObject(sprite, collision)
mm, _ := NewMotionObject(obj, Point{
X: 0,
Y: -1,
}, Point{
X: 8,
Y: 8,
})
mm.MinSpeed.X = 8
mm.MinSpeed.Y = 8
mm.MaxSpeed.X = 16
mm.MaxSpeed.Y = 16
mm.AccelDuration = time.Second * 2
co, _ := NewControlledObject(control, nil)
oo, _ := NewObservableObject(chanel, nil)
calibrationObject := &CalibrationObject{
MotionObject: mm,
ControlledObject: co,
ObservableObject: oo,
opposite: nil,
Probe: make([]*Point, 0, 8),
}
calibrationObject.ControlledObject.Owner = calibrationObject
calibrationObject.ObservableObject.Owner = calibrationObject
return calibrationObject, nil
}
func (receiver *CalibrationObject) Free() {
receiver.ControlledObject.Free()
receiver.MotionObject.Free()
}
func NewCalibrationBinary(control *controller.Control, chanel EventChanel, x1, y1, x2, y2 float64) (active *CalibrationObject, passive *CalibrationObject, error error) {
none, _ := controller.NewNoneControl()
obj1, _ := NewCalibrationObject(control, chanel, x1, y1)
obj2, _ := NewCalibrationObject(none, nil, x2, y2)
obj1.opposite = obj2
obj2.opposite = obj1
return obj1, obj2, nil
}