summaryrefslogtreecommitdiffhomepage
path: root/samples/07_advanced_rendering/08_splitscreen_camera/app/main.rb
blob: 9d22918f529557ee773425f6380671f626230dff (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
class CameraMovement
  attr_accessor :state, :inputs, :outputs, :grid

  #==============================================================================================
  #Serialize
  def serialize
    {state: state, inputs: inputs, outputs: outputs, grid: grid }
  end

  def inspect
    serialize.to_s
  end

  def to_s
    serialize.to_s
  end

  #==============================================================================================
  #Tick
  def tick
    defaults
    calc
    render
    input
  end

  #==============================================================================================
  #Default functions
  def defaults
    outputs[:scene].background_color = [0,0,0]
    state.trauma ||= 0.0
    state.trauma_power ||= 2
    state.player_cyan ||= new_player_cyan
    state.player_magenta ||= new_player_magenta
    state.camera_magenta ||= new_camera_magenta
    state.camera_cyan ||= new_camera_cyan
    state.camera_center ||= new_camera_center
    state.room ||= new_room
  end

  def default_player x, y, w, h, sprite_path
    state.new_entity(:player,
                     { x: x,
                       y: y,
                       dy: 0,
                       dx: 0,
                       w: w,
                       h: h,
                       damage: 0,
                       dead: false,
                       orientation: "down",
                       max_alpha: 255,
                       sprite_path: sprite_path})
  end

  def default_floor_tile x, y, w, h, sprite_path
    state.new_entity(:room,
                     { x: x,
                       y: y,
                       w: w,
                       h: h,
                       sprite_path: sprite_path})
  end

  def default_camera x, y, w, h
    state.new_entity(:camera,
                     { x: x,
                       y: y,
                       dx: 0,
                       dy: 0,
                       w: w,
                       h: h})
  end

  def new_player_cyan
    default_player(0, 0, 64, 64,
                   "sprites/player/player_#{state.player_cyan.orientation}_standing.png")
  end

  def new_player_magenta
    default_player(64, 0, 64, 64,
                   "sprites/player/player_#{state.player_magenta.orientation}_standing.png")
  end

  def new_camera_magenta
    default_camera(0,0,720,720)
  end

  def new_camera_cyan
    default_camera(0,0,720,720)
  end

  def new_camera_center
    default_camera(0,0,1280,720)
  end


  def new_room
    default_floor_tile(0,0,1024,1024,'sprites/rooms/camera_room.png')
  end

  #==============================================================================================
  #Calculation functions
  def calc
    calc_camera_magenta
    calc_camera_cyan
    calc_camera_center
    calc_player_cyan
    calc_player_magenta
    calc_trauma_decay
  end

  def center_camera_tolerance
    return Math.sqrt(((state.player_magenta.x - state.player_cyan.x) ** 2) +
              ((state.player_magenta.y - state.player_cyan.y) ** 2)) > 640
  end

  def calc_player_cyan
    state.player_cyan.x += state.player_cyan.dx
    state.player_cyan.y += state.player_cyan.dy
  end

  def calc_player_magenta
    state.player_magenta.x += state.player_magenta.dx
    state.player_magenta.y += state.player_magenta.dy
  end

  def calc_camera_center
    timeScale = 1
    midX = (state.player_magenta.x + state.player_cyan.x)/2
    midY = (state.player_magenta.y + state.player_cyan.y)/2
    targetX = midX - state.camera_center.w/2
    targetY = midY - state.camera_center.h/2
    state.camera_center.x += (targetX - state.camera_center.x) * 0.1 * timeScale
    state.camera_center.y += (targetY - state.camera_center.y) * 0.1 * timeScale
  end


  def calc_camera_magenta
    timeScale = 1
    targetX = state.player_magenta.x + state.player_magenta.w - state.camera_magenta.w/2
    targetY = state.player_magenta.y + state.player_magenta.h - state.camera_magenta.h/2
    state.camera_magenta.x += (targetX - state.camera_magenta.x) * 0.1 * timeScale
    state.camera_magenta.y += (targetY - state.camera_magenta.y) * 0.1 * timeScale
  end

  def calc_camera_cyan
    timeScale = 1
    targetX = state.player_cyan.x + state.player_cyan.w - state.camera_cyan.w/2
    targetY = state.player_cyan.y + state.player_cyan.h - state.camera_cyan.h/2
    state.camera_cyan.x += (targetX - state.camera_cyan.x) * 0.1 * timeScale
    state.camera_cyan.y += (targetY - state.camera_cyan.y) * 0.1 * timeScale
  end

  def calc_player_quadrant angle
    if angle < 45 and angle > -45 and state.player_cyan.x < state.player_magenta.x
      return 1
    elsif angle < 45 and angle > -45 and state.player_cyan.x > state.player_magenta.x
      return 3
    elsif (angle > 45 or angle < -45) and state.player_cyan.y < state.player_magenta.y
      return 2
    elsif (angle > 45 or angle < -45) and state.player_cyan.y > state.player_magenta.y
      return 4
    end
  end

  def calc_camera_shake
    state.trauma
  end

  def calc_trauma_decay
    state.trauma = state.trauma * 0.9
  end

  def calc_random_float_range(min, max)
    rand * (max-min) + min
  end

  #==============================================================================================
  #Render Functions
  def render
    render_floor
    render_player_cyan
    render_player_magenta
    if center_camera_tolerance
      render_split_camera_scene
    else
      render_camera_center_scene
    end
  end

  def render_player_cyan
    outputs[:scene].sprites << {x: state.player_cyan.x,
                                y: state.player_cyan.y,
                                w: state.player_cyan.w,
                                h: state.player_cyan.h,
                                path: "sprites/player/player_#{state.player_cyan.orientation}_standing.png",
                                r: 0,
                                g: 255,
                                b: 255}
  end

  def render_player_magenta
    outputs[:scene].sprites << {x: state.player_magenta.x,
                                y: state.player_magenta.y,
                                w: state.player_magenta.w,
                                h: state.player_magenta.h,
                                path: "sprites/player/player_#{state.player_magenta.orientation}_standing.png",
                                r: 255,
                                g: 0,
                                b: 255}
  end

  def render_floor
    outputs[:scene].sprites << [state.room.x, state.room.y,
                                state.room.w, state.room.h,
                                state.room.sprite_path]
  end

  def render_camera_center_scene
    zoomFactor = 1
    outputs[:scene].width = state.room.w
    outputs[:scene].height = state.room.h

    maxAngle = 10.0
    maxOffset = 20.0
    angle = maxAngle * calc_camera_shake * calc_random_float_range(-1,1)
    offsetX = 32 - (maxOffset * calc_camera_shake * calc_random_float_range(-1,1))
    offsetY = 32 - (maxOffset * calc_camera_shake * calc_random_float_range(-1,1))

    outputs.sprites << {x: (-state.camera_center.x - offsetX)/zoomFactor,
                        y: (-state.camera_center.y - offsetY)/zoomFactor,
                        w: outputs[:scene].width/zoomFactor,
                        h: outputs[:scene].height/zoomFactor,
                        path: :scene,
                        angle: angle,
                        source_w: -1,
                        source_h: -1}
    outputs.labels << [128,64,"#{state.trauma.round(1)}",8,2,255,0,255,255]
  end

  def render_split_camera_scene
     outputs[:scene].width = state.room.w
     outputs[:scene].height = state.room.h
     render_camera_magenta_scene
     render_camera_cyan_scene

     angle = Math.atan((state.player_magenta.y - state.player_cyan.y)/(state.player_magenta.x- state.player_cyan.x)) * 180/Math::PI
     output_split_camera angle

  end

  def render_camera_magenta_scene
     zoomFactor = 1
     offsetX = 32
     offsetY = 32

     outputs[:scene_magenta].sprites << {x: (-state.camera_magenta.x*2),
                                         y: (-state.camera_magenta.y),
                                         w: outputs[:scene].width*2,
                                         h: outputs[:scene].height,
                                         path: :scene}

  end

  def render_camera_cyan_scene
    zoomFactor = 1
    offsetX = 32
    offsetY = 32
    outputs[:scene_cyan].sprites << {x: (-state.camera_cyan.x*2),
                                     y: (-state.camera_cyan.y),
                                     w: outputs[:scene].width*2,
                                     h: outputs[:scene].height,
                                     path: :scene}
  end

  def output_split_camera angle
    #TODO: Clean this up!
    quadrant = calc_player_quadrant angle
    outputs.labels << [128,64,"#{quadrant}",8,2,255,0,255,255]
    if quadrant == 1
      set_camera_attributes(w: 640, h: 720, m_x: 640, m_y: 0, c_x: 0, c_y: 0)

    elsif quadrant == 2
      set_camera_attributes(w: 1280, h: 360, m_x: 0, m_y: 360, c_x: 0, c_y: 0)

    elsif quadrant == 3
      set_camera_attributes(w: 640, h: 720, m_x: 0, m_y: 0, c_x: 640, c_y: 0)

    elsif quadrant == 4
      set_camera_attributes(w: 1280, h: 360, m_x: 0, m_y: 0, c_x: 0, c_y: 360)

    end
  end

  def set_camera_attributes(w: 0, h: 0, m_x: 0, m_y: 0, c_x: 0, c_y: 0)
    state.camera_cyan.w = w + 64
    state.camera_cyan.h = h + 64
    outputs[:scene_cyan].width = (w) * 2
    outputs[:scene_cyan].height = h

    state.camera_magenta.w = w + 64
    state.camera_magenta.h = h + 64
    outputs[:scene_magenta].width = (w) * 2
    outputs[:scene_magenta].height = h
    outputs.sprites << {x: m_x,
                        y: m_y,
                        w: w,
                        h: h,
                        path: :scene_magenta}
    outputs.sprites << {x: c_x,
                        y: c_y,
                        w: w,
                        h: h,
                        path: :scene_cyan}
  end

  def add_trauma amount
    state.trauma = [state.trauma + amount, 1.0].min
  end

  def remove_trauma amount
    state.trauma = [state.trauma - amount, 0.0].max
  end
  #==============================================================================================
  #Input functions
  def input
    input_move_cyan
    input_move_magenta

    if inputs.keyboard.key_down.t
      add_trauma(0.5)
    elsif inputs.keyboard.key_down.y
      remove_trauma(0.1)
    end
  end

  def input_move_cyan
    if inputs.keyboard.key_held.up
      state.player_cyan.dy = 5
      state.player_cyan.orientation = "up"
    elsif inputs.keyboard.key_held.down
      state.player_cyan.dy = -5
      state.player_cyan.orientation = "down"
    else
      state.player_cyan.dy *= 0.8
    end
    if inputs.keyboard.key_held.left
      state.player_cyan.dx = -5
      state.player_cyan.orientation = "left"
    elsif inputs.keyboard.key_held.right
      state.player_cyan.dx = 5
      state.player_cyan.orientation = "right"
    else
      state.player_cyan.dx *= 0.8
    end

    outputs.labels << [128,512,"#{state.player_cyan.x.round()}",8,2,0,255,255,255]
    outputs.labels << [128,480,"#{state.player_cyan.y.round()}",8,2,0,255,255,255]
  end

  def input_move_magenta
    if inputs.keyboard.key_held.w
      state.player_magenta.dy = 5
      state.player_magenta.orientation = "up"
    elsif inputs.keyboard.key_held.s
      state.player_magenta.dy = -5
      state.player_magenta.orientation = "down"
    else
      state.player_magenta.dy *= 0.8
    end
    if inputs.keyboard.key_held.a
      state.player_magenta.dx = -5
      state.player_magenta.orientation = "left"
    elsif inputs.keyboard.key_held.d
      state.player_magenta.dx = 5
      state.player_magenta.orientation = "right"
    else
      state.player_magenta.dx *= 0.8
    end

    outputs.labels << [128,360,"#{state.player_magenta.x.round()}",8,2,255,0,255,255]
    outputs.labels << [128,328,"#{state.player_magenta.y.round()}",8,2,255,0,255,255]
  end
end

$camera_movement = CameraMovement.new

def tick args
  args.outputs.background_color = [0,0,0]
  $camera_movement.inputs  = args.inputs
  $camera_movement.outputs = args.outputs
  $camera_movement.state   = args.state
  $camera_movement.grid    = args.grid
  $camera_movement.tick
end