-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathplayer.coffee
173 lines (140 loc) · 5.98 KB
/
player.coffee
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
171
172
# Rcssim2d.js - A robot soccer simulator 2d in js
# Copyright 2013 Jules Wang
#
# Rcssim2d.js is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Rcssim2d.js is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class Player
constructor:(p, dir, wm, side) ->
@fc = 'grey'
@sc = 'black'
@m = 5.0 #kg
@r = 6.5
@t = 'none'
@v = [0, 0]
@decay = 0.4
@maxdashforce = 6
@maxkickforce = 2.2
@maxturnangle = 0.1
@force = 0
@kickforce = 0
@dd = 0
@wm = wm
@side = side
@p = @transpos(p)
@d = @transdir(dir)
@initpos = clone(@p)
@last_v = clone(@v)
reset:() ->
@p = clone(@initpos)
render:(canvas) ->
# draw body
if !@client or [email protected]_color
fill_color = @fc
else
fill_color = @client.fill_color
stroke_color = @sc
x = @p[0]
y = @p[1]
canvas.fillCircle(fill_color, x, y, @r)
canvas.drawCircle(stroke_color, x, y, @r)
# draw dir, sc
canvas.fillArc(stroke_color, x, y, @r, @d-2*Math.PI/5, @d+2*Math.PI/5)
take_action:() ->
return if not @client
actions = @client.think(@getbasicinfo())
for action of actions
switch action
when 'jump' then @jump(actions['jump'])
when 'dash' then @dash(actions['dash'])
when 'turn' then @turn(actions['turn'])
when 'kick' then @kick(actions['kick'])
when 'suck' then @suck()
update:() ->
if @wm.selected != this
@take_action()
@last_v = clone(@v)
if Vector2d.len(@v) > 1e-5
ds = @v
@p = Vector2d.add(@p, ds)
@v = Vector2d.multiply(@v, @decay)
a = @force / @m
@force = 0
unitv = Vector2d.vector(@d)
dv = Vector2d.multiply(unitv, a)
@v = Vector2d.add(@v, dv)
@d += @dd
@dd = 0
@wm.pitch.last_touch_ball = @side if @kickforce != 0
@wm.ball.acc(unitv, @kickforce)
@kickforce = 0
dash:(force) ->
force = @maxdashforce if force > @maxdashforce
force = -@maxdashforce if force < -@maxdashforce
@force = force
turn:(dir) ->
@dd = dir
@dd = @maxturnangle if @dd > @maxturnangle
@dd = -@maxturnangle if @dd < -@maxturnangle
kick:(force) ->
return if not @wm
return if force <= 0
bp = @wm.ball.p
return if Vector2d.distance(@p, bp) > @r + @wm.ball.r+3
p2b = Vector2d.subtract(bp, @p)
unitv = Vector2d.vector(@d)
return if Math.abs(Vector2d.angle(p2b, unitv)) > Math.PI/6
force = @maxkickforce if force > @maxkickforce
@kickforce = force
jump:(pos) ->
@p = clone(pos)
@p = @transpos(@p)
# Goalie only, our robots dont have hands, so they suck like the cleaner
suck:() ->
pos = @transpos(@wm.ball.p)
return if @client.teamnum != 0
return if pos[0] > [email protected]_length/2 + @wm.pitch.penalty_area_length
return if pos[1] > @wm.pitch.penalty_area_width/2
bp = @wm.ball.p
return if Vector2d.distance(@p, bp) > @r + @wm.ball.r + 3
p2b = Vector2d.subtract(bp, @p)
unitv = Vector2d.vector(@d)
return if Math.abs(Vector2d.angle(p2b, unitv)) > Math.PI/6
unitdir = Vector2d.vector(@d)
@wm.ball.p = Vector2d.add(Vector2d.multiply(unitdir, @wm.ball.r + @r + 0.1), @p)
@wm.ball.v = [0,0]
@wm.pitch.change_state('goalkick_' + @side)
getbasicinfo:() ->
wm = {}
wm.leftplayers = []
wm.rightplayers = []
for player in @wm.leftplayers
p = clone(player.p)
p = @transpos(p)
wm.leftplayers.push p
for player in @wm.rightplayers
p = clone(player.p)
p = @transpos(p)
wm.rightplayers.push p
wm.gamestate = @wm.pitch.state
wm.ball = clone(@wm.ball.p)
wm.ball = @transpos(wm.ball)
wm.mydir = @transdir(@d)
return wm
# whatever the team is at left or right
# the x axis of our half-field is always negative.
transpos:(p) ->
return p if @side is 'left'
return [-p[0], -p[1]]
transdir:(dir) ->
return dir if @side is 'left'
return Math.normaliseRadians(dir+Math.PI)