Kenney makes absolutely amazing art, but one of my wishes for a while now was that he would make a roguelike sprite sheet. Well, he’s released a new donation pack, and it comes with an amazing 2125 sprites for a top down 2D roguelike!
Unfortunately, this sprite sheet doesn’t come with a data file (XML or JSON), which makes it difficult to use in some game engines. I took some code that I previously wrote for my Texture Atlas Converter and hacked it up to generate a JSON data file for this amazing sprite sheet.
The code is a little rough, but it gets the job done.
PHP Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
<?php $ROWS = 43; $COLS = 57; $TILE_SIZE = 15; $MARGIN_X = 2; $MARGIN_Y = 2; $total_sprites = $ROWS * $COLS; $json = createJSONHash(); $filename = "Rogue.json"; logMsg("{$total_sprites} textures processed successfully"); file_put_contents($filename, json_encode($json, JSON_PRETTY_PRINT)); logMsg("Saved to {$filename}"); die(); function createJSONHash(){ global $ROWS, $COLS, $TILE_SIZE, $MARGIN_X, $MARGIN_Y; $total_sprites = $ROWS * $COLS; $x = 0; $y = 0; $json = new stdClass(); //build out the frames obj (contains all the images) $json->frames = new stdClass(); //build out the meta object (just info about this program) $json->meta = new stdClass(); //loop through all subtextures $i = 0; while ($i < $total_sprites){ $tile_name = "t_" . $i; //make the right object, JSON arr would differ here $json->frames->{$tile_name} = new stdClass(); //get coords //var x = ((i % MAP_WIDTH) * TILE_SIZE) + LEFT_OFFSET; $x = ($i % $COLS) * ($TILE_SIZE + $MARGIN_X); //var y = (Math.floor(i / MAP_WIDTH) * TILE_SIZE) + TOP_OFFSET; $y = floor($i / $COLS) * ($TILE_SIZE + $MARGIN_Y); //make the frame object $frame_obj = new stdClass(); $frame_obj->x = $x; $frame_obj->y = $y; $frame_obj->w = $TILE_SIZE; $frame_obj->h = $TILE_SIZE; //set the frame object $json->frames->{$tile_name}->frame = $frame_obj; //make the sprite source object $sprite_obj = new stdClass(); $sprite_obj->x = 0; $sprite_obj->y = 0; $sprite_obj->w = $TILE_SIZE; $sprite_obj->h = $TILE_SIZE; //set the sprite source object $json->frames->{$tile_name}->spriteSourceSize = $sprite_obj; //make the source size object $source_obj = new stdClass(); $source_obj->w = $TILE_SIZE; $source_obj->h = $TILE_SIZE; //set the sprite source object $json->frames->{$tile_name}->sourceSize = $source_obj; //add some more stuff that may or may not matter $json->frames->{$tile_name}->rotated = false; $json->frames->{$tile_name}->trimmed = false; //make the pivot object $pivot_obj = new stdClass(); $pivot_obj->x = 0.5; $pivot_obj->y = 0.5; $json->frames->{$tile_name}->rotated = false; $i++; } $json->meta->sprites = $i; $json->meta->image = "roguelikeSheet_transparent.png"; logMsg("The sprite sheet should be these dimensions - w:" . ($x + $TILE_SIZE) . " h:" . ($y + $TILE_SIZE)); return $json; } function logMsg ($message){ echo "{$message}\n"; } ?> |