Auto-destroy bodies
This commit is contained in:
@@ -12,14 +12,24 @@
|
|||||||
(remove-input! renderer)
|
(remove-input! renderer)
|
||||||
(add-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!
|
(defn ^:private update-screen!
|
||||||
([{:keys [world g2dp-listener]}]
|
([{:keys [world g2dp-listener]}]
|
||||||
(when (isa? (type world) World)
|
(when (isa? (type world) World)
|
||||||
(.setContactListener ^World world g2dp-listener)))
|
(.setContactListener ^World world g2dp-listener)))
|
||||||
([{:keys [renderer] :as screen} entities]
|
([{:keys [renderer world] :as screen} entities]
|
||||||
(cond
|
(when (isa? (type renderer) Stage)
|
||||||
(isa? (type renderer) Stage)
|
(update-stage! screen entities))
|
||||||
(update-stage! screen entities))))
|
(when (isa? (type world) World)
|
||||||
|
(update-box-2d! screen entities))))
|
||||||
|
|
||||||
; tiled maps
|
; tiled maps
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
(:require [play-clj.math :as m]
|
(:require [play-clj.math :as m]
|
||||||
[play-clj.utils :as u])
|
[play-clj.utils :as u])
|
||||||
(:import [com.badlogic.gdx.physics.box2d Body BodyDef ChainShape CircleShape
|
(: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
|
; world
|
||||||
|
|
||||||
@@ -68,19 +69,23 @@
|
|||||||
|
|
||||||
(defn body-transform!
|
(defn body-transform!
|
||||||
[entity x y angle]
|
[entity x y angle]
|
||||||
(body! entity :set-transform x y angle))
|
(body! entity :set-transform x y angle)
|
||||||
|
entity)
|
||||||
|
|
||||||
(defn body-x!
|
(defn body-x!
|
||||||
[entity 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!
|
(defn body-y!
|
||||||
[entity 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!
|
(defn body-angle!
|
||||||
[entity 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
|
; fixtures
|
||||||
|
|
||||||
@@ -149,6 +154,36 @@
|
|||||||
[object k & options]
|
[object k & options]
|
||||||
`(u/call! ^PolygonShape ~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
|
; listeners
|
||||||
|
|
||||||
(defn contact-listener
|
(defn contact-listener
|
||||||
|
|||||||
Reference in New Issue
Block a user