Created
June 9, 2015 02:24
-
-
Save PhilipWitte/a38afecf9dddad4cdd16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | |
Sprite {.pure inheritable.} = object | |
x, y: float | |
image: string | |
Fighter = object of Sprite | |
health: float | |
strength: float | |
proc move(s:var Sprite, x, y:float) = | |
s.x += x | |
s.y += y | |
proc touch(f:var Fighter, other:Fighter) = | |
f.health -= other.strength | |
if f.health < 0: | |
echo "Ship ", f.image, " dead!" | |
proc print(f:Fighter) = | |
echo f.x, ", ", f.y, ", ", f.image, ", ", f.health, ", ", f.strength | |
proc newFighter(x, y:float, image:string, health, strength:float): Fighter = | |
when Fighter is ref: | |
result.new() | |
result.x = x | |
result.y = y | |
result.image = image | |
result.health = health | |
result.strength = strength | |
var | |
tie = newFighter(1, 2, "Tie Fighter", 1, 0.1) | |
xwing = newFighter(3, 4, "Rebel XWing", 1, 0.5) | |
echo sizeof Sprite | |
echo sizeof Fighter | |
tie.print() | |
xwing.print() | |
tie.move(10, 20) | |
xwing.move(5, 3) | |
tie.touch(xwing) | |
xwing.touch(tie) | |
tie.print() | |
xwing.print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment