-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement killing, basic AI, correct render ordering, basic entity co…
…mponent system, text rendering. Finish part 6 of the tutorial.
- Loading branch information
Showing
9 changed files
with
660 additions
and
137 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
tjc source/main.vx source/kernel32.vx source/sdl.vx source/sdlimage.vx source/tile.vx source/entity.vx source/window.vx source/utils.vx bin/SDL2.dll bin/SDL2_image.dll C:\Windows\System32\kernel32.dll --of=bin/main.exe --print-time --print-mem | ||
tjc source/death_functions.vx source/main.vx source/kernel32.vx source/sdl.vx source/sdlimage.vx source/tile.vx source/entity.vx source/window.vx source/utils.vx bin/SDL2.dll bin/SDL2_image.dll C:\Windows\System32\kernel32.dll --of=bin/main.exe --print-time --print-mem |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
build.cmd && cd bin && main.exe | ||
build.cmd && cd bin && start main.exe |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import main; | ||
import entity; | ||
import utils; | ||
|
||
void kill_entity(GameMap* map, EntityId eid) | ||
{ | ||
if (eid == 0) | ||
kill_player(map, eid); | ||
else | ||
kill_monster(map, eid); | ||
} | ||
|
||
void kill_player(GameMap* map, EntityId eid) | ||
{ | ||
Entity* player = &map.entities[eid]; | ||
player.char = '%'; | ||
player.r = 150; | ||
player.g = 0; | ||
player.b = 0; | ||
player.render_order = RenderOrder.CORPSE; | ||
map.game_state = GameState.PLAYER_DEAD; | ||
println("You died!"); | ||
} | ||
|
||
void kill_monster(GameMap* map, EntityId eid) | ||
{ | ||
Entity* monster = &map.entities[eid]; | ||
monster.char = '%'; | ||
monster.r = 150; | ||
monster.g = 0; | ||
monster.b = 0; | ||
monster.render_order = RenderOrder.CORPSE; | ||
monster.ai = AiId.max; | ||
monster.fighter = FighterId.max; | ||
monster.flags &= ~EntityFlags.blocks; | ||
print(monster.name); | ||
println(" is dead!"); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,95 @@ | ||
import main; | ||
import tile; | ||
import utils; | ||
import death_functions; | ||
|
||
enum EntityFlags : u8 { | ||
blocks = 1 << 0, | ||
} | ||
|
||
enum RenderOrder : u8 { | ||
CORPSE = 0, | ||
ITEM = 1, | ||
ACTOR = 2, | ||
} | ||
|
||
struct Entity | ||
{ | ||
i32 x; | ||
i32 y; | ||
u8 flags; | ||
u8 char; | ||
u8 render_order; | ||
u8 r; | ||
u8 g; | ||
u8 b; | ||
u8[] name; | ||
|
||
void move(i32 dx, i32 dy) { | ||
print("Move "); | ||
printInt(x); | ||
print(" "); | ||
printInt(y); | ||
x += dx; | ||
y += dy; | ||
print(" -> "); | ||
printInt(x); | ||
print(" "); | ||
printInt(y); | ||
print(" "); | ||
print(name); | ||
println(null); | ||
} | ||
FighterId fighter; | ||
AiId ai; | ||
|
||
bool blocks() { return flags & EntityFlags.blocks; } | ||
} | ||
|
||
alias EntityId = u16; | ||
alias EntityId = u16; | ||
|
||
struct Fighter | ||
{ | ||
EntityId owner; | ||
u16 max_hp; | ||
u16 hp; | ||
u16 defense; | ||
u16 power; | ||
} | ||
alias FighterId = u16; | ||
|
||
struct Ai | ||
{ | ||
EntityId owner; | ||
} | ||
alias AiId = u16; | ||
|
||
void do_ai(GameMap* map, EntityId monsterId, EntityId targetId) | ||
{ | ||
Entity* monster = &map.entities[monsterId]; | ||
Entity* target = &map.entities[targetId]; | ||
Tile tile = map.tiles[monster.y][monster.x]; | ||
if (tile.visible) | ||
{ | ||
Fighter* target_fighter = &map.fighters[target.fighter]; | ||
if (map.distance_between(monsterId, targetId) > 1) | ||
{ | ||
map.move_towards(monsterId, target.x, target.y); | ||
} | ||
else if (target_fighter.hp > 0) | ||
{ | ||
attack(map, monsterId, targetId); | ||
} | ||
} | ||
} | ||
|
||
void attack(GameMap* map, EntityId attackerId, EntityId targetId) | ||
{ | ||
Entity* attacker = &map.entities[attackerId]; | ||
Entity* target = &map.entities[targetId]; | ||
Fighter* attacker_fighter = &map.fighters[attacker.fighter]; | ||
Fighter* target_fighter = &map.fighters[target.fighter]; | ||
i32 damage = attacker_fighter.power - target_fighter.defense; | ||
if (damage > 0) { | ||
print(attacker.name); print(" "); printInt(attackerId); | ||
print(" attacks "); print(target.name); print(" for "); printInt(damage); print(" hit points. "); | ||
if (target_fighter.hp <= damage) { | ||
target_fighter.hp = 0; | ||
printInt(0); | ||
println(" hp left."); | ||
kill_entity(map, targetId); | ||
} | ||
else { | ||
target_fighter.hp -= cast(u16)damage; | ||
printInt(target_fighter.hp); | ||
println(" hp left."); | ||
} | ||
|
||
} else { | ||
print(attacker.name); print(" attacks "); print(target.name); println(" but does no damage."); | ||
} | ||
} |
Oops, something went wrong.