summaryrefslogtreecommitdiffhomepage
path: root/src/logic.rb
blob: 1be80243fd8a868a301aaafc58f9ddbdbf31d0f9 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
FECS::Cmp.new('Player')
FECS::Cmp.new('Velocity', x: 0, y: 0)
FECS::Cmp.new('Position', x: 0, y: 0)
FECS::Cmp.new('Movement', deceleration: 3, acceleration: 8, max_speed: 15)
FECS::Cmp.new('Sprite',
              :texture,
              :origin,
              :source_rect,
              :dest_rect,
              :rotation,
              :tint)
FECS::Cmp.new('Tileset',
              :origin,
              :tileset,
              :dest_rect,
              :rotation,
              :tint)
FECS::Cmp.new('Hp', value: 0)

lancelot = Tileset.new(texture: Rl::Texture.new('./assets/lancelot_.png'))

8.times do |x|
  lancelot.frames.push Rl::Rectangle.new((24 * x), 0, 24, 24)
end


@player = FECS::Ent.new(
  FECS::Cmp::Player.new,
  FECS::Cmp::Position.new,
  FECS::Cmp::Velocity.new,
  #FECS::Cmp::Sprite.new(
  #  texture: Rl::Texture.new('./assets/lancelot_.png'),
  #  source_rect: Rl::Rectangle.new(0,0,24,24),
  #  dest_rect: Rl::Rectangle.new(0,0,48,48),
  #  origin: Rl::Vector2.new(0,0),
  #  tint: Rl::Color.new(255,255,255,255),
  #  rotation: 0
  #),
  FECS::Cmp::Tileset.new(
    tileset: lancelot,
    dest_rect: Rl::Rectangle.new(0,0,48,48),
    origin: Rl::Vector2.new(0,0),
    tint: Rl::Color.new(255,255,255,255),
    rotation: 0
  ),
  FECS::Cmp::Movement.new(
    deceleration: 2.25,
    acceleration: 3,
    max_speed: 300,
  ),
)



FECS::Stg.add(FECS::Scn.new('Play'))

FECS::Scn::Play.add(
  FECS::Sys.new('PlayerInput') do
    ent = FECS::Cmp::Player.first.entity
    velocity_cmp = ent.component[FECS::Cmp::Velocity]
    #velocity_cmp.x = 0
    #velocity_cmp.y = 0
    input_x = 0
    input_y = 0
    movement_cmp = ent.component[FECS::Cmp::Movement]
    if Rl.key_down? 87 # UP W
      #velocity_cmp.y += movement_cmp.acceleration
      input_y += 1.0
      #velocity_cmp.y = [velocity_cmp.y,
      #                  (movement_cmp.max_speed + movement_cmp.deceleration)].min
    end
    if Rl.key_down? 83 # DOWN S
      #velocity_cmp.y -= movement_cmp.acceleration 
      input_y -= 1.0
      #velocity_cmp.y = [velocity_cmp.y,
      #                  (-movement_cmp.max_speed - movement_cmp.deceleration)].max
    end
    if Rl.key_down? 65 # LEFT A
      #velocity_cmp.x += movement_cmp.acceleration 
      input_x += 1.0
      #velocity_cmp.x = [velocity_cmp.x,
      #                  (movement_cmp.max_speed + movement_cmp.deceleration)].min
    end
    if Rl.key_down? 68 # RIGHT D
      #velocity_cmp.x -= movement_cmp.acceleration 
      input_x -= 1.0
      #velocity_cmp.x = [velocity_cmp.x,
      #                  (-movement_cmp.max_speed - movement_cmp.deceleration)].max
    end
    # Normalize input
    input_mag = Math.sqrt((input_x**2) + (input_y**2))
    unless input_mag.zero?
      input_x /= input_mag
      input_y /= input_mag
    end

    # Add normalized speed
    velocity_cmp.x += input_x * movement_cmp.acceleration
    velocity_cmp.y += input_y * movement_cmp.acceleration

    # Get magnitude
    velocity_mag = Math.sqrt((velocity_cmp.x**2) + (velocity_cmp.y**2))

    # If going slower then deceleration
    if velocity_mag < movement_cmp.deceleration
      # Set to 0
      velocity_cmp.x = 0
      velocity_cmp.y = 0
    else
      # Normalize Velocity
      velocity_x_mag = velocity_cmp.x / velocity_mag
      velocity_y_mag = velocity_cmp.y / velocity_mag

      # Add deceleration
      velocity_cmp.x -= velocity_x_mag * movement_cmp.deceleration
      velocity_cmp.y -= velocity_y_mag * movement_cmp.deceleration

      velocity_mag = Math.sqrt((velocity_cmp.x**2) + (velocity_cmp.y**2))

      if velocity_mag > movement_cmp.max_speed
        velocity_x_mag = velocity_cmp.x / velocity_mag
        velocity_y_mag = velocity_cmp.y / velocity_mag

        velocity_cmp.x = velocity_x_mag * movement_cmp.max_speed
        velocity_cmp.y = velocity_y_mag * movement_cmp.max_speed
      end
    end


    #if velocity_cmp.x > (movement_cmp.deceleration)
    #  velocity_cmp.x -= movement_cmp.deceleration 
    #elsif velocity_cmp.x <  (-movement_cmp.deceleration)
    #  velocity_cmp.x += movement_cmp.deceleration 
    #else
    #  velocity_cmp.x = 0
    #end
    #if velocity_cmp.y > (movement_cmp.deceleration)
    #  velocity_cmp.y -= movement_cmp.deceleration 
    #elsif velocity_cmp.y <  (-movement_cmp.deceleration)
    #  velocity_cmp.y += movement_cmp.deceleration 
    #else
    #  velocity_cmp.y = 0
    #end
  end,
  FECS::Sys.new('Movement') do
    FECS::Cmp::Velocity.each do |velocity_cmp|
      ent = velocity_cmp.entity
      position_component = ent.component[FECS::Cmp::Position]
      movement_cmp = ent.component[FECS::Cmp::Movement]

      position_component.x += velocity_cmp.x * Rl.frame_time
      position_component.y += velocity_cmp.y * Rl.frame_time
    end
  end,
  FECS::Sys.new('ApplyPositionToSprite') do
    FECS::Cmp::Position.each do |position_cmp|
      sprite = position_cmp.entity.component[FECS::Cmp::Tileset]
      sprite.tileset.step(4 * Rl.frame_time)
      if sprite
        sprite.origin.x = position_cmp.x
        sprite.origin.y = position_cmp.y
      end
    end
  end,
  FECS::Sys.new('ShowSpeed') do
    player = FECS::Cmp::Player.first.entity
    y_vel = player.component[FECS::Cmp::Velocity].y.abs
    x_vel = player.component[FECS::Cmp::Velocity].x.abs
    vel = Math.sqrt(x_vel**2 + y_vel**2)
    movement = player.component[FECS::Cmp::Movement]
    frame = player.component[FECS::Cmp::Tileset].tileset.frame
    max_speed = (movement.max_speed)

    Rl.draw_text(text: "x max: #{"%.1f" % max_speed}", x: 500, y: 0, font_size: 30, color: BLACK)
    Rl.draw_text(text: "animation frame: #{"%.2f" % frame}", x: 500, y: 30, font_size: 30, color: BLACK)
    Rl.draw_text(text: "x vel: #{"%.1f" % x_vel}", x: 500, y: 60, font_size: 30, color: BLACK)
    Rl.draw_text(text: "y vel: #{"%.1f" % y_vel}", x: 500, y: 90, font_size: 30, color: BLACK)
    Rl.draw_text(text: "n vel: #{"%.1f" % vel}", x: 500, y: 120, font_size: 30, color: BLACK)
  end,
  FECS::Sys.new('Render') do
    #FECS::Cmp::Tileset.each do |sprite_cmp|
    #  Rl.draw_texture_pro(texture: sprite_cmp.texture,
    #                      origin: sprite_cmp.origin,
    #                      source_rect: sprite_cmp.source_rect,
    #                      dest_rect: sprite_cmp.dest_rect,
    #                      tint: sprite_cmp.tint,
    #                      rotation: sprite_cmp.rotation)
    #end
    FECS::Cmp::Tileset.each do |sprite_cmp|
      Rl.draw_texture_pro(texture: sprite_cmp.tileset.texture,
                          origin: sprite_cmp.origin,
                          source_rect: sprite_cmp.tileset.rect,
                          dest_rect: sprite_cmp.dest_rect,
                          tint: sprite_cmp.tint,
                          rotation: sprite_cmp.rotation)
    end
  end,
)

FelECS::Order.sort(
  FECS::Sys::PlayerInput,
  FECS::Sys::Movement,
  FECS::Sys::ApplyPositionToSprite,
  FECS::Sys::Render,
)