oneup / puituniverse

particle attractor, multiverse, ruby game programming tutorial & 2d engine

This URL has Read+Write access

puituniverse / volleyball / player.rb
100644 141 lines (114 sloc) 2.56 kb
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
class VolleyballPlayer < VolleyballGameobject
  attr_accessor :score, :side
  @@speed = 5
  
  def self.left
    self.new :left
  end
  
  def self.right
    self.new :right
  end
  
  def controller side
    {:right => {"left".key => :move_left,
                "right".key => :move_right,
                "left".gamepad => :move_left,
                "right".gamepad => :move_right,
                "up".key => :jump,
                "up".gamepad => :jump},
 
      :left => {"a".key => :move_left,
                "d".key => :move_right,
                "w".key => :jump}
    }[side]
  end
            
  def initialize side
    set_sprite("puit/jack/stand")
 
    @side = side
    if side == :left
      @x = 10
    else
      @x = $game.width - sprite.width - 1.px
    end
 
    @y = bottom_border
    @vel_x = 0
    @vel_y = 0
    
    @score = 0
    
    set_keys controller(side)
  end
  
  def is_facing_left
    @facing_left = true
  end
  
  def is_facing_right
    @facing_left = false
  end
  
  def is_facing_left?
    return @facing_left
  end
  def touch_ground
    super
    @vel_y = 0
  end
 
  def draw
    if is_facing_left?
      sprite.draw(@x+width,@y, 0, -1)
    else
      sprite.draw(@x,@y)
    end
    x = @side == :left ? 10 : $game.width - 150
    font.draw("#{@score} points", x, 10, 0)
  end
  
  def font
    "puit/font/Busk_3x3pixel_fin".ttf
  end
    
  def jump pressed
    if pressed
      @jump_count = 2 if not in_air?
      
      if @jump_count > 0
        if in_air?
          @vel_y += jump_velocity # add to jump speed to doublejump higher
        else
          @vel_y = jump_velocity # jump up
        end
        @jump_count -= 1
      end
    end
  end
  
  def jump_velocity
    -6
  end
  
  def acceleration
    2.5
  end
  
  def max_speed
    5
  end
  
  def friction
    acceleration/2
  end
  
  def update
    super
    
    if is_moving?
      if @vel_x > 0
        @vel_x -= friction
        @vel_x = 0 if @vel_x < 0
      else
        @vel_x += friction
        @vel_x = 0 if @vel_x > 0
      end
    end
    
    if is_pressed? :move_left and is_pressed? :move_right
      # do nothing lol
    elsif is_pressed? :move_left
      @vel_x = -1 if @vel_x >= 0
      @vel_x -= acceleration
      is_facing_left
    elsif is_pressed? :move_right
      @vel_x = 1 if @vel_x <= 0
      @vel_x += acceleration
      is_facing_right
    end
    
    @vel_x = (-max_speed..max_speed).limit @vel_x
  end
  
  def move_right pressed
  end
  
  def move_left pressed
  end
end