Skip to content

Commit

Permalink
More and more progress
Browse files Browse the repository at this point in the history
  • Loading branch information
tatjam committed Jul 9, 2022
1 parent c24e16b commit e456997
Show file tree
Hide file tree
Showing 31 changed files with 425 additions and 79 deletions.
1 change: 1 addition & 0 deletions res/enemies/rockman.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
sprite: res/enemies/rockman.png
fx_sprite: res/enemies/rockman_emit.png
scale_origin: [16.0, 16.0]
animations:
- name: "idle"
loop: true
Expand Down
Binary file added res/enemies/rockman_idle.mp3
Binary file not shown.
Binary file modified res/level1/map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 6 additions & 12 deletions res/level1/map.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,11 @@ areas:
color: [0, 0, 255]

points:
- name: tree1
color: [200, 0, 255]
- name: rock
color: [140, 140, 200]

- name: tree2
color: [255, 0, 255]
- name: player
color: [100, 255, 50]

- name: log_spawn
color: [0, 255, 150]

- name: box_spawn
color: [0, 255, 200]

- name: player_spawn
color: [255, 0, 0]
- name: rockman
color: [255, 100, 50]
Binary file modified res/level1/music.mp3
Binary file not shown.
Binary file added res/objects/magmarock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions res/objects/magmarock.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sprite: res/objects/magmarock.png
fx_sprite: res/objects/magmarock_emit.png
scale_origin: [60.0, 60.0]
animations:
- name: "idle"
loop: true
frames:
- clip: [0, 0, 120, 120]
duration: 1.0
Binary file added res/objects/magmarock_emit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/objects/rock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions res/objects/rock.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sprite: res/objects/rock.png
fx_sprite: none
scale_origin: [60.0, 60.0]
animations:
- name: "idle"
loop: true
frames:
- clip: [0, 0, 120, 120]
duration: 1.0
Binary file added res/objects/roll.mp3
Binary file not shown.
Binary file added res/player/fall.mp3
Binary file not shown.
Binary file added res/player/jump.mp3
Binary file not shown.
Binary file added res/player/land.mp3
Binary file not shown.
Binary file modified res/player/player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 30 additions & 6 deletions res/player/player.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
sprite: res/player/player.png
fx_sprite: res/player/player_emit.png
scale_origin: [19.0, 40.0]
animations:
- name: "idle"
loop: true
frames:
- clip: [0, 0, 40, 76]
- clip: [0, 0, 70, 76]
duration: 0.2
- clip: [40, 0, 40, 76]
- clip: [70, 0, 70, 76]
duration: 0.2
- clip: [80, 0, 40, 76]
- clip: [140, 0, 70, 76]
duration: 0.2
- clip: [120, 0, 40, 76]
- clip: [210, 0, 70, 76]
duration: 0.2
- clip: [80, 0, 40, 76]
- clip: [140, 0, 70, 76]
duration: 0.2
- clip: [40, 0, 40, 76]
- clip: [70, 0, 70, 76]
duration: 0.2
- name: "walk"
loop: true
frames:
- clip: [0, 76, 70, 76]
duration: 0.2
- clip: [70, 76, 70, 76]
duration: 0.2
- clip: [140, 76, 70, 76]
duration: 0.2
- clip: [70, 76, 70, 76]
duration: 0.2
- name: "push"
loop: true
frames:
- clip: [0, 152, 70, 76]
duration: 0.2
- clip: [70, 152, 70, 76]
duration: 0.2
- name: "fall"
loop: true
frames:
- clip: [210, 76, 70, 76]
duration: 0.2
Binary file modified res/player/player_emit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/player/steps.mp3
Binary file not shown.
4 changes: 3 additions & 1 deletion res/shader/fullscreen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ void main()
coord = vec2(vTex.x, 1.0-vTex.y);

float n = cnoise(vec3(vTex.x * 80.0, vTex.y * 160.0, t * 0.5));
vec2 off = (vec2(n, n) * 2.0 - 1.0) * 0.0025;
vec2 dist = vec2(0.5, 0.5) - vTex;
float fac = dot(dist, dist);
vec2 off = (vec2(n, n) * 2.0 - 1.0) * 0.015 * fac;

vec2 fxcoord = coord + off;

Expand Down
17 changes: 16 additions & 1 deletion src/engine/audio/audio_engine.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,19 @@ proc load_sound*(path: cstring): WavHandle =
proc play_sound*(sound: WavHandle, loop: bool = false): AudioHandle =
let handle = Soloud_play(soloud, sound.wave)
Soloud_setLooping(soloud, handle, loop.int32)
return cast[AudioHandle](handle)
return cast[AudioHandle](handle)

proc pause*(sound: AudioHandle) =
Soloud_setPause(soloud, cast[cuint](sound), 1)

proc resume*(sound: AudioHandle) =
Soloud_setPause(soloud, cast[cuint](sound), 0)

proc set_volume*(sound: AudioHandle, vol: float) =
let clipvol = max(min(vol, 1.0), 0.0)
Soloud_setVolume(soloud, cast[cuint](sound), clipvol)

proc create_sound*(sound: WavHandle, loop: bool = false): AudioHandle =
let handle = play_sound(sound, loop)
handle.pause()
return handle
2 changes: 2 additions & 0 deletions src/engine/globals.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import base/renderer as rnd
import nimgl/[glfw, opengl]

var should_quit* = false
var renderer*: Renderer = nil
var dt*: float32
var glfw_window*: GLFWWindow

var update_fnc*: proc() = nil
var render_fnc*: proc() = nil
Expand Down
57 changes: 43 additions & 14 deletions src/engine/graphics/sprite.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ type Sprite* = ref object
layer*: int
# Position, to be used with the renderer
position*: Vec2f
rotation: float
scale*: Vec2f
# Movement in pixels respect to the sprite top-left corner
# Also used for rotation
scale_origin*: Vec2f

proc create_sprite*(image: GLuint, fx_image: GLuint, width: int, height: int): Sprite =
return Sprite(texture_id: image, fx_texture_id: fx_image, texture_width: width, texture_height: height,
Expand Down Expand Up @@ -107,8 +111,18 @@ proc do_draw*(sprite: Sprite) =
sprite.shader.set_int("flip_v", if sprite.flip_v: 1 else: 0)
sprite.shader.set_vec4("tint", sprite.tint)
sprite.shader.set_vec4("clip", sprite.clip)
var stform = mat4f().scale(sprite.scale.x * sprite.texture_width.toFloat * sprite.clip.z,
sprite.scale.y * sprite.texture_height.toFloat * sprite.clip.w, 1.0)
let w = sprite.texture_width.toFloat
let h = sprite.texture_height.toFloat
# Scale is applied respect to the center, so first center, scale, and uncenter
# we also adjust the sprite so it's correctly clipped
# Note that the whole sprite is being transformed!
var stform = mat4f()
.scale(sprite.clip.z, sprite.clip.w, 1.0)
.scale(w, h, 1.0)
.translate(sprite.scale_origin.x, sprite.scale_origin.y, 0.0)
.scale(sprite.scale.x, sprite.scale.y, 1.0)
.rotate(sprite.rotation, 0, 0, 1)
.translate(-sprite.scale_origin.x, -sprite.scale_origin.y, 0.0)
sprite.shader.set_mat4("sprite_tform", stform)

if sprite.fx_texture_id != 0:
Expand All @@ -127,10 +141,14 @@ proc do_draw*(sprite: Sprite) =


proc `center_position=`*(sprite: Sprite, pos: Vec2f) =
sprite.position = vec2f(pos.x - sprite.texture_width.toFloat * 0.5, pos.y - sprite.texture_height.toFloat * 0.5)
sprite.position = vec2f(
pos.x - sprite.texture_width.toFloat * 0.5 * sprite.clip.z,
pos.y - sprite.texture_height.toFloat * 0.5 * sprite.clip.w)

proc center_position*(sprite: Sprite): Vec2f =
return vec2f(sprite.position.x + sprite.texture_width.toFloat * 0.5, sprite.position.y + sprite.texture_height.toFloat * 0.5)
return vec2f(
sprite.position.x + sprite.texture_width.toFloat * 0.5 * sprite.clip.z,
sprite.position.y + sprite.texture_height.toFloat * 0.5 * sprite.clip.w)

type AnimationFrame* = object
frame: Vec4f
Expand Down Expand Up @@ -177,9 +195,16 @@ proc shader*(sprite: AnimatedSprite): Shader =

proc `center_position=`*(sprite: AnimatedSprite, pos: Vec2f) =
sprite.sprite.center_position = pos

proc center_position*(sprite: AnimatedSprite): Vec2f =
return sprite.sprite.center_position

proc `rotation=`*(sprite: AnimatedSprite, r: float) =
sprite.sprite.rotation = r

proc rotation*(sprite: AnimatedSprite): float =
return sprite.sprite.rotation

proc animate*(sprite: var AnimatedSprite, dt: float) =
if sprite.paused:
return
Expand All @@ -196,11 +221,12 @@ proc animate*(sprite: var AnimatedSprite, dt: float) =
if anim.frame < anim.frames.len:
sprite.sprite.clip = anim.frames[anim.frame].frame

proc start_anim*(sprite: AnimatedSprite, anim: string) =
sprite.cur_anim = anim
if sprite.animations.has_key(sprite.cur_anim):
sprite.animations[sprite.cur_anim].time = 0
sprite.animations[sprite.cur_anim].frame = 0
proc start_anim*(sprite: AnimatedSprite, anim: string, restart: bool = false) =
if sprite.cur_anim != anim or restart:
sprite.cur_anim = anim
if sprite.animations.has_key(sprite.cur_anim):
sprite.animations[sprite.cur_anim].time = 0
sprite.animations[sprite.cur_anim].frame = 0

type FrameData = object
clip: array[4, int]
Expand All @@ -213,7 +239,8 @@ type AnimationData = object

type AnimatedSpriteData = object
sprite: string
fx_sprite: Option[string]
scale_origin: array[2, float]
fx_sprite: string
animations: seq[AnimationData]

# Takes the YAML of the sprite
Expand All @@ -224,15 +251,14 @@ proc create_animated_sprite*(path: string): AnimatedSprite =
s.close()

var sprite: Sprite
if data.fx_sprite.isSome:
sprite = create_sprite(data.sprite, data.fx_sprite.get)
if data.fx_sprite != "none":
sprite = create_sprite(data.sprite, data.fx_sprite)
else:
sprite = create_sprite(data.sprite)

var animations: Table[string, Animation]
var cur_anim: string
var cur_anim = data.animations[0].name
for animation in data.animations:
cur_anim = animation.name
var anim = Animation()
anim.time = 0.0
anim.frame = 0
Expand All @@ -248,5 +274,8 @@ proc create_animated_sprite*(path: string): AnimatedSprite =
animations[animation.name] = anim

sprite.clip = animations[cur_anim].frames[0].frame
sprite.scale_origin = vec2f(
data.scale_origin[0] / (sprite.texture_width.toFloat * sprite.clip.z),
data.scale_origin[1] / (sprite.texture_height.toFloat * sprite.clip.w))

return AnimatedSprite(sprite: sprite, animations: animations, cur_anim: cur_anim, paused: false)
5 changes: 4 additions & 1 deletion src/engine/launcher.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ glfwWindowHint(GLFWOpenglForwardCompat, GLFW_TRUE) # Used for Mac
glfwWindowHint(GLFWOpenglProfile, GLFW_OPENGL_CORE_PROFILE)
glfwWindowHint(GLFWResizable, GLFW_FALSE)

let glfw_window*: GLFWWindow = glfwCreateWindow(1280, 768, "Minijam 110")
glfw_window = glfwCreateWindow(1280, 768, "Minijam 110")
if glfw_window == nil:
quit(-1)

Expand Down Expand Up @@ -48,6 +48,9 @@ proc launch_game*() =
glfw_window.swapBuffers()
var new_time = glfwGetTime()
dt = new_time - last_time
if dt > 0.1:
echo "Limiting delta-time!"
dt = 0.1
last_time = new_time

if quit_fnc != nil: quit_fnc()
Expand Down
16 changes: 9 additions & 7 deletions src/engine/map/map_loader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ proc create_map_drawer*(map: Table[string, seq[Tile]]): MapDrawer =
type Map* = ref object
drawer*: MapDrawer
# We must keep the segments
segments: seq[SegmentShape]
segments*: seq[SegmentShape]
points*: Table[string, seq[Vec2f]]

proc hash(x: Vec3i): Hash =
Expand Down Expand Up @@ -264,8 +264,8 @@ proc marching_squares(tile: Image, scale: int, tile_info: Table[Vec3i, TileData]
let tex_x = (x - bounds.x) * scale
let tex_y = (y - bounds.y) * scale
# For colliders:
let tx = x.toFloat
let ty = y.toFloat
let tx = x.toFloat + 0.5
let ty = y.toFloat + 0.5

let tl = not is_pixel_white(tile, x - 1, y - 1)
let tr = not is_pixel_white(tile, x, y - 1)
Expand Down Expand Up @@ -323,12 +323,12 @@ proc marching_squares(tile: Image, scale: int, tile_info: Table[Vec3i, TileData]
v1 = v(v1.x * scale.toFloat, v1.y * scale.toFloat)
v2 = v(v2.x * scale.toFloat, v2.y * scale.toFloat)
v3 = v(v3.x * scale.toFloat, v3.y * scale.toFloat)
var segment = newSegmentShape(space.staticBody, v0, v1, 3)
var segment = newSegmentShape(space.staticBody, v0, v1, 1)
segments.add(segment)
discard space.addShape(segments[^1])
if has_two:
let segment2 = newSegmentShape(space.staticBody, v2, v3, 3)
segments.add(segment2)
let segment2 = newSegmentShape(space.staticBody, v2, v3, 1)
segments.add(segment)
discard space.addShape(segments[^1])


Expand Down Expand Up @@ -527,6 +527,7 @@ proc load_map*(map: string, scale: int, space: Space): Map =
tiles.add(marching_squares(image, scale, tile_textures, space, segments))
ground_tiles[class] = tiles


# Load points
var points: Table[string, seq[Vec2f]]
for point in map_info.points:
Expand All @@ -539,8 +540,9 @@ proc load_map*(map: string, scale: int, space: Space): Map =
if image.get_pixel(vec2i(x.int32, y.int32)) == color:
let pf = vec2f((x * scale).toFloat, (y * scale).toFloat)
points[point.name].add(pf)

let drawer = create_map_drawer(ground_tiles)
return Map(drawer: drawer, segments: segments)
return Map(drawer: drawer, segments: segments, points: points)



Loading

0 comments on commit e456997

Please sign in to comment.