Skip to content

Instantly share code, notes, and snippets.

@wizard1066
Created April 27, 2023 17:53
Show Gist options
  • Save wizard1066/6eba6cb3b12d331fdd127c18465ab0cd to your computer and use it in GitHub Desktop.
Save wizard1066/6eba6cb3b12d331fdd127c18465ab0cd to your computer and use it in GitHub Desktop.
//
// ContentViewA.swift
// Emitter
//
// Created by localuser on 18.04.23.
//
import Foundation
import SwiftUI
//
// ContentView.swift
// Emitter
//
// Created by localuser on 18.04.23.
import SwiftUI
struct ContentView: View {
@State var numParticlesToEmit = 100
var body: some View {
ZStack {
ForEach((0..<numParticlesToEmit), id: \.self) { num in
ParticleBuild(colorGroup: ColoursAvailable.colorA)
}
}
}
}
struct ParticleBuild: View {
@State var colorGroup:ColoursAvailable
@ViewBuilder
var body: some View {
let newAttr = Attributes(colorIndex: colorGroup)
Particle(attributes: newAttr)
}
}
struct Particle: View {
@State var attributes:Attributes!
@State var opacity = 1.0
@State var xOffset = 0.0
@State var yOffset = 0.0
var body: some View {
Circle()
.fill(returnColors()[attributes.colour])
.frame(width: 16, height: 16)
.scaleEffect(attributes.scaleRange)
.opacity(opacity)
.offset(x: xOffset, y:yOffset)
.onAppear(perform: {
withAnimation(.easeIn(duration: attributes.lifeTimeRange)) {
opacity = 0
xOffset = attributes.xAcceleration
yOffset = attributes.yAcceleration
}
})
.blendMode(.screen)
}
func returnColors() -> [Color] {
switch attributes.colorIndex {
case ColoursAvailable.colorA:
return Attributes.coloursA
case ColoursAvailable.colorB:
return Attributes.coloursB
case ColoursAvailable.colorC:
return Attributes.coloursC
}
}
}
enum ColoursAvailable:Int {
case colorA = 0
case colorB = 1
case colorC = 2
}
struct Attributes {
@State var colorIndex:ColoursAvailable
static var coloursA = [Color.red, Color.orange, Color.yellow]
static var coloursB = [Color.blue, Color.purple, Color.indigo]
static var coloursC = [Color.teal, Color.green, Color.mint]
@State var birthRate = 10
@State var lifeTimeRange = Double.random(in: 5...10)
@State var xAcceleration = Double.random(in: -100...100)
@State var yAcceleration = Double.random(in: -100...100)
@State var colour = Int.random(in: 0..<coloursA.count)
@State var scaleRange = Double.random(in: 0.1...0.9)
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment