Skip to content

Commit

Permalink
changed TEntity x and y to float for smooth moving
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladar committed Jun 19, 2012
1 parent 0202854 commit 10fecf9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 18 deletions.
15 changes: 15 additions & 0 deletions lib/collider.nim
Expand Up @@ -61,6 +61,12 @@ proc newBoxCollider*(x, y, w, h: int): PBoxCollider =
new(result)
init(result, int16(x), int16(y), UInt16(w), UInt16(h))


proc newBoxCollider*(pos: TPoint, w, h: int): PBoxCollider =
new(result)
init(result, pos.x, pos.y, UInt16(w), UInt16(h))


# CircleCollider

proc init*(obj: PCircleCollider, x, y: int16, r: UInt16) =
Expand All @@ -76,6 +82,11 @@ proc newCircleCollider*(x, y, r: int): PCircleCollider =
new(result)
init(result, int16(x), int16(y), UInt16(r))

proc newCircleCollider*(pos: TPoint, r: int): PCircleCollider =
new(result)
init(result, pos.x, pos.y, UInt16(r))


# MaskCollider

proc init*(obj: PMaskCollider, mask: PMask, x, y: int16) =
Expand All @@ -87,6 +98,10 @@ proc newMaskCollider*(mask: PMask, x, y: int): PMaskCollider =
new(result)
init(result, mask, int16(x), int16(y))

proc newMaskCollider*(mask: PMask, pos: TPoint): PMaskCollider =
new(result)
init(result, mask, pos.x, pos.y)


# methods

Expand Down
40 changes: 40 additions & 0 deletions lib/common.nim
Expand Up @@ -15,6 +15,46 @@ const UE_UPDATE_FPS*: cint = 2
proc distance*(a: TPoint, b: TPoint): float32 =
return sqrt(pow(toFloat(b.x) - toFloat(a.x), 2.0) + pow(toFloat(b.y) - toFloat(a.y), 2.0))

proc distance*(ax, ay: int16, b: TPoint): float32 {.inline.} =
return distance((ax, ay), b)

proc distance*(a: TPoint, bx, by: int16): float32 {.inline.} =
return distance(a, (bx, by))

proc distance*(ax, ay: int, b: TPoint): float32 {.inline.} =
return distance((ax.int16, ay.int16), b)

proc distance*(a: TPoint, bx, by: int): float32 {.inline.} =
return distance(a, (bx.int16, by.int16))


# angle direction from one to other point
proc direction*(a: TPoint, b: TPoint): float64 =
let dx = float(a.x - b.x)
let dy = float(a.y - b.y)
return -(arctan2(dy, dx) / pi) * 180.0 + 90.0

proc direction*(ax, ay: int16, b: TPoint): float64 {.inline.} =
return direction((ax, ay), b)

proc direction*(a: TPoint, bx, by: int16): float64 {.inline.} =
return direction(a, (bx, by))

proc direction*(ax, ay: int, b: TPoint): float64 {.inline.} =
return direction((ax.int16, ay.int16), b)

proc direction*(a: TPoint, bx, by: int): float64 {.inline.} =
return direction(a, (bx.int16, by.int16))


# convert degrees to radians
template toRad*(a: float64): expr =
(a * pi / 180.0)

# convert radians to degrees
template toDeg*(a: float64): expr =
(a * 180.0 / pi)


# create TColor
proc color*(r: int, g: int, b: int): TColor {.inline.} =
Expand Down
58 changes: 40 additions & 18 deletions lib/entity.nim
@@ -1,12 +1,13 @@
import
sdl,
sdl, math,
common, engine, image, collider

type

PEntity* = ref TEntity
TEntity* = object of TObject
fX, fY: int16
fX, fY: float
fXi, fYi: int16
fLayer*: int
updateLayer*, deleteEntity*: bool
kind*: string
Expand All @@ -21,14 +22,16 @@ proc free*(obj: PEntity) =

proc init*(obj: PEntity,
graphic: PImage,
x: int16 = 0'i16,
y: int16 = 0'i16,
x: float = 0.0,
y: float = 0.0,
layer: int = 0,
kind: string = "",
collider: PCollider = nil
) =
obj.fX = x
obj.fXi = x.round.int16
obj.fY = y
obj.fYi = y.round.int16
obj.fLayer = layer
obj.updateLayer = false
obj.deleteEntity = false
Expand All @@ -38,19 +41,20 @@ proc init*(obj: PEntity,


proc newEntity*(graphic: PImage,
x: int = 0,
y: int = 0,
x: float = 0.0,
y: float = 0.0,
layer: int = 0,
kind: string = "",
collider: PCollider = nil
): PEntity =
new(result, free)
init(result, graphic, int16(x), int16(y), layer, kind, collider)
init(result, graphic, x, y, layer, kind, collider)


# render

proc renderEntity*(obj: PEntity) =
obj.graphic.blit(obj.fX, obj.fY)
obj.graphic.blit(obj.fXi, obj.fYi)

method render*(obj: PEntity) {.inline.} =
obj.renderEntity()
Expand All @@ -66,33 +70,51 @@ method update*(obj: PEntity) {.inline.} =
# get/set methods

method getRect*(obj: PEntity): TRect {.inline.} =
result.x = obj.fX + obj.graphic.x
result.y = obj.fY + obj.graphic.y
result.x = obj.fXi + obj.graphic.x
result.y = obj.fYi + obj.graphic.y
result.w = UInt16(obj.graphic.surface.w)
result.h = UInt16(obj.graphic.surface.h)


method getCircle*(obj: PEntity): TCircle {.inline.} =
result.r = UInt16(min(obj.graphic.surface.w, obj.graphic.surface.h)/2)
result.x = obj.fX + result.r + obj.graphic.x
result.y = obj.fY + result.r + obj.graphic.y
result.x = obj.fXi + result.r + obj.graphic.x
result.y = obj.fYi + result.r + obj.graphic.y

# x
method x*(obj: PEntity): int {.inline.} =
method x*(obj: PEntity): float {.inline.} =
return obj.fX

method xi*(obj: PEntity): int {.inline.} =
return obj.fXi

method `x=`*(obj: PEntity, value: int) {.inline.} =
obj.fX = int16(value)
obj.collider.x = obj.fX + obj.graphic.x
obj.fX = float(value)
obj.fXi = int16(value)
obj.collider.x = obj.fXi + obj.graphic.x

method `x=`*(obj: PEntity, value: float) {.inline.} =
obj.fX = value
obj.fXi = value.round.int16
obj.collider.x = obj.fXi + obj.graphic.x


# y
method y*(obj: PEntity): int {.inline.} =
method y*(obj: PEntity): float {.inline.} =
return obj.fY

method yi*(obj: PEntity): int {.inline.} =
return obj.fYi

method `y=`*(obj: PEntity, value: int) {.inline.} =
obj.fY = int16(value)
obj.collider.y = obj.fY + obj.graphic.y
obj.fY = float(value)
obj.fYi = int16(value)
obj.collider.y = obj.fY.round.int16 + obj.graphic.y

method `y=`*(obj: PEntity, value: float) {.inline.} =
obj.fY = value
obj.fYi = value.round.int16
obj.collider.y = obj.fYi + obj.graphic.y


# layer
Expand Down

1 comment on commit 10fecf9

@Vladar4
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And added some helper functions too.

Please sign in to comment.