public FluidSystem(SystemManager systemManager, EntityManager entityManager) { _systemManager = systemManager; _entityManager = entityManager; _physicsSystem = (PhysicsSystem)_systemManager.getSystem(SystemType.Physics); _renderSystem = (RenderSystem)_systemManager.getSystem(SystemType.Render); fluidGrid = new Dictionary<int, Dictionary<int, List<int>>>(); liquid = new Particle[MAX_PARTICLES]; activeParticles = new int[MAX_PARTICLES]; _simPositions = new Vector2[MAX_PARTICLES]; _simVelocities = new Vector2[MAX_PARTICLES]; _delta = new Vector2[MAX_PARTICLES]; for (int i = 0; i < MAX_PARTICLES; i++) { liquid[i] = new Particle(this, i, LIQUID_ORIGIN); } }
// killParticle private void killParticle(Particle particle) { particle.position = LIQUID_ORIGIN; particle.alive = false; particle.active = false; numActiveParticles--; }
// handleParticleInfluence -- Handles interaction between entities and particles public void handleParticleInfluence(string levelUid, Particle particle) { for (int i = 0; i < particle.entityInfluenceCount; i++) { int entityId = particle.entitiesToInfluence[i]; ParticleInfluenceComponent particleInfluenceComponent = _entityManager.getComponent(levelUid, entityId, ComponentType.ParticleInfluence) as ParticleInfluenceComponent; particleInfluenceComponent.particleCount++; if (particleInfluenceComponent.type == ParticleInfluenceType.Physical) { // Physical body influences -- body-to-particle influences are usually already handled by resolveCollisions() PhysicsComponent physicsComponent = _entityManager.getComponent(levelUid, entityId, ComponentType.Physics) as PhysicsComponent; physicsComponent.body.ApplyLinearImpulse(particle.oldPosition - particle.position, particle.oldPosition); } else if (particleInfluenceComponent.type == ParticleInfluenceType.Dynamite) { // Dynamite influence PhysicsComponent physicsComponent = _entityManager.getComponent(levelUid, entityId, ComponentType.Physics) as PhysicsComponent; Vector2 bodyVelocity = physicsComponent.body.LinearVelocity; physicsComponent.body.LinearVelocity = bodyVelocity * 0.98f + particle.velocity; physicsComponent.body.AngularVelocity = physicsComponent.body.AngularVelocity * 0.98f; particle.velocity += bodyVelocity * 0.003f; } else if (particleInfluenceComponent.type == ParticleInfluenceType.Character) { // Character influence PhysicsComponent physicsComponent = _entityManager.getComponent(levelUid, entityId, ComponentType.Physics) as PhysicsComponent; Vector2 bodyVelocity = physicsComponent.body.LinearVelocity; physicsComponent.body.LinearVelocity = bodyVelocity * 0.95f + particle.velocity; particle.velocity += bodyVelocity * 0.003f; } else if (particleInfluenceComponent.type == ParticleInfluenceType.Explosion) { ExplosionComponent explosionComponent = _entityManager.getComponent(levelUid, entityId, ComponentType.Explosion) as ExplosionComponent; Vector2 relative; float distanceSq; Vector2 force; relative = particle.position - explosionComponent.position; distanceSq = relative.LengthSquared(); relative.Normalize(); force = relative * (explosionComponent.strength / Math.Max(distanceSq, 1f)); particle.velocity += force * 0.0055f; } } }