A loooooooot of refactoring to make the function calls easier and more consistant

This commit is contained in:
SlyVTT 2023-08-11 08:54:04 +02:00
parent 1bf630dfcb
commit e012e5a428
7 changed files with 85 additions and 228 deletions

View file

@ -1,188 +0,0 @@
from random import randint
import fxconv
import json
import pathlib
import csv
import os
def convert(input, output, params, target):
if params["custom-type"] == "map":
#convert_map(input, output, params, target)
#return 0
return 1
elif params["custom-type"] == "world":
convert_world(input, output, params, target)
return 0
else:
return 1
def convert_world(input, output, params, target):
print( "WE ARE COMPUTING THE WORLD", input )
data = json.load(open(input, "r"))
nbMaps = ["fileName" in i for i in data["maps"]].count(True)
print( "We have to treat ", nbMaps, " maps")
print( "So let's go ... ")
structWorld = fxconv.Structure()
for i in range(nbMaps):
nameMap = data["maps"][i]["fileName"].replace(".tmx","")
nameMapFree = nameMap.split("/")[-1]
#count the number of "back" (cd ..) to locate the map on the computer
nbRetour = nameMap.count("..")+1
#create the map absolute path
nameTMX = "/".join(input.split("/")[:-nbRetour]) + "/" + nameMap + ".tmx"
nameJSON = "/".join(input.split("/")[:-nbRetour]) + "/" + nameMap + ".json"
commandline = 'tiled --export-map json ' + nameTMX + ' ' + nameJSON
print( "TILED COMMAND LINE FOR MAPS : ", commandline )
os.system( commandline )
mapPath = "/".join(input.split("/")[:-nbRetour]) + "/" + nameMap + ".json"
print("Map ", i , " name : ", mapPath )
xmin = data["maps"][i]["x"]
print( "xmin = ", xmin )
structWorld += fxconv.u16( xmin ) #xmin parameter
ymin = data["maps"][i]["y"]
print( "ymin = ", ymin )
structWorld += fxconv.u16( ymin ) #ymin parameter
xmax = data["maps"][i]["x"] + data["maps"][i]["width"]
print( "xmax = ", xmax )
structWorld += fxconv.u16( xmax ) #xmax parameter
ymax = data["maps"][i]["y"] + data["maps"][i]["height"]
print( "ymax = ", ymax )
structWorld += fxconv.u16( ymax ) #ymax parameter
map = convert_map( mapPath, output, params, target )
print( "Map = ", map )
structWorld += map
#generate !
fxconv.elf(structWorld, output, "_" + params["name"], **target)
def convert_map(input, output, params, target):
print( "WE ARE COMPUTING THE MAP : ", input )
data = json.load(open(input, "r"))
#find the tileset in use. it's a relative path (like ../tileset.tsx)
nameTileset = data["tilesets"][0]["source"].replace(".tsx","")
print(nameTileset)
#the name of the tileset without the .something
nameTilesetFree = nameTileset.split("/")[-1]
#count the number of "back" (cd ..) to locate the tileset on the computer
nbRetour = nameTileset.count("..")+1
#create the tileset absolute path
tilesetTSX = "/".join(input.split("/")[:-nbRetour]) + "/" + nameTileset + ".tsx"
tilesetJSON = "/".join(input.split("/")[:-nbRetour]) + "/" + nameTileset + ".json"
commandline = 'tiled --export-tileset json ' + tilesetTSX + ' ' + tilesetJSON
print( "TILED COMMAND LINE FOR TILESET : ", commandline )
os.system( commandline )
tileset = open(tilesetJSON, "r")
data_tileset = json.load(tileset)
tileset_size = data_tileset.get("columns")
tileset.close()
#find the ID of the first tile in the walkable tileset ()
indexWalkable = data["tilesets"][1]["firstgid"]
print(indexWalkable)
#Extract from the json the width, height
w, h = data["width"], data["height"]
#nbTileLayer is the number of "true" layers (without ObjectsLayer)
nbTilelayer = ["data" in i for i in data["layers"]].count(True) - 1
print( nbTilelayer)
#index of the various layers (may change from one map to another)
layer_walkable = 0
layer_foreground = 0
layer_background = 0
#create the structure of the map
structMap = fxconv.Structure()
structMap += fxconv.u16(w) + fxconv.u16(h) + fxconv.u16(nbTilelayer)
structMap += fxconv.u16(tileset_size)
structMap += fxconv.ref(f"img_{nameTilesetFree}")
#extraction of the data contained in the layer "Walkable" of the map
for i in range(nbTilelayer+1):
datavalid = data["layers"][i]
if datavalid["name"]=="Walkable":
layer_walkable = i
print( "Walkable Tile Data in layer : ", layer_walkable)
break
elif i==nbTilelayer:
printf( "ERROR : No Walkable layer data !!!" )
walk_data = bytes()
layer = data["layers"][layer_walkable]
for tile in layer["data"]:
#print( tile )
if tile == 0: walk_data += fxconv.u8(tile) #if walkable_data = 0 then it is a blanck cell so nothing to change
else : walk_data += fxconv.u8(tile-indexWalkable) #if !=0 than we need to shift the tile number by considering the first tileID (given by indexwalkable)
structMap += fxconv.ptr(walk_data)
#extraction of the data contained in the layer "Background" and "Foreground" of the map
#import the Background layer of the map
for i in range(nbTilelayer+1):
datavalid = data["layers"][i]
if datavalid["name"]=="Background":
layer_background = i
print( "Background Tile Data in layer : ", layer_background)
break
elif i==nbTilelayer:
printf( "ERROR : No Background layer data !!!" )
layer_data = bytes()
layer = data["layers"][layer_background]
for tile in layer["data"]:
layer_data += fxconv.u16(tile-1)
structMap += fxconv.ptr(layer_data)
#import the foreground layer of the map
for i in range(nbTilelayer+1):
datavalid = data["layers"][i]
if datavalid["name"]=="Foreground":
layer_foreground = i
print( "Foreground Tile Data in layer : ", layer_foreground)
break
elif i==nbTilelayer:
printf( "ERROR : No Foreground layer data !!!" )
layer_data = bytes()
layer = data["layers"][layer_foreground]
for tile in layer["data"]:
layer_data += fxconv.u16(tile-1)
structMap += fxconv.ptr(layer_data)
#generate !
#fxconv.elf(structMap, output, "_" + params["name"], **target)
return structMap

View file

@ -12,9 +12,9 @@ void game_logic(Game *game) {
void draw(Game *game) { void draw(Game *game) {
/* Draw everything. */ /* Draw everything. */
render_map_by_layer(&game->player, game->map_level, BACKGROUND); render_map_by_layer(game, BACKGROUND);
player_draw(&game->player); player_draw(game);
render_map_by_layer(&game->player, game->map_level, FOREGROUND); render_map_by_layer(game, FOREGROUND);
} }
/* Key management */ /* Key management */
@ -32,16 +32,20 @@ void get_inputs(Game *game) {
if(keydown(KEY_EXIT)) game->exittoOS = true; if(keydown(KEY_EXIT)) game->exittoOS = true;
/* Player actions - Prototypes in player.h and implementation in player.c */ /* Player actions - Prototypes in player.h and implementation in player.c */
if(keydown(KEY_LEFT)) player_move(game->map_level, &game->player, D_LEFT); if(keydown(KEY_LEFT)) player_move(game, D_LEFT);
if(keydown(KEY_RIGHT)) player_move(game->map_level, &game->player, D_RIGHT); if(keydown(KEY_RIGHT)) player_move(game, D_RIGHT);
if(keydown(KEY_UP)) player_move(game->map_level, &game->player, D_UP); if(keydown(KEY_UP)) player_move(game, D_UP);
if(keydown(KEY_DOWN)) player_move(game->map_level, &game->player, D_DOWN); if(keydown(KEY_DOWN)) player_move(game, D_DOWN);
if(keydown(KEY_SHIFT)) player_action(&game->player); if(keydown(KEY_SHIFT)) player_action(game);
/* Display Debug Information on screen */
#if DEBUGMODE
if(keydown(KEY_F1)) game->debug_map = !game->debug_map; if(keydown(KEY_F1)) game->debug_map = !game->debug_map;
if(keydown(KEY_F2)) game->debug_player = !game->debug_player; if(keydown(KEY_F2)) game->debug_player = !game->debug_player;
#endif
/* if USB is enabled - keybinding for screencapture */ /* if USB is enabled - keybinding for screencapture */
#if USB_FEATURE #if USB_FEATURE
if(keydown(KEY_7)) game->screenshot = true; if(keydown(KEY_7)) game->screenshot = true;

View file

@ -123,7 +123,7 @@ int main(void) {
/* render the map */ /* render the map */
draw(&game); draw(&game);
#if DEBUGMODE #if DEBUGMODE && FXCG50
if (game.debug_map) if (game.debug_map)
{ {
dfont( NULL ); dfont( NULL );

View file

@ -1,9 +1,14 @@
#include "map.h" #include "map.h"
#include "config.h" #include "config.h"
#include "game.h"
#include <gint/display.h> #include <gint/display.h>
void render_map(Player *player, Map *map_level) { void render_map(Game *game) {
Map *map_level = game->map_level;
Player *player = &game->player;
/* for all Layer (2 in the current configuration: Background is layer 0 and /* for all Layer (2 in the current configuration: Background is layer 0 and
* foreground is layer 1 ) */ * foreground is layer 1 ) */
/* x and y will contain the position in the loop. */ /* x and y will contain the position in the loop. */
@ -95,7 +100,11 @@ void render_map(Player *player, Map *map_level) {
} }
} }
void render_map_by_layer(Player *player, Map *map_level, int layer) { void render_map_by_layer(Game *game, int layer) {
Map *map_level = game->map_level;
Player *player = &game->player;
/* for all Layer (2 in the current configuration: Background is layer 0 and /* for all Layer (2 in the current configuration: Background is layer 0 and
* foreground is layer 1 ) */ * foreground is layer 1 ) */
/* x and y will contain the position in the loop. */ /* x and y will contain the position in the loop. */
@ -181,14 +190,18 @@ void render_map_by_layer(Player *player, Map *map_level, int layer) {
} }
} }
short int get_tile(Map *map_level, int x, int y, int l) { short int get_tile(Game *game, int x, int y, int l) {
Map *map_level = game->map_level;
/* Get the tile at (x, y) on layer l. Returns the tile ID or MAP_OUTSIDE if /* Get the tile at (x, y) on layer l. Returns the tile ID or MAP_OUTSIDE if
* she's not found. */ * she's not found. */
return x>=0 && x < map_level->w && y>=0 && y < map_level->h ? return x>=0 && x < map_level->w && y>=0 && y < map_level->h ?
map_level->layers[l][y * map_level->w + x] : MAP_OUTSIDE; map_level->layers[l][y * map_level->w + x] : MAP_OUTSIDE;
} }
short int get_walkable(Map *map_level, int x, int y) { short int get_walkable(Game *game, int x, int y) {
Map *map_level = game->map_level;
/* Get the tile at (x, y). Returns the tile ID or MAP_OUTSIDE if she's not /* Get the tile at (x, y). Returns the tile ID or MAP_OUTSIDE if she's not
* found. */ * found. */
return x>=0 && x < map_level->w && y>=0 && y < map_level->h ? return x>=0 && x < map_level->w && y>=0 && y < map_level->h ?

View file

@ -11,18 +11,26 @@
#include "game.h" #include "game.h"
#include "player.h" #include "player.h"
/* Structure 'Map' has been moved to game.h */
/* to avoid circular references between map.h, game.h and player.h */
/* only methods propotypes are now in dedicated header files */
/* Draws the map map on the entire screen to be viewed by the player player. */ /* Draws the map map on the entire screen to be viewed by the player player. */
void render_map(Player *player, Map *map_level); void render_map(Game *game);
/* Draws the map layer on the entire screen to be viewed by the player player. /* Draws the map layer on the entire screen to be viewed by the player player.
*/ */
void render_map_by_layer(Player *player, Map *map_level, int layer); void render_map_by_layer(Game *game, int layer);
/* Get the tile at (x, y) of the map map. If the tile is located outside of the /* Get the tile at (x, y) of the map map. If the tile is located outside of the
* screen, MAP_OUTSIDE is returned. */ * screen, MAP_OUTSIDE is returned. */
short int get_tile(Map *map_level, int x, int y, int l); short int get_tile(Game *game, int x, int y, int l);
/* Returns what is in the walkable layer at (x, y). */ /* Returns what is in the walkable layer at (x, y). */
short int get_walkable(Map *map_level, int x, int y); short int get_walkable(Game *game, int x, int y);
#endif #endif

View file

@ -27,24 +27,27 @@ const char damage_taken_walkable[WALKABLE_TILE_MAX] = {
extern bopti_image_t demo_player_img; extern bopti_image_t demo_player_img;
void player_draw(Player *player) { void player_draw(Game *game) {
Player *player = &game->player;
dimage(player->px-P_WIDTH/2, player->py-P_HEIGHT/2, &demo_player_img); dimage(player->px-P_WIDTH/2, player->py-P_HEIGHT/2, &demo_player_img);
} }
void player_move(Map *map_level, Player *player, Direction direction) { void player_move(Game *game, Direction direction) {
Player *player = &game->player;
/* How this player movement will modify the player x and y. */ /* How this player movement will modify the player x and y. */
char dx, dy; char dx, dy;
/* If the player will collide with a hard tile or if the will go outside of /* If the player will collide with a hard tile or if the will go outside of
* the map. */ * the map. */
if(player_collision(map_level, player, direction, P_CENTER)){ if(player_collision(game, direction, P_CENTER)){
/* If the will collide with the center of the player. */ /* If the will collide with the center of the player. */
dx = one_px_mov[direction*2]*player->speed; dx = one_px_mov[direction*2]*player->speed;
dy = one_px_mov[direction*2+1]*player->speed; dy = one_px_mov[direction*2+1]*player->speed;
player_fix_position(player, dx, dy); player_fix_position(game, dx, dy);
}else{ }
if(player_collision(map_level, player, direction, P_RIGHTDOWN) || else{
player_collision(map_level, player, direction, P_LEFTUP)){ if(player_collision(game, direction, P_RIGHTDOWN) ||
player_collision(game, direction, P_LEFTUP)){
/* If the will collide with the edges of the player. */ /* If the will collide with the edges of the player. */
/* I fix his position so he won't be partially in the tile. */ /* I fix his position so he won't be partially in the tile. */
/* I invert dx and dy to fix the axis where he is not moving on. */ /* I invert dx and dy to fix the axis where he is not moving on. */
@ -52,7 +55,7 @@ void player_move(Map *map_level, Player *player, Direction direction) {
*/ */
dx = one_px_mov[direction*2]*player->speed; dx = one_px_mov[direction*2]*player->speed;
dy = one_px_mov[direction*2+1]*player->speed; dy = one_px_mov[direction*2+1]*player->speed;
player_fix_position(player, dx==0, dy==0); player_fix_position(game, dx==0, dy==0);
} }
/* If he won't collide with the center, so I just move him normally */ /* If he won't collide with the center, so I just move him normally */
dx = one_px_mov[direction*2]*player->speed; dx = one_px_mov[direction*2]*player->speed;
@ -62,12 +65,15 @@ void player_move(Map *map_level, Player *player, Direction direction) {
} }
} }
void player_action(Player *player) { void player_action(Game *game) {
/**/ /* TODO */
} }
bool player_collision(Map *map_level, Player *player, Direction direction, bool player_collision(Game *game, Direction direction,
Checkpos nomov_axis_check) { Checkpos nomov_axis_check) {
Player *player = &game->player;
/* Where is the tile where he will go to from his position. */ /* Where is the tile where he will go to from his position. */
char dx = one_px_mov[direction*2]; char dx = one_px_mov[direction*2];
char dy = one_px_mov[direction*2+1]; char dy = one_px_mov[direction*2+1];
@ -86,7 +92,7 @@ bool player_collision(Map *map_level, Player *player, Direction direction,
else player_tile_x = player_tile_x/T_WIDTH; else player_tile_x = player_tile_x/T_WIDTH;
if(player_tile_y < 0) player_tile_y = player_tile_y/T_HEIGHT-1; if(player_tile_y < 0) player_tile_y = player_tile_y/T_HEIGHT-1;
else player_tile_y = player_tile_y/T_HEIGHT; else player_tile_y = player_tile_y/T_HEIGHT;
int on_walkable = get_walkable(map_level, player_tile_x, player_tile_y); int on_walkable = get_walkable(game, player_tile_x, player_tile_y);
int speed = on_walkable >= 0 && on_walkable < WALKABLE_TILE_MAX ? int speed = on_walkable >= 0 && on_walkable < WALKABLE_TILE_MAX ?
walkable_speed[on_walkable] : 0; walkable_speed[on_walkable] : 0;
/* if he's on a hard tile */ /* if he's on a hard tile */
@ -97,14 +103,19 @@ bool player_collision(Map *map_level, Player *player, Direction direction,
return false; /* He won't collide with a hard tile. */ return false; /* He won't collide with a hard tile. */
} }
void player_fix_position(Player *player, bool fix_x, bool fix_y) { void player_fix_position(Game *game, bool fix_x, bool fix_y) {
Player *player = &game->player;
/* I fix his poition on x or/and on y if y need to, so that he won't be over /* I fix his poition on x or/and on y if y need to, so that he won't be over
* the hard tile that he collided with. */ * the hard tile that he collided with. */
if(fix_x) player->x = player->x/T_WIDTH*T_WIDTH+P_WIDTH/2; if(fix_x) player->x = player->x/T_WIDTH*T_WIDTH+P_WIDTH/2;
if(fix_y) player->y = player->y/T_HEIGHT*T_HEIGHT+P_HEIGHT/2; if(fix_y) player->y = player->y/T_HEIGHT*T_HEIGHT+P_HEIGHT/2;
} }
void player_damage(Player *player, int amount) { void player_damage(Game *game, int amount) {
Player *player = &game->player;
player->life-=amount; player->life-=amount;
/* TODO: Let the player dye if life < 1. */ /* TODO: Let the player dye if life < 1. */
}; };

View file

@ -10,7 +10,7 @@
#define P_HEIGHT 8 #define P_HEIGHT 8
#endif #endif
/* SPEED should NOT be 8 or bigger: it this may cause bugs when handling /* SPEED should NOT be 8 or bigger: it may cause bugs when handling
* collisions! */ * collisions! */
#define SPEED PXSIZE*2 #define SPEED PXSIZE*2
@ -20,24 +20,33 @@
#include "memory.h" #include "memory.h"
/* Structure 'Player' has been moved to game.h */
/* to avoid circular references between map.h, game.h and player.h */
/* only methods propotypes are now in dedicated header files */
/* Draws the player player. This function should be called after drawing the /* Draws the player player. This function should be called after drawing the
* map! */ * map! */
void player_draw(Player *player); void player_draw(Game *game);
/* Move the player player in the direction direction. */ /* Move the player player in the direction direction. */
void player_move(Map *map_level, Player *player, Direction direction); void player_move(Game *game, Direction direction);
/* (Mibi88) TODO: Describe this function please, I've no idea what she's for! */ /* (Mibi88) TODO: Describe this function please, I've no idea what she's for! */
void player_action(Player *player); void player_action(Game *game);
/* Check if the player is in collision with the map or a NPC. Checkpos is used /* Check if the player is in collision with the map or a NPC. Checkpos is used
* to check the axis where the player is not moving. */ * to check the axis where the player is not moving. */
bool player_collision(Map *map_level, Player *player, Direction direction, bool player_collision(Game *game, Direction direction,
Checkpos nomov_axis_check); Checkpos nomov_axis_check);
/* Fix the position of the player so that he's not a bit inside of a hard block /* Fix the position of the player so that he's not a bit inside of a hard block
* after a collision. */ * after a collision. */
void player_fix_position(Player *player, bool fix_x, bool fix_y); void player_fix_position(Game *game, bool fix_x, bool fix_y);
/* Apply damage to player */
void player_damage(Game *game, int amount);
#endif #endif