Commit bb864358b9dc826ae7f1952e6bd14cca5d7e238a
1 parent
05565c3229
Exists in
master
keep player inside the screen
Showing 2 changed files with 19 additions and 15 deletions Side-by-side Diff
lib/drawable.rb
... | ... | @@ -33,13 +33,8 @@ |
33 | 33 | game.space.add_shape(@shape) |
34 | 34 | end |
35 | 35 | |
36 | - def pos | |
37 | - @body.p.to_a | |
38 | - end | |
39 | - | |
40 | - def pos= p | |
41 | - @body.p = CP::Vec2.new(*p) | |
42 | - end | |
36 | + def pos; @body.p.to_a; end | |
37 | + def vel; @body.v.to_a; end | |
43 | 38 | |
44 | 39 | def rect |
45 | 40 | Rect.new(@body.p.to_a, @surface.size) |
lib/player.rb
... | ... | @@ -11,18 +11,27 @@ |
11 | 11 | def move direction |
12 | 12 | case direction |
13 | 13 | when :up |
14 | - @body.v += CP::Vec2.new(0.0, -1.0) | |
14 | + @body.p += CP::Vec2.new(0.0, -1.0 * @params[:speed] * 10) | |
15 | 15 | when :down |
16 | - @body.v += CP::Vec2.new(0.0, 1.0) | |
16 | + @body.p += CP::Vec2.new(0.0, 1.0 * @params[:speed] * 10) | |
17 | 17 | when :left |
18 | - @body.v += CP::Vec2.new(-1.0, 0.0) | |
18 | + @body.p += CP::Vec2.new(-1.0 * @params[:speed] * 10, 0.0) | |
19 | 19 | when :right |
20 | - @body.v += CP::Vec2.new(1.0, 0.0) | |
20 | + @body.p += CP::Vec2.new(1.0 * @params[:speed] * 10, 0.0) | |
21 | 21 | end |
22 | - @pos[0] = 0 if @pos[0] < 0 | |
23 | - @pos[1] = 0 if @pos[1] < 0 | |
24 | - @pos[0] = screen.size[0] - size[0] if @pos[0] > screen.size[0] - size[0] | |
25 | - @pos[1] = screen.size[1] - size[1] if @pos[1] > screen.size[1] - size[1] | |
22 | + end | |
23 | + | |
24 | + def draw *a | |
25 | + p, v = pos, vel | |
26 | + p[0], v[0] = 0, 0 if p[0] < 0 | |
27 | + p[1], v[1] = 0, 0 if p[1] < 0 | |
28 | + p[0], v[0] = screen.size[0], 0 if p[0] > screen.size[0] | |
29 | + p[1], v[1] = screen.size[1], 0 if p[1] > screen.size[1] | |
30 | + if pos != p | |
31 | + @body.p = CP::Vec2.new(*p) | |
32 | + @body.v = CP::Vec2.new(*v) | |
33 | + end | |
34 | + super *a | |
26 | 35 | end |
27 | 36 | |
28 | 37 | def destroy |