Skip to content

Commit

Permalink
More progress
Browse files Browse the repository at this point in the history
  • Loading branch information
tatjam committed Jul 10, 2022
1 parent e456997 commit 47a2086
Show file tree
Hide file tree
Showing 21 changed files with 299 additions and 134 deletions.
Binary file added res/enemies/rockman_hurt.mp3
Binary file not shown.
Binary file added res/level1/back.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 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.
11 changes: 11 additions & 0 deletions res/level1/map.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ seed: 1234

images:
- res/level1/map.png
- res/level1/map_ent.png

tiles:
- class: ground
Expand All @@ -19,6 +20,16 @@ tiles:
noise: 3.0
color: [200, 200, 200]

- class: B@back
texture: res/level1/back.png
noise: 1.0
color: [240, 240, 240]

- class: B@backwood
texture: res/level1/wood_back.png
noise: 0.0
color: [160, 160, 160]

areas:
- name: water
color: [0, 0, 255]
Expand Down
Binary file added res/level1/map_ent.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/level1/wood_back.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/attack.mp3
Binary file not shown.
Binary file added res/player/hit.mp3
Binary file not shown.
Binary file added res/player/hit_connect.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.
38 changes: 37 additions & 1 deletion res/player/player.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,40 @@ animations:
loop: true
frames:
- clip: [210, 76, 70, 76]
duration: 0.2
duration: 0.2
- name: "attack"
loop: false
frames:
- clip: [140, 152, 70, 76]
duration: 0.1
- clip: [210, 152, 70, 76]
duration: 0.1
- clip: [280, 152, 70, 76]
duration: 0.1
- clip: [280, 76, 70, 76]
duration: 0.2
- clip: [280, 0, 70, 76]
duration: 0.3
- name: "toss"
loop: false
frames:
- clip: [350, 0, 70, 76]
duration: 0.6
- clip: [350, 76, 70, 76]
duration: 0.4
- clip: [350, 152, 70, 76]
duration: 0.1
- clip: [350, 76, 70, 76]
duration: 0.1
- clip: [350, 152, 70, 76]
duration: 0.1
- clip: [350, 76, 70, 76]
duration: 0.1
- clip: [350, 152, 70, 76]
duration: 0.1
- clip: [350, 76, 70, 76]
duration: 0.1
- clip: [350, 152, 70, 76]
duration: 0.1
- clip: [350, 76, 70, 76]
duration: 0.1
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.
4 changes: 4 additions & 0 deletions src/engine/graphics/sprite.nim
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ proc rotation*(sprite: AnimatedSprite): float =
return sprite.sprite.rotation

proc animate*(sprite: var AnimatedSprite, dt: float) =
if sprite.animations.has_key(sprite.cur_anim):
var anim = sprite.animations[sprite.cur_anim]
if anim.frame < anim.frames.len:
sprite.sprite.clip = anim.frames[anim.frame].frame
if sprite.paused:
return

Expand Down
107 changes: 55 additions & 52 deletions src/engine/map/map_loader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ proc bilinear_interp(tl: Vec3i, tr: Vec3i, bl: Vec3i, br: Vec3i, h: float, v: fl
return vec3i(sum.x.int32, sum.y.int32, sum.z.int32)

proc marching_squares(tile: Image, scale: int, tile_info: Table[Vec3i, TileData], space: Space,
segments: var seq[SegmentShape]): Tile =
segments: var seq[SegmentShape], collide: bool): Tile =
let bounds = get_bounds(tile)
echo "Processing tile of size: " & $(bounds.z - bounds.x + 1) & "x" & $(bounds.w - bounds.y + 1)
let pos = vec2i(bounds.x * scale.int32, bounds.y * scale.int32)
echo bounds
var image = create_image((bounds.z - bounds.x + 2) * scale, (bounds.w - bounds.y + 2) * scale)
var image = create_image((bounds.z - bounds.x + 2) * scale, (bounds.w - bounds.y + 2) * scale, true)
var body = newBody(1.0, 1.0)

# We travel the corners of the pixels in the map texture
Expand All @@ -279,57 +279,58 @@ proc marching_squares(tile: Image, scale: int, tile_info: Table[Vec3i, TileData]
continue

# Generate the segment(s)
if ms_case != 15:
var v0: Vect
var v1: Vect
var has_two: bool
var v2: Vect
var v3: Vect
case ms_case:
of 1, 14:
v0 = v(tx - 0.5, ty + 0.5)
v1 = v(tx, ty + 1.0)
of 2, 13:
v0 = v(tx + 0.5, ty + 0.5)
v1 = v(tx, ty + 1.0)
of 3, 12:
v0 = v(tx - 0.5, ty)
v1 = v(tx + 0.5, ty)
of 4, 11:
v0 = v(tx + 0.5, ty)
v1 = v(tx, ty - 0.5)
of 5:
v0 = v(tx - 0.5, ty)
v1 = v(tx, ty - 0.5)
has_two = true
v2 = v(tx + 0.5, ty + 0.5)
v1 = v(tx, ty + 1.0)
of 6, 9:
v0 = v(tx, ty - 0.5)
v1 = v(tx, ty + 0.5)
of 7, 8:
v0 = v(tx - 0.5, ty)
v1 = v(tx, ty - 0.5)
of 10:
v0 = v(tx - 0.5, ty + 0.5)
v1 = v(tx, ty + 1.0)
has_two = true
v2 = v(tx + 0.5, ty)
v3 = v(tx, ty - 0.5)
else:
discard

v0 = v(v0.x * scale.toFloat, v0.y * scale.toFloat)
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, 1)
segments.add(segment)
discard space.addShape(segments[^1])
if has_two:
let segment2 = newSegmentShape(space.staticBody, v2, v3, 1)
if collide:
if ms_case != 15:
var v0: Vect
var v1: Vect
var has_two: bool = false
var v2: Vect
var v3: Vect
case ms_case:
of 1, 14:
v0 = v(tx - 0.5, ty + 0.5)
v1 = v(tx, ty + 1.0)
of 2, 13:
v0 = v(tx + 0.5, ty + 0.5)
v1 = v(tx, ty + 1.0)
of 3, 12:
v0 = v(tx - 0.5, ty)
v1 = v(tx + 0.5, ty)
of 4, 11:
v0 = v(tx + 0.5, ty)
v1 = v(tx, ty - 0.5)
of 5:
v0 = v(tx - 0.5, ty)
v1 = v(tx, ty - 0.5)
has_two = true
v2 = v(tx + 0.5, ty + 0.5)
v3 = v(tx, ty + 1.0)
of 6, 9:
v0 = v(tx, ty - 0.5)
v1 = v(tx, ty + 0.5)
of 7, 8:
v0 = v(tx - 0.5, ty)
v1 = v(tx, ty - 0.5)
of 10:
v0 = v(tx - 0.5, ty + 0.5)
v1 = v(tx, ty + 1.0)
has_two = true
v2 = v(tx + 0.5, ty)
v3 = v(tx, ty - 0.5)
else:
discard

v0 = v(v0.x * scale.toFloat, v0.y * scale.toFloat)
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, 1)
segments.add(segment)
discard space.addShape(segments[^1])
if has_two:
var segment2 = newSegmentShape(space.staticBody, v2, v3, 1)
segments.add(segment2)
discard space.addShape(segments[^1])


var tldata, trdata, bldata, brdata: Option[TileData]
Expand Down Expand Up @@ -488,6 +489,7 @@ proc extract_separated_tiles(imageo: Image): seq[Image] =

return tiles

# classes of name B@wathever will not have collision (they are useful as backgrounds)
proc load_map*(map: string, scale: int, space: Space): Map =
let map_info = load_map_info(map)
# Load images
Expand Down Expand Up @@ -524,7 +526,8 @@ proc load_map*(map: string, scale: int, space: Space): Map =
for class, images in ground_tiles_img:
var tiles: seq[Tile]
for image in images:
tiles.add(marching_squares(image, scale, tile_textures, space, segments))
tiles.add(marching_squares(image, scale, tile_textures, space, segments,
not(class[0] == 'B' and class[1] == '@')))
ground_tiles[class] = tiles


Expand Down
Empty file.
36 changes: 29 additions & 7 deletions src/game/entities/enemy.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include ../../engine/base

import enemies/rockman
import player
import physical_object
import random

import ../userdata
Expand All @@ -17,31 +17,46 @@ type
retreat_goal: float
retreat: bool

dead*: bool
health: float
hurt_wav: WavHandle
sprite*: AnimatedSprite
phys_body: Body
phys_shape: Shape
user_data: UserData
user_data*: UserData

proc create_rockman*(pos: Vec2f, space: Space): Enemy =
proc create_rockman*(pos: Vec2f, space: Space, id: int): Enemy =
result = new(Enemy)
result.sprite = create_animated_sprite("res/enemies/rockman.yaml")
let mass = 50.0
let moment = momentforBox(mass, 31.0, 31.0)

result.phys_body = space.addBody(newBody(mass, moment))
result.phys_shape = space.addShape(newBoxShape(result.phys_body, 31.0, 31.0, 0.0))
result.user_data = make_enemy_userdata(addr result)
result.phys_shape.userData = addr result.user_data
result.phys_shape.friction = 0.8

result.phys_body.position = v(pos.x, pos.y)
result.hurt_wav = load_sound("res/enemies/rockman_hurt.mp3")
result.user_data = make_enemy_userdata(id)
result.phys_shape.userData = addr result.user_data

result.kind = ekRockman
result.health = 2.0


proc die(this: var Enemy, objects: var seq[PhysicalObject], space: Space) =
space.removeShape(this.phys_shape)
space.removeBody(this.phys_body)
this.dead = true
discard

proc update*(this: var Enemy, player: Player) =
proc update*(this: var Enemy, player: Player, objects: var seq[PhysicalObject], space: Space) =
this.sprite.center_position = vec2f(this.phys_body.position.x, this.phys_body.position.y)
this.sprite.animate(dt)

if this.health <= 0.0:
this.die(objects, space)

if this.kind == ekRockman:
# Simple moving towards player behaviour, with random retreats
if this.retreat:
Expand All @@ -59,10 +74,17 @@ proc update*(this: var Enemy, player: Player) =
player_dir /= player_dist
if player_dist < 300.0:
if this.retreat:
this.phys_body.velocity = v(player_dir.x * 80.0, this.phys_body.velocity.y)
this.phys_body.velocity = v(player_dir.x * 70.0, this.phys_body.velocity.y)
else:
this.phys_body.velocity = v(-player_dir.x * 60.0, this.phys_body.velocity.y)


proc hurt*(this: var Enemy, point: Vect) =
this.health -= 1.0
let p = this.phys_body.position
this.phys_body.applyImpulseAtWorldPoint(v(0, -5000.0), p)
discard this.hurt_wav.play_sound()

proc draw*(this: var Enemy) =
renderer.draw(this.sprite)

29 changes: 0 additions & 29 deletions src/game/entities/objects/rocks.nim

This file was deleted.

32 changes: 29 additions & 3 deletions src/game/entities/physical_object.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,40 @@ type
phys_shape: Shape
user_data: UserData

include objects/rocks

proc update*(this: var PhysicalObject) =
this.sprite.center_position = vec2f(this.phys_body.position.x, this.phys_body.position.y)
this.sprite.rotation = this.phys_body.angle
echo this.phys_body.angle
this.sprite.animate(dt)

proc draw*(this: var PhysicalObject) =
renderer.draw(this.sprite)

proc create_rock*(pos: Vec2f, space: Space, id: int): PhysicalObject =
result = new(PhysicalObject)
result.sprite = create_animated_sprite("res/objects/rock.yaml")
let mass = 50.0
let moment = momentForCircle(mass, 0.0, 58.0, vzero)

result.phys_body = space.addBody(newBody(mass, moment))
result.phys_shape = space.addShape(newCircleShape(result.phys_body, 58.0, vzero))
result.user_data = make_enemy_userdata(id)
result.phys_shape.userData = addr result.user_data
result.phys_body.position = v(pos.x, pos.y)
result.phys_shape.friction = 0.1

result.kind = okRock

proc create_magmarock*(pos: Vec2f, space: Space, id: int): PhysicalObject =
result = new(PhysicalObject)
result.sprite = create_animated_sprite("res/objects/magmarock.yaml")
let mass = 60.0
let moment = momentForCircle(mass, 0.0, 58.0, vzero)

result.phys_body = space.addBody(newBody(mass, moment))
result.phys_shape = space.addShape(newCircleShape(result.phys_body, 58.0, vzero))
result.user_data = make_enemy_userdata(id)
result.phys_shape.userData = addr result.user_data
result.phys_body.position = v(pos.x, pos.y)
result.phys_shape.friction = 0.1

result.kind = okMagmaRock
Loading

0 comments on commit 47a2086

Please sign in to comment.