| 
									
										
										
										
											2021-01-02 13:51:50 -08:00
										 |  |  | #include "game.h"
 | 
					
						
							|  |  |  | #include "content.h"
 | 
					
						
							| 
									
										
										
										
											2021-01-02 16:20:01 -08:00
										 |  |  | #include "masks.h"
 | 
					
						
							| 
									
										
										
										
											2021-01-02 17:50:25 -08:00
										 |  |  | #include "assets/tileset.h"
 | 
					
						
							| 
									
										
										
										
											2021-01-02 16:20:01 -08:00
										 |  |  | #include "components/collider.h"
 | 
					
						
							| 
									
										
										
										
											2021-01-02 17:50:25 -08:00
										 |  |  | #include "components/tilemap.h"
 | 
					
						
							| 
									
										
										
										
											2021-01-02 17:05:12 -08:00
										 |  |  | #include "factory.h"
 | 
					
						
							| 
									
										
										
										
											2021-01-02 13:51:50 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace TL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Game::startup() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// load our content
 | 
					
						
							|  |  |  | 	Content::load(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// framebuffer for the game
 | 
					
						
							| 
									
										
										
										
											2021-01-02 17:50:25 -08:00
										 |  |  | 	buffer = FrameBuffer::create(width, height); | 
					
						
							| 
									
										
										
										
											2021-01-02 13:51:50 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// set batcher to use Nearest Filter
 | 
					
						
							|  |  |  | 	batch.default_sampler = TextureSampler(TextureFilter::Nearest); | 
					
						
							| 
									
										
										
										
											2021-01-02 16:20:01 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-03 13:04:11 -08:00
										 |  |  | 	m_draw_colliders = false; | 
					
						
							| 
									
										
										
										
											2021-01-02 16:20:01 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-03 13:04:11 -08:00
										 |  |  | 	load_room(Point(0, 0)); | 
					
						
							| 
									
										
										
										
											2021-01-02 17:05:12 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-01-02 16:20:01 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-03 13:04:11 -08:00
										 |  |  | void Game::load_room(Point cell) | 
					
						
							| 
									
										
										
										
											2021-01-02 17:05:12 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-03 13:04:11 -08:00
										 |  |  | 	const Image* grid = Content::find_room(cell); | 
					
						
							|  |  |  | 	BLAH_ASSERT(grid, "Room doesn't exist!"); | 
					
						
							|  |  |  | 	room = cell; | 
					
						
							| 
									
										
										
										
											2021-01-02 16:20:01 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-03 13:04:11 -08:00
										 |  |  | 	// destroy all the entities
 | 
					
						
							|  |  |  | 	world.clear(); | 
					
						
							| 
									
										
										
										
											2021-01-02 17:05:12 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-02 17:50:25 -08:00
										 |  |  | 	// get the castle tileset for now
 | 
					
						
							|  |  |  | 	auto castle = Content::find_tileset("castle"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// make the floor
 | 
					
						
							|  |  |  | 	auto floor = world.add_entity(); | 
					
						
							| 
									
										
										
										
											2021-01-03 13:04:11 -08:00
										 |  |  | 	auto tilemap = floor->add(Tilemap(8, 8, columns, rows)); | 
					
						
							|  |  |  | 	auto solids = floor->add(Collider::make_grid(8, 40, 23)); | 
					
						
							|  |  |  | 	solids->mask = Mask::solid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// loop over the room grid
 | 
					
						
							|  |  |  | 	for (int x = 0; x < columns; x ++) | 
					
						
							|  |  |  | 		for (int y = 0; y < rows; y++) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Color col = grid->pixels[x + y * columns]; | 
					
						
							|  |  |  | 			uint32_t rgb = | 
					
						
							|  |  |  | 				((uint32_t)col.r << 16) | | 
					
						
							|  |  |  | 				((uint32_t)col.g << 8) | | 
					
						
							|  |  |  | 				((uint32_t)col.b); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			switch (rgb) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 			// black does nothing
 | 
					
						
							|  |  |  | 			case 0x000000: | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// solids
 | 
					
						
							|  |  |  | 			case 0xffffff: | 
					
						
							|  |  |  | 				tilemap->set_cell(x, y, &castle->random_tile()); | 
					
						
							|  |  |  | 				solids->set_cell(x, y, true); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// player
 | 
					
						
							|  |  |  | 			case 0x6abe30: | 
					
						
							|  |  |  | 				Factory::player(&world, Point(x * tile_width + tile_width / 2, (y + 1) * tile_height)); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-02 13:51:50 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Game::shutdown() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Game::update() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-02 16:20:01 -08:00
										 |  |  | 	if (Input::pressed(Key::F1)) | 
					
						
							|  |  |  | 		m_draw_colliders = !m_draw_colliders; | 
					
						
							| 
									
										
										
										
											2021-01-02 17:05:12 -08:00
										 |  |  | 	if (Input::pressed(Key::F2)) | 
					
						
							| 
									
										
										
										
											2021-01-03 13:04:11 -08:00
										 |  |  | 		load_room(room); | 
					
						
							| 
									
										
										
										
											2021-01-02 13:51:50 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-02 16:20:01 -08:00
										 |  |  | 	world.update(); | 
					
						
							| 
									
										
										
										
											2021-01-02 13:51:50 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Game::render() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// draw gameplay stuff
 | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2021-01-03 13:04:11 -08:00
										 |  |  | 		buffer->clear(0x150e22); | 
					
						
							| 
									
										
										
										
											2021-01-02 16:20:01 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		world.render(batch); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (m_draw_colliders) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			auto collider = world.first<Collider>(); | 
					
						
							|  |  |  | 			while (collider) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				collider->render(batch); | 
					
						
							|  |  |  | 				collider = (Collider*)collider->next(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-02 13:51:50 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		batch.render(buffer); | 
					
						
							|  |  |  | 		batch.clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// draw buffer to the screen
 | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		float scale = Calc::min( | 
					
						
							|  |  |  | 			App::backbuffer->width() / (float)buffer->width(), | 
					
						
							|  |  |  | 			App::backbuffer->height() / (float)buffer->height()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Vec2 screen_center = Vec2(App::backbuffer->width(), App::backbuffer->height()) / 2; | 
					
						
							|  |  |  | 		Vec2 buffer_center = Vec2(buffer->width(), buffer->height()) / 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		App::backbuffer->clear(Color::black); | 
					
						
							|  |  |  | 		batch.push_matrix(Mat3x2::create_transform(screen_center, buffer_center, Vec2::one * scale, 0)); | 
					
						
							|  |  |  | 		batch.tex(buffer->attachment(0), Vec2::zero, Color::white); | 
					
						
							|  |  |  | 		batch.pop_matrix(); | 
					
						
							|  |  |  | 		batch.render(App::backbuffer); | 
					
						
							|  |  |  | 		batch.clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |