Class: GameLogic

Inherits:
Object
  • Object
show all
Defined in:
lib/server/gamelogic.rb

Overview

high level game logic

Instance Method Summary collapse

Constructor Details

#initialize(console) ⇒ GameLogic

Returns a new instance of GameLogic.



7
8
9
10
# File 'lib/server/gamelogic.rb', line 7

def initialize(console)
  @console = console
  @alive_players = 0
end

Instance Method Details

#check_collide(players, player) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/server/gamelogic.rb', line 27

def check_collide(players, player)
  players.each do |other|
    next if other == player
    next unless player.collide[:down]

    x_force = player.check_player_collide(other)
    player.apply_force(x_force, -8) unless x_force.zero?
  end
end

#game_map_collision_vertical(game_map, player) ⇒ Object



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
# File 'lib/server/gamelogic.rb', line 57

def game_map_collision_vertical(game_map, player)
  if player.dy.positive?
    # left bottom
    col = game_map.collision?(player.x / TILE_SIZE, (player.y + player.h) / TILE_SIZE)
    if col
      player.y = col[:y] * TILE_SIZE
      player.y -= player.crouching? ? PLAYER_SIZE / 2 : PLAYER_SIZE
      player.do_collide(:down, true)
    end
    # right bottom
    col = game_map.collision?((player.x + player.w) / TILE_SIZE, (player.y + player.h) / TILE_SIZE)
    if col
      player.y = col[:y] * TILE_SIZE
      player.y -= player.crouching? ? PLAYER_SIZE / 2 : PLAYER_SIZE
      player.do_collide(:down, true)
    end
  elsif player.dy.negative?
    # left top
    col = game_map.collision?(player.x / TILE_SIZE, player.y / TILE_SIZE)
    if col
      player.y = (col[:y] * TILE_SIZE) + player.h
      player.y += 1
      player.y += PLAYER_SIZE / 2 if player.crouching?
      player.do_collide(:up, true)
    end
    # right top
    col = game_map.collision?((player.x + player.w) / TILE_SIZE, player.y / TILE_SIZE)
    if col
      player.y = (col[:y] * TILE_SIZE) + player.h
      player.y += 1
      player.y += PLAYER_SIZE / 2 if player.crouching?
      player.do_collide(:up, true)
    end
  end
  nil
end

#gravity(game_map, player, _dt, _tick) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/server/gamelogic.rb', line 184

def gravity(game_map, player, _dt, _tick)
  if player.dead
    player.dead_ticks += 1
    player.state[:bleeding] = true
    if player.dead_ticks > 3
      player.dead = false
      player.state[:bleeding] = false
      player.die
    end
  elsif game_map.death?(player.x / TILE_SIZE, (player.y + player.h) / TILE_SIZE)
    player.dead = true
    player.dead_ticks = 0
  end

  # grav = 100000 * dt
  # @console.log "grav: #{grav}"
  # player.y += grav
  player.dy += 1 if player.dy < 16
end

#handle_client_requests(game_map, data, id, players, _dt) ⇒ Object



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
# File 'lib/server/gamelogic.rb', line 94

def handle_client_requests(game_map, data, id, players, _dt)
  player = Player.get_player_by_id(players, id)
  if player.nil?
    @console.log "WARNING failed to update nil player with id=#{id}"
    if players.count.positive?
      @console.log 'connected players:'
    else
      @console.log 'no players currently connected!'
    end
    players.each do |p|
      @console.log "id=#{p.id} name='#{p.name}'"
    end
    return players
  end

  # reset values (should stay first)
  player.wants_crouch = false

  # move request
  if data[0] == '1'
    @console.dbg "player=#{id} wants to crouch"
    player.crouch!
    player.wants_crouch = true
    player.x -= PLAYER_SIZE / 4 unless player.was_crouching
    # TODO: why is it checking right when on left side!?
    if closest_interval_side(TILE_SIZE, player.x) == SIDE_LEFT
      player.check_move_right(game_map)
    else
      player.check_move_left(game_map)
    end
    player.was_crouching = true
  end
  if data[1] == 'l'
    game_map_collision_vertical(game_map, player)
    @console.dbg "player=#{id} wants to walk left"
    player.move_left(game_map)
  end
  if data[1] == 'r'
    @console.dbg "player=#{id} wants to walk right"
    game_map_collision_vertical(game_map, player)
    player.move_right(game_map)
  end
  if data[2] == '1'
    @console.dbg "player=#{id} wants to jump"
    player.do_jump
  end
  if data[3] == '1' && player.crouching? == false
    @console.dbg "player=#{id} wants to fire"
    player.fire_ticks += 1
    if player.fire_ticks > 29
      player.state[:fire] = 3
    elsif player.fire_ticks > 19
      player.state[:fire] = 2
    elsif player.fire_ticks > 9
      player.state[:fire] = 1
    end
  else
    if player.fire_ticks.positive?
      dx = (player.aim_x - player.x).clamp(-200, 200) / 20
      dy = (player.aim_y - player.y).clamp(-200, 200) / 20
      dx *= (player.fire_ticks / 10).clamp(1, 3)
      dy *= (player.fire_ticks / 10).clamp(1, 3)
      player.projectile.fire(player.x + TILE_SIZE / 4, player.y + TILE_SIZE / 2, dx, dy, player)
    end
    player.fire_ticks = 0
    player.state[:fire] = 0
  end
  player.aim_x = net_unpack_bigint(data[4..5])
  player.aim_y = net_unpack_bigint(data[6..7])
  # player.projectile.x = player.aim_x + 20
  # player.projectile.y = player.aim_y + 20

  player.check_out_of_game_map

  # return updated players
  players
end

#on_player_connect(client, players) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/server/gamelogic.rb', line 12

def on_player_connect(client, players)
  player = Player.get_player_by_id(players, client[PLAYER_ID])
  return if player.nil?

  port, ip = Socket.unpack_sockaddr_in(client[NET_CLIENT].getpeername)
  @console.log "player joined ID=#{player.id} IP=#{ip}:#{port} name='#{player.name}'"
end

#on_player_disconnect(client, players) ⇒ Object



20
21
22
23
24
25
# File 'lib/server/gamelogic.rb', line 20

def on_player_disconnect(client, players)
  player = Player.get_player_by_id(players, client[PLAYER_ID])
  return if player.nil?

  @console.log "player left ID=#{player.id} name='#{player.name}'"
end

#posttick(game_map, players, _dt) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/server/gamelogic.rb', line 172

def posttick(game_map, players, _dt)
  players.each do |player|
    # stopped crouching -> stand up
    next unless player.was_crouching && player.wants_crouch == false

    player.x += PLAYER_SIZE / 4
    player.was_crouching = false
    player.stop_crouch!
    game_map_collision_vertical(game_map, player)
  end
end

#tick(game_map, players, dt, tick) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/server/gamelogic.rb', line 37

def tick(game_map, players, dt, tick)
  players.each do |player|
    # reset values (should stay first)
    player.reset_collide

    gravity(game_map, player, dt, tick)
    player.tick
    game_map_collision_vertical(game_map, player)
    if player.dx.positive?
      player.check_move_right(game_map)
    elsif player.dx.negative?
      player.check_move_left(game_map)
    end
    player.projectile.tick(players)
    # player collsions works
    # but it eats performance and delays jumping
    check_collide(players, player)
  end
end