-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.rb
133 lines (124 loc) · 3.51 KB
/
player.rb
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
class Player
attr_reader :x, :y, :icon, :bombs, :bombs_used, :movespeed
attr_writer :bomb
def initialize(window, tile)
@window = window
@coin_pickup = Gosu::Sample.new(@window, 'music/coin.ogg')
@bomb_drop = Gosu::Sample.new(@window, 'music/bomb.ogg')
@icon = Gosu::Image.new(@window, "runright/playerright.png", true)
@iconleft = Gosu::Image.new(@window, "runleft/playerleft.png", true)
@fallright = Gosu::Image.new(@window, "runright/fallright.png", true)
@fallleft = Gosu::Image.new(@window, "runleft/fallleft.png", true)
@runright = []
12.times do |i|
@runright << Gosu::Image.new(@window, "runright/right#{i+1}.png", true)
end
@runleft = []
12.times do |i|
@runleft << Gosu::Image.new(@window, "runleft/left#{i+1}.png", true)
end
@x = (window.width / 2) - (@icon.width / 2)
@y = (tile.height * 8) - @icon.height
@tile = tile
@movespeed = 13
@direction = 1
@falling = false
@accel = 1
@bombs = []
@bombs_used = 0
end
def jump(speed)
if @falling == false
@accel = -10 - speed
@falling = true
end
end
def move_left(board, offset)
@direction = -1
@x = @x - @movespeed
board.each do |tile|
if tile.x < @x && @x - tile.x < @tile.width
if (tile.y + offset) > @y - @tile.height && (tile.y + offset) < @y + @icon.height
@x = tile.x + @tile.width + 1
end
end
end
if @x < 0 then @x = 0 end
end
def move_right(board, offset)
@direction = 1
@x = @x + @movespeed
board.each do |tile|
if tile.x > @x && tile.x - @x < @icon.width
if (tile.y + offset) > @y - @tile.height && (tile.y + offset) < @y + @icon.height
@x = tile.x - @icon.width - 1
end
end
end
if @x > @window.width - @icon.width then @x = @window.width - @icon.width end
end
def floor_contact(board, offset, height)
if @falling == true
@accel += 1
else
@accel = 1
end
@y += 1 * @accel
@falling = true
board.each do |tile|
if (tile.y + offset) > @y && (tile.y + offset) - @y < @icon.height
if tile.x > 0 && tile.x < @window.width - @tile.width
if tile.x > @x - @tile.width && tile.x < @x + @icon.width
@y = (tile.y + offset) - @icon.height - 1
@falling = false
end
end
end
end
if @y > height - @icon.height
@y = height - @icon.height
@falling = false
end
end
def draw(movement)
if @direction == 1
if @falling == true
@fallright.draw(@x - 10, @y, 5)
elsif movement == 0
@icon.draw(@x, @y, 2)
else
num = ((movement - 1) / 3)
@runright[num].draw(@x - 10, @y, 5)
end
elsif @direction == -1
if @falling == true
@fallleft.draw(@x - 10, @y, 5)
elsif movement == 0
@iconleft.draw(@x, @y, 2)
else
num = ((movement - 1) / 3)
@runleft[num].draw(@x - 10, @y, 5)
end
end
end
def dead?
@y < [email protected]
end
def collect_coins(coins, offset, sfx)
if coins.reject! { |coin| Gosu::distance(@x, @y + 24, coin.x, coin.y + offset) < 50 } then
if sfx == true
@coin_pickup.play(0.2)
end
return true
end
false
end
def drop_bomb(board, offset, sfx)
@bombs << Bomb.new(@window, @x, @y + @icon.height)
if sfx == true
@bomb_drop.play(0.5)
end
board.reject! { |tile| Gosu::distance(@bombs[-1].x, @bombs[-1].y, tile.x, tile.y + offset) < 80 }
@bombs_used += 1
end
end