Skip to content

Instantly share code, notes, and snippets.

@carloscm
Created October 5, 2015 14:20
Show Gist options
  • Save carloscm/75365235976652de19d2 to your computer and use it in GitHub Desktop.
Save carloscm/75365235976652de19d2 to your computer and use it in GitHub Desktop.
(define recycler-sm (state-machine
(start find-litter)
(args recycler phase)
(vars
pick-ticks 0
work-ticks 0)
(state find-litter
(vars
center (ecs-preferred-center recycler)
found-litter (and center (ecs-query (recycler #@world)
must: (list litter-component)
exclude: (list ChildComponent_Kind BusyComponent_Kind)
single: #t
by-distance: center
path-from: center))
found-worker (and found-litter (ecs-query (recycler #@world)
must: (list WorkerComponent_Kind recycler-worker-component)
exclude: (list BusyComponent_Kind)
single: #t
by-distance: center
path-from: center)))
(to walk-to-litter (and found-litter found-worker)
([ center (ecs-pos-center found-litter) ]
(ecs-make-add-component recycler BusyComponent_Kind
'litter-ref (ecs-ref found-litter)
'worker-ref (ecs-ref found-worker))
(ecs-make-add-component found-litter BusyComponent_Kind
#@removeCompOnInvalid #t
#@subject.id (recycler #@ref.id)
#@subject.slot (recycler #@ref.slot))
(ecs-make-add-component found-worker BusyComponent_Kind
#@removeCompOnInvalid #t
#@subject.id (recycler #@ref.id)
#@subject.slot (recycler #@ref.slot))
(ecs-make-add-component found-worker PathComponent_Kind
#@destination.x (center 0)
#@destination.y (center 1)))))
(state cleanup
(vars
busyc (ecs-get-component recycler BusyComponent_Kind)
litter (and busyc (ecs-deref (recycler #@world) (busyc 'litter-ref)))
own-litter (and litter (ecs-busy-with? litter recycler))
worker (and busyc (ecs-deref (recycler #@world) (busyc 'worker-ref)))
own-worker (and worker (ecs-busy-with? worker recycler)))
(to find-litter #t
(set! pick-ticks 0)
(set! work-ticks 0)
(ecs-remove-component recycler BusyComponent_Kind)
(when own-litter (ecs-remove-component litter BusyComponent_Kind))
(when own-worker (ecs-remove-component worker BusyComponent_Kind))))
(state job-integrity
(vars
busyc (ecs-get-component recycler BusyComponent_Kind)
litter (and busyc (ecs-deref (recycler #@world) (busyc 'litter-ref)))
own-litter (and litter (ecs-busy-with? litter recycler))
worker (and busyc (ecs-deref (recycler #@world) (busyc 'worker-ref)))
own-worker (and worker (ecs-busy-with? worker recycler)))
;walking-worker (and worker (ecs-has-component? worker PathComponent_Kind)))
(to cleanup (or (not own-litter) (not own-worker))
(format #t "job-integrity INTEGRITY FAILURE worker ~A own-worker ~A litter ~A own-litter ~A\n" worker own-worker litter own-litter)))
(state walk-to-litter
(extend job-integrity)
(vars
over-litter (ecs-intersect-boundary? worker litter)
walking (ecs-has-component? worker PathComponent_Kind))
(to pick-litter over-litter
(ecs-remove-component worker PathComponent_Kind)
(ecs-phys-stop worker)
(ecs-pose worker "action"))
(to cleanup (and (not over-litter) (not walking))))
(state pick-litter
(extend job-integrity)
(vars
done (>= pick-ticks 20))
(to pick-litter (not done)
(set! pick-ticks (+ pick-ticks phase))
(break))
(to walk-to-recycler done
([ center (ecs-preferred-center recycler) ]
(ecs-pose worker "action" #f)
(ecs-make-add-component litter ChildComponent_Kind
#@parent.id (worker #@ref.id)
#@parent.slot (worker #@ref.slot))
(ecs-make-add-component worker PathComponent_Kind
#@destination.x (center 0)
#@destination.y (center 1)))))
(state walk-to-recycler
(extend job-integrity)
(vars
walking (ecs-has-component? worker PathComponent_Kind)
center (ecs-preferred-center recycler)
obj-center (ecs-pos-center recycler)
over-center (ecs-intersect-point? worker center))
(to work-recycler over-center
(ecs-remove-component worker PathComponent_Kind)
(ecs-phys-stop worker)
(ecs-phys-heading-current worker obj-center 0.0)
(ecs-pose worker "action"))
(to cleanup (and (not over-center) (not walking))))
(state work-recycler
(extend job-integrity)
(vars
done (>= work-ticks 50))
(to work-recycler (not done)
(set! work-ticks (+ work-ticks phase))
(break))
(to find-litter done
([ center (ecs-preferred-center recycler) ]
(ecs-pose worker "action" #f)
(make-scrolling-text (recycler #@world) (center 0) (center 1) by: 40.0
text: (format #f "Recycled"))
(ecs-remove-component recycler BusyComponent_Kind)
(set! pick-ticks 0)
(set! work-ticks 0)
(when own-litter (ecs-destroy-entity litter))
(when own-worker (ecs-remove-component worker BusyComponent_Kind)))))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment