| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | package systems | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-04-03 19:39:08 +02:00
										 |  |  | 	"log/slog" | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 	"openquell/components" | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 	. "openquell/components" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-13 18:15:52 +01:00
										 |  |  | 	. "openquell/config" | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 	"openquell/observers" | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/hajimehoshi/ebiten/v2" | 
					
						
							|  |  |  | 	"github.com/mlange-42/arche/ecs" | 
					
						
							|  |  |  | 	"github.com/mlange-42/arche/generic" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type CollectibleSystem struct { | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 	World    *ecs.World | 
					
						
							| 
									
										
										
										
											2024-04-03 19:39:08 +02:00
										 |  |  | 	Cellsize int | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 	Selector *generic.Filter3[Position, Collectible, Renderable] | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 19:39:08 +02:00
										 |  |  | func NewCollectibleSystem(world *ecs.World, cellsize int) System { | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 	system := &CollectibleSystem{ | 
					
						
							|  |  |  | 		Selector: generic.NewFilter3[Position, Collectible, Renderable](), | 
					
						
							|  |  |  | 		World:    world, | 
					
						
							| 
									
										
										
										
											2024-04-03 19:39:08 +02:00
										 |  |  | 		Cellsize: cellsize, | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return system | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-23 18:47:15 +01:00
										 |  |  | func (system *CollectibleSystem) Update() error { | 
					
						
							| 
									
										
										
										
											2024-02-27 14:45:23 +01:00
										 |  |  | 	observer := observers.GetGameObserver(system.World) | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 	posID := ecs.ComponentID[components.Position](system.World) | 
					
						
							|  |  |  | 	veloID := ecs.ComponentID[components.Velocity](system.World) | 
					
						
							| 
									
										
										
										
											2024-02-12 17:27:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 	query := system.Selector.Query(system.World) | 
					
						
							| 
									
										
										
										
											2024-02-11 14:24:30 +01:00
										 |  |  | 	numcollectibles := query.Count() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-27 14:45:23 +01:00
										 |  |  | 	if numcollectibles == 0 || observer.Lost { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:25 +02:00
										 |  |  | 		slog.Debug("WON") | 
					
						
							|  |  |  | 		timer := observers.GetGameObserver(system.World).StopTimer | 
					
						
							|  |  |  | 		if !timer.Running { | 
					
						
							|  |  |  | 			timer.Start(LEVEL_END_WAIT) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-02-11 14:24:30 +01:00
										 |  |  | 		query.Close() | 
					
						
							| 
									
										
										
										
											2024-02-23 18:47:15 +01:00
										 |  |  | 		return nil | 
					
						
							| 
									
										
										
										
											2024-02-11 14:24:30 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for query.Next() { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:25 +02:00
										 |  |  | 		colposition, collectible, render := query.Get() | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-27 14:45:23 +01:00
										 |  |  | 		for _, player := range observer.GetPlayers() { | 
					
						
							| 
									
										
										
										
											2024-02-26 19:07:35 +01:00
										 |  |  | 			if !system.World.Alive(player) { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 			playerposition := (*Position)(system.World.Get(player, posID)) | 
					
						
							|  |  |  | 			playervelocity := (*Velocity)(system.World.Get(player, veloID)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-26 13:55:54 +01:00
										 |  |  | 			ok, _ := colposition.Intersects(playerposition, playervelocity) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:25 +02:00
										 |  |  | 			if ok && !collectible.Hit { | 
					
						
							| 
									
										
										
										
											2024-04-03 19:39:08 +02:00
										 |  |  | 				slog.Debug("bumped into collectible", "colpos", colposition) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:25 +02:00
										 |  |  | 				render.StartAnimation(components.Destruction) | 
					
						
							| 
									
										
										
										
											2024-04-03 19:39:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// position the animation relative to the middle of the current entity | 
					
						
							|  |  |  | 				colposition.Update( | 
					
						
							|  |  |  | 					colposition.X-(system.Cellsize/2), | 
					
						
							|  |  |  | 					colposition.Y-(system.Cellsize/2), | 
					
						
							|  |  |  | 					64, | 
					
						
							|  |  |  | 				) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				collectible.Hit = true | 
					
						
							| 
									
										
										
										
											2024-04-03 19:39:08 +02:00
										 |  |  | 				numcollectibles-- | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-23 18:47:15 +01:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (system *CollectibleSystem) Draw(screen *ebiten.Image) { | 
					
						
							|  |  |  | 	// write the movable tiles | 
					
						
							|  |  |  | 	op := &ebiten.DrawImageOptions{} | 
					
						
							|  |  |  | 	query := system.Selector.Query(system.World) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for query.Next() { | 
					
						
							|  |  |  | 		pos, _, sprite := query.Get() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 19:39:08 +02:00
										 |  |  | 		if !sprite.Hidden { | 
					
						
							|  |  |  | 			op.GeoM.Reset() | 
					
						
							|  |  |  | 			op.GeoM.Translate(float64(pos.X), float64(pos.Y)) | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 19:39:08 +02:00
										 |  |  | 			screen.DrawImage(sprite.Image, op) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |