-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.odin
177 lines (163 loc) · 3.93 KB
/
game.odin
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
package mario
import "core:fmt"
import "core:strings"
import "core:math"
import rl "vendor:raylib"
Sprite :: struct {
pos: rl.Vector2,
type: int
}
SpriteType :: struct {
texture: int,
texture_area: rl.Rectangle
}
game : struct {
player: struct {
sprite: Sprite,
velocity: rl.Vector2,
is_jumping: bool,
collision: rl.Rectangle,
ground_collision: rl.Rectangle,
},
level: struct {
sprites: [400]Sprite,
sprites_len: int,
ground, question, brick, gpipe, gtail: int
},
textures: [2]rl.Texture,
textures_len: int,
sprite_types: [20]SpriteType,
sprite_types_len: int,
collisions: [dynamic]rl.Rectangle,
camera: rl.Vector2
}
game_init :: proc() {
player_init()
level_init()
level_1_1()
}
game_update :: proc() {
player_update()
if game.player.sprite.pos.x < 112 {
game.camera.x = 0
} else {
game.camera.x = game.player.sprite.pos.x - 112
}
}
game_render :: proc() {
level_render()
player_render()
}
game_add_collision :: proc(r: rl.Rectangle) -> int {
append(&game.collisions, r)
return len(game.collisions)-1
}
game_check_collisions :: proc(box: rl.Rectangle, collisions: []rl.Rectangle) -> int {
for coll, i in collisions {
if rl.CheckCollisionRecs(box, coll) {
return i
}
}
return -1
}
game_move_with_collision :: proc(vel: rl.Vector2, box: rl.Rectangle, collisions: []rl.Rectangle) -> rl.Vector2 {
new_vel := vel
new_box := box
// Update position and check collisions
// Collisions X
new_box.x += vel.x
for {
collider := game_check_collisions(new_box, collisions)
if collider == -1 do break
coll := collisions[collider]
coll_rec := rl.GetCollisionRec(new_box, coll)
if vel.x > 0 {
new_box.x -= coll_rec.width
new_vel.x -= coll_rec.width
} else {
new_box.x += coll_rec.width
new_vel.x += coll_rec.width
}
}
// Collisions Y
new_box.y += vel.y
for {
collider := game_check_collisions(new_box, collisions)
if collider == -1 do break
coll := collisions[collider]
coll_rec := rl.GetCollisionRec(new_box, coll)
if vel.y > 0 {
new_box.y -= coll_rec.height
new_vel.y -= coll_rec.height
} else {
new_box.y += coll_rec.height
new_vel.y += coll_rec.height
}
}
return new_vel
}
// returns normal of the side of r2 where r1 collided
game_get_collision_normal :: proc(v: rl.Vector2, r1, r2: rl.Rectangle) -> rl.Vector2 {
if r1.x > r2.x && r1.x + r1.width < r2.x + r2.width {
// between left and right
if r1.y < r2.y do return {0, -1}
else do return {0, 1}
}
else if r1.y > r2.y && r1.y + r1.height < r2.y + r2.height {
// between top and bottom
if r1.x < r2.x do return {-1, 0}
else do return {1, 0}
}
//if v.x == 0 do return
col_rec := rl.GetCollisionRec(r1, r2)
cross1 := v.x*col_rec.y - v.y*col_rec.x
cross2 := -v.x*col_rec.y - v.y*col_rec.x
if r1.x < r2.x {
assert(r1.x + r1.width > r2.x)
if r1.y < r2.y {
// top left
return {0, -1} if col_rec.width > col_rec.height else {-1, 0}
}
else {
// bottom left
return {0, 1} if col_rec.width > col_rec.height else {-1, 0}
}
}
else {
assert(r1.x < r2.x + r2.width)
if r1.y < r2.y {
// top right
return {0, -1} if col_rec.width > col_rec.height else {1, 0}
}
else {
// bottom right
return {0, 1} if col_rec.width > col_rec.height else {1, 0}
}
}
panic("unreachable")
}
game_add_texture :: proc(texture: rl.Texture) -> int {
game.textures[game.textures_len] = texture
game.textures_len += 1
return game.textures_len - 1
}
game_add_sprite_type :: proc(type: SpriteType) -> int {
game.sprite_types[game.sprite_types_len] = type
game.sprite_types_len += 1
return game.sprite_types_len - 1
}
game_render_sprite :: proc(s: Sprite) {
scale := f32(scale)
type := game.sprite_types[s.type]
area := type.texture_area
rl.DrawTexturePro(
game.textures[type.texture],
type.texture_area,
{
scale*math.floor(s.pos.x) - scale*math.trunc(game.camera.x),
scale*math.floor(s.pos.y) - scale*math.trunc(game.camera.y),
scale*area.width,
scale*area.height
},
{0,0}, 0, rl.WHITE)
}