| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | package systems | 
					
						
							| 
									
										
										
										
											2024-02-09 20:20:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	. "openquell/components" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 	"github.com/hajimehoshi/ebiten/v2" | 
					
						
							| 
									
										
										
										
											2024-02-09 20:20:13 +01:00
										 |  |  | 	"github.com/mlange-42/arche/ecs" | 
					
						
							|  |  |  | 	"github.com/mlange-42/arche/generic" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ParticleSystem struct { | 
					
						
							|  |  |  | 	World    *ecs.World | 
					
						
							|  |  |  | 	Selector *generic.Filter2[Position, Particle] | 
					
						
							|  |  |  | 	Cellsize int | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | func NewParticleSystem(world *ecs.World, cellsize int) *ParticleSystem { | 
					
						
							| 
									
										
										
										
											2024-02-09 20:20:13 +01:00
										 |  |  | 	system := &ParticleSystem{ | 
					
						
							|  |  |  | 		Selector: generic.NewFilter2[Position, Particle](), | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 		World:    world, | 
					
						
							|  |  |  | 		Cellsize: cellsize, | 
					
						
							| 
									
										
										
										
											2024-02-09 20:20:13 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return system | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | func (system *ParticleSystem) Update() { | 
					
						
							| 
									
										
										
										
											2024-02-09 20:20:13 +01:00
										 |  |  | 	// display debris after collecting | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 	EntitiesToRemove := []ecs.Entity{} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-09 20:20:13 +01:00
										 |  |  | 	query := system.Selector.Query(system.World) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for query.Next() { | 
					
						
							|  |  |  | 		// we loop, but it's only one anyway | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 		_, particle := query.Get() | 
					
						
							| 
									
										
										
										
											2024-02-09 20:20:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 		switch { | 
					
						
							|  |  |  | 		// particle shows from earlier tick, animate | 
					
						
							|  |  |  | 		case particle.Index > -1 && particle.Index < len(particle.Particles)-1: | 
					
						
							|  |  |  | 			particle.Index++ | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			// last sprite reached, remove it | 
					
						
							|  |  |  | 			EntitiesToRemove = append(EntitiesToRemove, query.Entity()) | 
					
						
							| 
									
										
										
										
											2024-02-09 20:20:13 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for _, entity := range EntitiesToRemove { | 
					
						
							|  |  |  | 		system.World.RemoveEntity(entity) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-02-09 20:20:13 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (system *ParticleSystem) Draw(screen *ebiten.Image) { | 
					
						
							|  |  |  | 	// write particles (these are no tiles!) | 
					
						
							|  |  |  | 	op := &ebiten.DrawImageOptions{} | 
					
						
							|  |  |  | 	query := system.Selector.Query(system.World) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for query.Next() { | 
					
						
							|  |  |  | 		pos, particle := query.Get() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-11 13:00:56 +01:00
										 |  |  | 		op.GeoM.Reset() | 
					
						
							|  |  |  | 		op.GeoM.Translate(float64(pos.X), float64(pos.Y)) | 
					
						
							|  |  |  | 		screen.DrawImage(particle.Particles[particle.Index], op) | 
					
						
							| 
									
										
										
										
											2024-02-10 19:45:06 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |