Skip to content

Commit

Permalink
Implement killing, basic AI, correct render ordering, basic entity co…
Browse files Browse the repository at this point in the history
…mponent system, text rendering. Finish part 6 of the tutorial.
  • Loading branch information
MrSmith33 committed Dec 12, 2019
1 parent 7c60502 commit b0166b0
Show file tree
Hide file tree
Showing 9 changed files with 660 additions and 137 deletions.
2 changes: 1 addition & 1 deletion build.cmd
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
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Compile with `build.cmd` script.

Produces `main.exe` for win64.
Produces `bin/main.exe` for win64.

Or compile and run with `run.cmd`

Expand Down
2 changes: 1 addition & 1 deletion run.cmd
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
38 changes: 38 additions & 0 deletions source/death_functions.vx
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!");
}
92 changes: 75 additions & 17 deletions source/entity.vx
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.");
}
}
Loading

0 comments on commit b0166b0

Please sign in to comment.