summaryrefslogtreecommitdiffhomepage
path: root/run.rb
blob: 51979789f875e05c5628aa1223f54665730592b7 (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
# frozen_string_literal: true

require 'ruby2d'
require_relative 'lib/camera/camera'
require_relative 'house'
require_relative 'room'

Window.set(icon: './assets/blobcoolthink.png',
           width: 1280,
           height: 720,
           background: 'blue')


@player = Sprite.new('./assets/sprites/mainblob-128.png',
                     x: 1920 / 1.1,
                     y: 1080 / 1.1,
                     width: 50,
                     height: 50,
                     clip_width: 128,
                     loop: true,
                     time: 1,
                     z: 99,
                     animations: {
                       walk: 0...60,
                       stand: 60...61
                     })
@shadow = Image.new(
  'assets/blobshadow.png',
  width: 52,
  height: 10,
  z: 4
)
Camera << @shadow
@player.play animation: :walk, loop: true
Camera << @player

# UI
Rectangle.new(
  width: 350,
  height: 135,
  color: 'navy',
  z: 100
)
@ui_pos_cam = Text.new(
  'pos: 0,0',
  x: 10,
  y: 10,
  color: 'teal',
  z: 101
)
@ui_pos_ply = Text.new(
  'pos: 0,0',
  x: 10,
  y: 40,
  color: 'teal',
  z: 101
)
@ui_zoom = Text.new(
  'zoom: 0',
  x: 10,
  y: 70,
  color: 'lime',
  z: 101
)
@ui_rotation = Text.new(
  'rotation: 0',
  x: 10,
  y: 100,
  color: 'lime',
  z: 101
)
Rectangle.new(
  x: (Window.width - 120),
  width: 120,
  height: 45,
  color: 'navy',
  z: 100
)
@ui_fps = Text.new(
  'fps: 60.00',
  x: (Window.width - 110),
  y: 10,
  color: 'teal',
  z: 101
)



# How fast the player can move
@speed = 5

# Initializing
@player_movement_x = 0
@player_movement_y = 0
@pressed_space = false
@scene_transition_into = false
@scene_transition_out = false
@indoors = false
@house = nil
@room = nil

on :key do |event|
  if event.key == 'w'
    @player_movement_y -= @speed unless @scene_transition_into || @scene_transition_out
  end
  if event.key == 's'
    @player_movement_y += @speed unless @scene_transition_into || @scene_transition_out
  end
  if event.key == 'd'
    @player_movement_x += @speed unless @scene_transition_into || @scene_transition_out
  end
  if event.key == 'a'
    @player_movement_x -= @speed unless @scene_transition_into || @scene_transition_out
  end
  if event.key == 'space'
    @pressed_space = true unless @scene_transition_into || @scene_transition_out
  end
  if event.key == 'q'
    Camera.angle += 1 unless @scene_transition_into || @scene_transition_out
  end
  if event.key == 'e'
    Camera.angle -= 1 unless @scene_transition_into || @scene_transition_out
  end
  if event.key == 'r'
    unless @scene_transition_into || @scene_transition_out || Camera.angle.zero?
      if Camera.angle <= 180
        if Camera.angle > 10
          Camera.angle -= 10
        else 
          Camera.angle -=1
        end
      elsif
        if Camera.angle < 350
          Camera.angle += 10
        else
          Camera.angle += 1
        end
      end
    end
  end
end

update do
  if (@player.x > 2371 && @player_movement_x.positive?) || (@player.x.negative? && @player_movement_x.negative?)
    @player_movement_x = 0
  end
  if (@player.y > 1608 && @player_movement_y.positive?) || (@player.y.negative? && @player_movement_y.negative?)
    @player_movement_y = 0
  end
  if !@player_movement_y.zero? == !@player_movement_x.zero?
    @player_movement_x /= 1.4141
    @player_movement_y /= 1.4141
  end
  @player.x += @player_movement_x
  @player.y += @player_movement_y
  if @player_movement_x.negative?
    @player.play animation: :walk, loop: true
  elsif @player_movement_x.positive? || !@player_movement_y.zero?
    @player.play animation: :walk, loop: true, flip: :vertical
  else
    @player.play animation: :stand
  end

  if !@scene_transition_into && !@scene_transition_out
    Camera.zoom += ((-[Math.sqrt(((@player.x + (@player.width / 2) - Camera.x)**2) + ((@player.y + (@player.width / 2) - Camera.y)**2)), 350].min * 0.004) + 2 - Camera.zoom) * 0.25
    Camera.x += (@player.x + (@player.width / 2) - Camera.x) * 0.025
    Camera.y += (@player.y + (@player.height / 2)- Camera.y) * 0.025
  elsif @scene_transition_into
    if Camera.zoom < 250
      Camera.zoom *= 1.05
      Camera.angle += 5
    else
      @scene_transition_into = false
      @scene_transition_out = true
      if @room.nil?
        @house.remove
        @house = nil
        @indoors = true
      else
        @room.remove
        @room = nil
        @indoors = false
      end
      Camera.remove @background
      @background.remove
      @background = nil
    end
    Camera.x += (@player.x + (@player.width / 2) - Camera.x) * 0.25
    Camera.y += (@player.y + (@player.height / 2) - Camera.y) * 0.25
  elsif @scene_transition_out
    if !((Camera.zoom <= (@zoom_transition * 1.001)) && (Camera.angle == @angle_transition))
      Camera.zoom /= 1.05
      Camera.angle -= 5
    else
      @scene_transition_out = false
    end
  end

  if @house.nil? && !@indoors
    @house = House.new(750, 300)
    @background = Image.new(
      'assets/background.png',
      x: 100, y: 100,
      z: -1
    )
    Camera << @background
  elsif @room.nil? && @indoors
    @room = Room.new(750,300)
    @background = Rectangle.new(
      color: 'black',
      x: 0,
      y: 0,
      width: 1920,
      height: 1080,
      z: -1
    )
  end
  @shadow.x = @player.x - 2
  @shadow.y = @player.y + 42

  Camera.remove @house_text
  @house_text&.remove
  if @indoors
    @house_text = @room.visted_by?(@player)
  else
    @house_text = @house.visted_by?(@player)
  end
  unless @house_text.nil?
    Camera << @house_text
    @house_text.center = true
  end
  if !@house_text.nil? && @pressed_space && !@scene_transition_into && !@scene_transition_out
    @scene_transition_into = true
    @angle_transition = Camera.angle
    @zoom_transition = Camera.zoom
  end
  @ui_pos_cam.text = "Camera Position: #{Camera.x.round(1)}, #{Camera.y.round(1)}"
  @ui_pos_ply.text = "Player Position: #{@player.x.round(1)}, #{@player.y.round(1)}"
  @ui_zoom.text = "Zoom: #{Camera.zoom.round(3)}"
  @ui_fps.text = "FPS: #{Window.fps.round(2)}"
  @ui_rotation.text = "Angle: #{Camera.angle}"
  @player_movement_x = 0
  @player_movement_y = 0
  @pressed_space = false

  Camera.redraw
end
show