Auto-destroy bodies

This commit is contained in:
oakes
2014-01-18 00:23:08 -05:00
parent e0371d4d28
commit da9b61c47e
2 changed files with 54 additions and 9 deletions

View File

@@ -12,14 +12,24 @@
(remove-input! renderer)
(add-input! renderer))
(defn ^:private update-box-2d!
[{:keys [^World world]} entities]
(when-not (.isLocked world)
(let [bodies (u/gdx-array [])]
(.getBodies world bodies)
(doseq [body bodies]
(when-not (some #(= body (:body %)) entities)
(.destroyBody world body))))))
(defn ^:private update-screen!
([{:keys [world g2dp-listener]}]
(when (isa? (type world) World)
(.setContactListener ^World world g2dp-listener)))
([{:keys [renderer] :as screen} entities]
(cond
(isa? (type renderer) Stage)
(update-stage! screen entities))))
([{:keys [renderer world] :as screen} entities]
(when (isa? (type renderer) Stage)
(update-stage! screen entities))
(when (isa? (type world) World)
(update-box-2d! screen entities))))
; tiled maps

View File

@@ -2,7 +2,8 @@
(:require [play-clj.math :as m]
[play-clj.utils :as u])
(:import [com.badlogic.gdx.physics.box2d Body BodyDef ChainShape CircleShape
ContactListener EdgeShape FixtureDef PolygonShape Transform World]))
Contact ContactListener EdgeShape Fixture FixtureDef PolygonShape
Transform World]))
; world
@@ -68,19 +69,23 @@
(defn body-transform!
[entity x y angle]
(body! entity :set-transform x y angle))
(body! entity :set-transform x y angle)
entity)
(defn body-x!
[entity x]
(body-transform! entity x (body-y entity) (body-angle entity)))
(body-transform! entity x (body-y entity) (body-angle entity))
entity)
(defn body-y!
[entity y]
(body-transform! entity (body-x entity) y (body-angle entity)))
(body-transform! entity (body-x entity) y (body-angle entity))
entity)
(defn body-angle!
[entity angle]
(body-transform! entity (body-x entity) (body-y entity) angle))
(body-transform! entity (body-x entity) (body-y entity) angle)
entity)
; fixtures
@@ -149,6 +154,36 @@
[object k & options]
`(u/call! ^PolygonShape ~object ~k ~@options))
; misc functions
(defmacro contact!
[object k & options]
`(u/call! ^Contact ~object ~k ~@options))
(defmacro fixture!
[object k & options]
`(u/call! ^Fixture ~object ~k ~@options))
(defn find-body
[body entities]
(some #(if (= body (:body %)) %) entities))
(defn first-contact
([screen]
(let [^Contact contact (or (:contact screen) screen)]
(assert contact)
(-> contact .getFixtureA .getBody)))
([screen entities]
(find-body (first-contact screen) entities)))
(defn second-contact
([screen]
(let [^Contact contact (or (:contact screen) screen)]
(assert contact)
(-> contact .getFixtureB .getBody)))
([screen entities]
(find-body (second-contact screen) entities)))
; listeners
(defn contact-listener