| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | package grid | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"image" | 
					
						
							|  |  |  | 	"log" | 
					
						
							| 
									
										
										
										
											2024-02-13 18:42:13 +01:00
										 |  |  | 	"log/slog" | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 	"openquell/assets" | 
					
						
							|  |  |  | 	"openquell/components" | 
					
						
							| 
									
										
										
										
											2024-02-11 14:24:30 +01:00
										 |  |  | 	"openquell/config" | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 	"openquell/observers" | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 	"github.com/mlange-42/arche/ecs" | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 	"github.com/mlange-42/arche/generic" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Grid struct { | 
					
						
							|  |  |  | 	Width    int | 
					
						
							|  |  |  | 	Height   int | 
					
						
							|  |  |  | 	Size     int | 
					
						
							|  |  |  | 	Tilesize int | 
					
						
							|  |  |  | 	Map      map[image.Point]*assets.Tile | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // FIXME: put component addition into extra callable function, to be called | 
					
						
							|  |  |  | // by game.go in a level-loop. Also remove components of previous level | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | func NewGrid(world *ecs.World, | 
					
						
							|  |  |  | 	tilesize, width, height int, mapslice map[image.Point]*assets.Tile) *Grid { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 	// we use this to turn  our tiles into iterable entities, used for | 
					
						
							|  |  |  | 	// collision detection, transformation and other things | 
					
						
							| 
									
										
										
										
											2024-02-11 14:24:30 +01:00
										 |  |  | 	playermapper := generic.NewMap5[ | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 		components.Position, | 
					
						
							|  |  |  | 		components.Velocity, | 
					
						
							|  |  |  | 		components.Renderable, | 
					
						
							| 
									
										
										
										
											2024-02-11 14:24:30 +01:00
										 |  |  | 		components.Speed, | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 		components.Player](world) | 
					
						
							| 
									
										
										
										
											2024-02-07 18:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 	solidmapper := generic.NewMap4[ | 
					
						
							|  |  |  | 		components.Position, | 
					
						
							|  |  |  | 		components.Renderable, | 
					
						
							|  |  |  | 		components.Tilish, | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 		components.Solid](world) | 
					
						
							| 
									
										
										
										
											2024-02-07 18:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 	emptymapper := generic.NewMap2[components.Position, components.Tilish](world) | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-07 18:01:58 +01:00
										 |  |  | 	colmapper := generic.NewMap3[ | 
					
						
							|  |  |  | 		components.Position, | 
					
						
							|  |  |  | 		components.Renderable, | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 		components.Collectible](world) | 
					
						
							| 
									
										
										
										
											2024-02-07 18:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-19 19:05:48 +01:00
										 |  |  | 	obsmapper := generic.NewMap3[ | 
					
						
							|  |  |  | 		components.Position, | 
					
						
							|  |  |  | 		components.Renderable, | 
					
						
							|  |  |  | 		components.Obstacle](world) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 	var pos *components.Position | 
					
						
							|  |  |  | 	var render *components.Renderable | 
					
						
							| 
									
										
										
										
											2024-02-11 14:24:30 +01:00
										 |  |  | 	var speed *components.Speed | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 	playerobserver := observers.GetPlayerObserver(world) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 	for point, tile := range mapslice { | 
					
						
							|  |  |  | 		switch tile.Renderable { | 
					
						
							|  |  |  | 		case true: | 
					
						
							|  |  |  | 			switch { | 
					
						
							|  |  |  | 			case tile.Solid: | 
					
						
							|  |  |  | 				entity := solidmapper.New() | 
					
						
							|  |  |  | 				pos, render, _, _ = solidmapper.Get(entity) | 
					
						
							|  |  |  | 			case tile.Player: | 
					
						
							|  |  |  | 				entity := playermapper.New() | 
					
						
							| 
									
										
										
										
											2024-02-11 14:24:30 +01:00
										 |  |  | 				pos, _, render, speed, _ = playermapper.Get(entity) | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 				playerobserver.AddEntity(entity) | 
					
						
							| 
									
										
										
										
											2024-02-11 14:24:30 +01:00
										 |  |  | 				speed.Value = config.PLAYERSPEED | 
					
						
							| 
									
										
										
										
											2024-02-19 19:05:48 +01:00
										 |  |  | 				slog.Debug("player start pos", "X", point.X*tilesize, | 
					
						
							|  |  |  | 					"Y", point.Y*tilesize, "Z", 191) | 
					
						
							| 
									
										
										
										
											2024-02-07 18:01:58 +01:00
										 |  |  | 			case tile.Collectible: | 
					
						
							|  |  |  | 				entity := colmapper.New() | 
					
						
							|  |  |  | 				pos, render, _ = colmapper.Get(entity) | 
					
						
							| 
									
										
										
										
											2024-02-19 19:05:48 +01:00
										 |  |  | 			case tile.Obstacle: | 
					
						
							|  |  |  | 				entity := obsmapper.New() | 
					
						
							|  |  |  | 				pos, render, _ = obsmapper.Get(entity) | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 			default: | 
					
						
							|  |  |  | 				log.Fatalln("unsupported tile type encountered") | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			render.Image = tile.Sprite | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			// empty  cell, this is  where the player[s]  move. No | 
					
						
							|  |  |  | 			// sprite required since  every level has a background | 
					
						
							|  |  |  | 			// image. | 
					
						
							|  |  |  | 			entity := emptymapper.New() | 
					
						
							|  |  |  | 			pos, _ = emptymapper.Get(entity) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// however, every tile has a position | 
					
						
							|  |  |  | 		pos.Update(point.X*tilesize, point.Y*tilesize, tilesize) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &Grid{ | 
					
						
							|  |  |  | 		Size:     len(mapslice), | 
					
						
							|  |  |  | 		Tilesize: tilesize, | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 		Width:    width, | 
					
						
							|  |  |  | 		Height:   height, | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 		Map:      mapslice, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | // return the tile the moving object would end up on if indeed moving | 
					
						
							|  |  |  | func (grid *Grid) GetTile( | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 	position *components.Position, | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 	velocity *components.Velocity) *assets.Tile { | 
					
						
							| 
									
										
										
										
											2024-02-06 19:02:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 	newpoint := image.Point{ | 
					
						
							|  |  |  | 		int(position.X+velocity.Data.X) / grid.Tilesize, | 
					
						
							|  |  |  | 		int(position.Y+velocity.Data.Y) / grid.Tilesize, | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 	tile := grid.Map[newpoint] | 
					
						
							|  |  |  | 	return tile | 
					
						
							| 
									
										
										
										
											2024-02-06 15:26:20 +01:00
										 |  |  | } |