-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.swift
289 lines (222 loc) · 6.47 KB
/
App.swift
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
//
// File.swift
// File
//
// Created by Christophe Bronner on 2021-09-04.
//
import RaylibKit
//MARK: - Application
@main struct ExamplePlatformer2D: Applet {
let startingPosition = Vector2(400, 280)
var camera: Camera2D
var player: Player
var cameraStyleIndex = 0
var cameraStyle: CameraStyle {
get { CameraStyles.all[cameraStyleIndex] }
nonmutating set { CameraStyles.all[cameraStyleIndex] = newValue }
}
init() {
Window.create(800, by: 450, title: "Example - Core - 2D Camera Platformer")
Renderer.background = .lightGray
Application.target(fps: 60)
player = Player(at: startingPosition)
camera = Camera2D(at: startingPosition)
}
mutating func update() {
player.update()
camera.zoom += Mouse.wheel * 0.05
camera.zoom.clamp(between: 0.25, and: 3)
if Keyboard.r.isPressed {
camera.zoom = 1
player.position = startingPosition
}
if Keyboard.c.isPressed {
cameraStyleIndex = CameraStyles.all.cycle(after: cameraStyleIndex)
}
cameraStyle.update(&camera, for: player)
}
func draw() {
Renderer.camera(camera) {
Renderer.color = .gray
for obstacle in environment {
Renderer2D.rectangle(obstacle)
}
Renderer2D.rectangle(at: player.position.x.toInt - 20, player.position.y.toInt - 40, size: 40, 40, color: .red)
}
Renderer.pointSize = 10
Renderer.textColor = .black
Renderer2D.text("Controls:", at: 20, 20)
Renderer2D.text("Camera mode:", at: 20, 140)
Renderer.textColor = .darkGray
Renderer2D.text("- Right/Left to move", at: 40, 40)
Renderer2D.text("- Space to jump", at: 40, 60)
Renderer2D.text("- Mouse Wheel to Zoom in-out", at: 40, 80)
Renderer2D.text("- R to reset zoom and position", at: 40, 100)
Renderer2D.text("- C to change camera mode", at: 40, 120)
Renderer2D.text(cameraStyle.name, at: 40, 160)
}
}
//MARK: - Game
let GRAVITY: Float = 400
let map = Vector2(1000, 200)
let environment = [
Rectangle(at: 0, 400, size: 1000, 600),
Rectangle(at: 300, 200, size: 400, 10),
Rectangle(at: 250, 300, size: 100, 10),
Rectangle(at: 650, 300, size: 100, 10),
]
//MARK: Player
struct Player {
let JUMP_SPEED: Float = 350
let MOVEMENT_SPEED: Float = 200
var position: Vector2
var speed: Float
var canJump: Bool
init(at position: Vector2) {
self.position = position
speed = 0
canJump = true
}
mutating func update() {
if Keyboard.left.isDown {
position.x -= MOVEMENT_SPEED * Time.delta
}
if Keyboard.right.isDown {
position.x += MOVEMENT_SPEED * Time.delta
}
if Keyboard.space.isDown && canJump {
speed = -JUMP_SPEED
canJump = false
}
let newY = position.y + speed * Time.delta
let hit = environment.first { obstacle in
obstacle.left.x <= position.x + 20
&& obstacle.right.x >= position.x - 20
&& obstacle.top.y >= position.y
&& obstacle.top.y < newY
}
if let obstacle = hit {
speed = 0
position.y = obstacle.y
canJump = true
} else {
position.y = newY
speed += GRAVITY * Time.delta
canJump = false
}
}
}
//MARK: Camera
protocol CameraStyle {
var name: String { get }
mutating func update(_ camera: inout Camera2D, for player: Player)
}
enum CameraStyles {
static var all: [CameraStyle] = [
CenterCameraStyle(),
ClampedCameraStyle(),
SmoothCameraStyle(),
EvenOutOnLandingCameraStyle(),
PushCameraStyle()
]
struct CenterCameraStyle: CameraStyle {
let name = "Follow player center"
func update(_ camera: inout Camera2D, for player: Player) {
camera.offset = Window.size / 2
camera.target = player.position
}
}
struct ClampedCameraStyle: CameraStyle {
let name = "Follow player center, but clamp to map edges"
func update(_ camera: inout Camera2D, for player: Player) {
camera.offset = Window.size / 2
camera.target = player.position
var min = Vector2(0)
var max = map
for obstacle in environment {
min.maximum(of: obstacle.topLeft)
max.minimum(of: obstacle.bottomRight)
}
min = camera.toScreen(world: min)
max = camera.toScreen(world: max)
let size = Window.size
if max.x < size.x {
camera.offset.x = size.x - (max.x - size.x / 2)
}
if max.y < size.y {
camera.offset.y = size.y - (max.y - size.y / 2)
}
if min.x > 0 {
camera.offset.x = size.x / 2 - min.x
}
if min.y > 0 {
camera.offset.y = size.y / 2 - min.y
}
}
}
struct SmoothCameraStyle: CameraStyle {
let name = "Follow player center, smoothed"
let minSpeed: Float = 30
let minEffectLength: Float = 10
let fractionSpeed: Float = 0.8
func update(_ camera: inout Camera2D, for player: Player) {
camera.offset = Window.size / 2
let diff = player.position - camera.target
let length = diff.magnitude
if length > minEffectLength {
let speed = max(fractionSpeed * length, minSpeed)
camera.target += diff * speed * Time.delta / length
}
}
}
struct EvenOutOnLandingCameraStyle: CameraStyle {
let name = "Follow player center horizontally; update player center vertically after landing"
let speed: Float = 700
private var active = false
private var target: Float = 0
mutating func update(_ camera: inout Camera2D, for player: Player) {
camera.offset = Window.size / 2
camera.target.x = player.position.x
if active {
if target > camera.target.y {
camera.target.y += speed * Time.delta
if camera.target.y > target {
camera.target.y = target
active = false
}
} else {
camera.target.y -= speed * Time.delta
if camera.target.y < target {
camera.target.y = target
active = false
}
}
} else if player.canJump && player.speed == 0 && player.position.y != camera.target.y {
active = true
target = player.position.y
}
}
}
struct PushCameraStyle: CameraStyle {
let name = "Player push camera on getting too close to screen edge"
private var bounds = Vector2(0.2)
func update(_ camera: inout Camera2D, for player: Player) {
let lowerBounds = (.one - bounds) / 2 * Window.size
let min = camera.toWorld(screen: lowerBounds)
let max = camera.toWorld(screen: (.one + bounds) / 2 * Window.size)
camera.offset = lowerBounds
if player.position.x < min.x {
camera.target.x = player.position.x
}
if player.position.y < min.y {
camera.target.y = player.position.y
}
if player.position.x > max.x {
camera.target.x = min.x + (player.position.x - max.x)
}
if player.position.y > max.y {
camera.target.y = min.y + (player.position.y - max.y)
}
}
}
}