From 866425423b0ab0e2287bde92d8c23baf3cc80bf1 Mon Sep 17 00:00:00 2001 From: Friedrich von Never Date: Sun, 7 Jan 2024 00:13:39 +0100 Subject: [PATCH] (#32) Web client: remove --- CONTRIBUTING.md | 10 -- build.gradle.kts | 1 - settings.gradle.kts | 2 +- web/frontend/build.gradle.kts | 41 ------- .../main/clojurescript/hyperspace/core.cljs | 43 ------- .../main/clojurescript/hyperspace/game.cljs | 90 --------------- .../clojurescript/hyperspace/geometry.cljs | 66 ----------- .../clojurescript/hyperspace/gravity.cljs | 17 --- .../main/clojurescript/hyperspace/misc.cljs | 18 --- .../main/clojurescript/hyperspace/render.cljs | 73 ------------ .../main/clojurescript/hyperspace/webgl.cljs | 70 ------------ .../main/clojurescript/hyperspace/world.cljs | 108 ------------------ web/frontend/src/main/html/index.html | 13 --- 13 files changed, 1 insertion(+), 551 deletions(-) delete mode 100644 web/frontend/build.gradle.kts delete mode 100644 web/frontend/src/main/clojurescript/hyperspace/core.cljs delete mode 100644 web/frontend/src/main/clojurescript/hyperspace/game.cljs delete mode 100644 web/frontend/src/main/clojurescript/hyperspace/geometry.cljs delete mode 100644 web/frontend/src/main/clojurescript/hyperspace/gravity.cljs delete mode 100644 web/frontend/src/main/clojurescript/hyperspace/misc.cljs delete mode 100644 web/frontend/src/main/clojurescript/hyperspace/render.cljs delete mode 100644 web/frontend/src/main/clojurescript/hyperspace/webgl.cljs delete mode 100644 web/frontend/src/main/clojurescript/hyperspace/world.cljs delete mode 100644 web/frontend/src/main/html/index.html diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index bf67eab..11c205c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -13,16 +13,6 @@ To run the standalone game client, use the following shell command: $ ./gradlew client:run ``` -## Web client - -To run the web version of the game, use the following shell command: - -```console -$ ./gradlew web:frontend:build -``` - -Then open a file `web/frontend/build/www/index.html` using a web browser. - ## Game server To run the game server, use the following shell command: diff --git a/build.gradle.kts b/build.gradle.kts index 70ce176..e693735 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,4 @@ plugins { id("dev.clojurephant.clojure") version "0.6.0" apply false - id("dev.clojurephant.clojurescript") version "0.6.0" apply false id("com.stehno.natives") version "0.3.1" apply false } diff --git a/settings.gradle.kts b/settings.gradle.kts index 5157320..6d60d6f 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,2 +1,2 @@ rootProject.name = "hyperspace" -include("client", "core", "server", "web", "web:frontend") +include("client", "core", "server", "web") diff --git a/web/frontend/build.gradle.kts b/web/frontend/build.gradle.kts deleted file mode 100644 index cc6f64f..0000000 --- a/web/frontend/build.gradle.kts +++ /dev/null @@ -1,41 +0,0 @@ -plugins { - application - id("dev.clojurephant.clojurescript") -} - -// Dependencies -repositories { - mavenCentral() - maven { - name = "clojars" - url = uri("https://repo.clojars.org") - } -} - -dependencies { - implementation("org.clojure:clojure:1.10.3") - implementation("org.clojure:clojurescript:1.10.773") -} - -tasks.register("copyHtml") { - from("src/main/html") - into("$buildDir/www") -} - -tasks.build { - dependsOn("copyHtml") -} - -clojurescript { - builds { - all { - this as dev.clojurephant.plugin.clojurescript.ClojureScriptBuild - compiler { - outputTo.set(file("$buildDir/www/js/main.js")) - outputDir.set(file("$buildDir/www/js/out")) - main = "hyperspace.core" - assetPath = "js/out" - } - } - } -} diff --git a/web/frontend/src/main/clojurescript/hyperspace/core.cljs b/web/frontend/src/main/clojurescript/hyperspace/core.cljs deleted file mode 100644 index 79a29bd..0000000 --- a/web/frontend/src/main/clojurescript/hyperspace/core.cljs +++ /dev/null @@ -1,43 +0,0 @@ -(ns hyperspace.core - (:require [hyperspace.webgl :as webgl] - [hyperspace.render :as render] - [hyperspace.game :as game] - [hyperspace.world :as world])) - -(defn get-time - [] - (.now js/Date)) - -(defn process-input - [world] -; TODO: process keys - world) - -(defn webgl-loop - [canvas context initial-world] - (def timestamp-atom (atom (get-time))) - (def accumulated-time-atom (atom 0)) - (def world-atom (atom initial-world)) - (def render (fn [] - (let [timestamp (deref timestamp-atom) - accumulated-time (deref accumulated-time-atom) - world (deref world-atom) - new-timestamp (get-time) - delta-time (+ (- new-timestamp timestamp) accumulated-time) - [new-world remaining-time] (game/update-world world delta-time) - final-world (process-input new-world)] - (render/render-world context final-world) - (reset! timestamp-atom new-timestamp) - (reset! accumulated-time-atom remaining-time) - (reset! world-atom final-world)) - (js/requestAnimationFrame render canvas))) - (render)) - -;; Run main function: -(set! (.-onload js/window) - (fn [] - (let [canvas (.getElementById js/document "canvas") - context (webgl/get-context canvas) - world (world/generate-world 800 600)] - (webgl/init context) - (webgl-loop canvas context world)))) diff --git a/web/frontend/src/main/clojurescript/hyperspace/game.cljs b/web/frontend/src/main/clojurescript/hyperspace/game.cljs deleted file mode 100644 index 2f4e137..0000000 --- a/web/frontend/src/main/clojurescript/hyperspace/game.cljs +++ /dev/null @@ -1,90 +0,0 @@ -(ns hyperspace.game - (:require [hyperspace.geometry :as geometry] - [hyperspace.gravity :as gravity] - [hyperspace.world :as world])) - -(defn update-particle - [{position :position - velocity :velocity - :as particle} - planets - delta-time] - (let [acceleration (apply geometry/vector-sum - (map #(gravity/gravity-acceleration particle %) - planets)) - ;; FIXME: Duplicate code - new-velocity (-> acceleration - (geometry/multiply-by-scalar (* delta-time 1e-3)) - (geometry/vector-sum velocity)) - new-position (-> new-velocity - (geometry/multiply-by-scalar (* delta-time 1e-3)) - (geometry/vector-sum position))] - (assoc particle - :position new-position - :velocity new-velocity))) - -(defn update-traces - [traces - {position :position - trace-index :trace-index}] - (update-in traces [trace-index] conj position)) - -(defn break-particle - [{particle-position :position - particle-radius :radius - velocity :velocity - radius :radius - :as particle} - planets] - (let [planet (some #(when (geometry/circle-X-circle?? % particle) %) planets)] - (if (and planet (> radius 1.0)) - (let [{planet-position :position - planet-radius :radius} planet - - fragments-position (-> (geometry/vector-subtract particle-position planet-position) - geometry/normalize-vector - (geometry/multiply-by-scalar (+ particle-radius planet-radius 5)) - (geometry/vector-sum planet-position))] - (repeatedly 5 - (fn [] - (world/make-fragment fragments-position - (geometry/polar->cartesian [(rand (* 2 Math/PI)), - (/ (geometry/vector-length velocity) 2)]) - (- radius 2))))) - ()))) - -(defn exit [world] - (assoc world - :exit true)) - -(def simulation-step 10) - -(defn update-world - [{missiles :missiles - planets :planets - fragments :fragments - traces :traces - :as world} - delta-time] - (if (<= delta-time simulation-step) - [world delta-time] - (let [broken-particles (mapcat #(break-particle % planets) - (concat missiles fragments)) - ;; FIXME: Duplicate code - new-missiles (->> missiles - (filter #(geometry/circle-X-rectangle?? % world)) - (remove #(geometry/circle-X-any-circle?? % planets)) - (map #(update-particle % planets simulation-step))) - new-fragments (->> fragments - (concat broken-particles) - (filter #(geometry/circle-X-rectangle?? % world)) - (remove #(geometry/circle-X-any-circle?? % planets)) - (map #(update-particle % planets simulation-step))) - - new-traces (reduce update-traces traces new-missiles)] - (recur - (assoc world - :missiles new-missiles - :fragments new-fragments - :traces new-traces) - (- delta-time simulation-step))))) diff --git a/web/frontend/src/main/clojurescript/hyperspace/geometry.cljs b/web/frontend/src/main/clojurescript/hyperspace/geometry.cljs deleted file mode 100644 index e538f18..0000000 --- a/web/frontend/src/main/clojurescript/hyperspace/geometry.cljs +++ /dev/null @@ -1,66 +0,0 @@ -(ns hyperspace.geometry) - -(def - ^{:arglists '([vtr & vtrs]) - :doc "Returns the sum of vectors."} - vector-sum (partial mapv +)) - -(def - ^{:arglists '([vtr & vtrs]) - :doc "If vtrs are supplied, returns the negation of vtr, else - subtracts the vtrs from vtr and returns the result."} - vector-subtract - (partial mapv -)) - -(defn multiply-by-scalar - "Returns the multiplication of vtr and scalar." - [vtr scalar] - (mapv #(* scalar %) vtr)) - -(defn polar->cartesian - "Converts polar coordinates to the cartesian ones." - [[a, d]] - [(* d (Math/cos a)) - (* d (Math/sin a))]) - -(defn cartesian->polar - "Converts cartesian cooradinates to the polar ones." - [[x, y]] - [(Math/atan2 y x) - (Math/sqrt (+ (* x x) (* y y)))]) - -(defn vector-length - "Returns the length of vtr." - [vtr] - (Math/sqrt (reduce #(+ %1 (* %2 %2)) 0.0 vtr))) - -(defn normalize-vector - "Normalizes vector, that is returns a vector with the same - direction, but with the length equal 1." - [vtr] - (let [length (vector-length vtr)] - (mapv #(/ % length) vtr))) - -(defn distance - "Returns the distance between p1 and p2." - [p1 p2] - (vector-length (vector-subtract p1 p2))) - -(defn circle-X-circle? - "Does the first circle intersects the second one?" - [{position1 :position radius1 :radius} - {position2 :position radius2 :radius}] - (<= (distance position1 position2) - (+ radius1 radius2))) - -(defn circle-X-any-circle? - "Does the circle intersects any other circles?" - [circle other-circles] - (some #(circle-X-circle? % circle) other-circles)) - -(defn circle-X-rectangle? - "Does the circle intersects the rectangle?" - [{[cx, cy] :position radius :radius} - {[rx, ry] :position [width, height] :size}] - (and (<= (- rx radius) cx (+ rx width radius)) - (<= (- ry radius) cy (+ ry height radius)))) \ No newline at end of file diff --git a/web/frontend/src/main/clojurescript/hyperspace/gravity.cljs b/web/frontend/src/main/clojurescript/hyperspace/gravity.cljs deleted file mode 100644 index fe3cb4b..0000000 --- a/web/frontend/src/main/clojurescript/hyperspace/gravity.cljs +++ /dev/null @@ -1,17 +0,0 @@ -(ns hyperspace.gravity - (:require [hyperspace.geometry :as geometry])) - -(def gravity-constant 0.1) - -(defn gravity-acceleration - "Returns acceleration for the first object under gravity's impact of - the second one." - [{position1 :position mass1 :mass} - {position2 :position mass2 :mass}] - (let [d (geometry/distance position1 position2) - force (/ (* gravity-constant mass1 mass2) - (* d d)) - acceleration (/ force mass1)] - (-> (geometry/vector-subtract position2 position1) - geometry/normalize-vector - (geometry/multiply-by-scalar acceleration)))) \ No newline at end of file diff --git a/web/frontend/src/main/clojurescript/hyperspace/misc.cljs b/web/frontend/src/main/clojurescript/hyperspace/misc.cljs deleted file mode 100644 index dc01c6f..0000000 --- a/web/frontend/src/main/clojurescript/hyperspace/misc.cljs +++ /dev/null @@ -1,18 +0,0 @@ -(ns hyperspace.misc) - -(defn rand-range - "Returns a random integer between low (inclusive) and - high (inclusive)" - [low high] - (-> (- high low) - (+ 1) - rand-int - (+ low))) - -(defn saturation - "Ensures that the value is not going out of bounds [low, high]" - [value low high] - (cond - (< value low) low - (< high value) high - :else value)) \ No newline at end of file diff --git a/web/frontend/src/main/clojurescript/hyperspace/render.cljs b/web/frontend/src/main/clojurescript/hyperspace/render.cljs deleted file mode 100644 index 5398176..0000000 --- a/web/frontend/src/main/clojurescript/hyperspace/render.cljs +++ /dev/null @@ -1,73 +0,0 @@ -(ns hyperspace.render - (:require [hyperspace.webgl :as webgl])) - -(defn render-missile - [context - {position :position - radius :radius - :as missile}] -; (GL11/glColor3f 1.0 0.0 1.0) - (webgl/ellipse context position radius 30) - missile) - -(defn render-planet - [context - {position :position - radius :radius - :as planet}] -; (GL11/glColor3f 0.0 1.0 0.0) - (webgl/ellipse context position radius 30) - planet) - -(defn render-fragment - [context - {position :position - radius :radius - :as fragment}] -; (GL11/glColor3f 1.0 0.0 0.0) - (webgl/ellipse context position radius 30) - fragment) - -(defn render-trace - [context - trace] -; (GL11/glColor3f 1.0 1.0 0.0) -; (GL11/glBegin GL11/GL_LINE_STRIP) -; (doseq [[x, y] trace] -; (GL11/glVertex2f x y)) -; (GL11/glEnd) - trace) - -(defn render-player - [context - {[x, y :as position] :position - radius :radius - [a, d :as heading] :heading - :as player}] -; (GL11/glColor3f 0.0 1.0 1.0) - (webgl/ellipse context position radius 30) - -; (GL11/glColor3f 1.0 1.0 1.0) -; (GL11/glBegin GL11/GL_LINES) -; (let [[hx, hy] (-> (polar->cartesian [a (* d 10)]) -; (vector-sum position))] -; (GL11/glVertex2f x y) -; (GL11/glVertex2f hx hy)) -; (GL11/glEnd) - player) - -(defn render-world - [context - {planets :planets - missiles :missiles - fragments :fragments - traces :traces - players :players - :as world}] - (webgl/clear context) - (doseq [trace traces] (render-trace context trace)) - (doseq [planet planets] (render-planet context planet)) - (doseq [missile missiles] (render-missile context missile)) - (doseq [fragment fragments] (render-fragment context fragment)) - (doseq [player players] (render-player context player)) - world) \ No newline at end of file diff --git a/web/frontend/src/main/clojurescript/hyperspace/webgl.cljs b/web/frontend/src/main/clojurescript/hyperspace/webgl.cljs deleted file mode 100644 index 6d58ea6..0000000 --- a/web/frontend/src/main/clojurescript/hyperspace/webgl.cljs +++ /dev/null @@ -1,70 +0,0 @@ -(ns hyperspace.webgl) - -(def vertex-shader " -attribute vec2 pos; - -void main() -{ - gl_Position = vec4(pos, 0, 1); -} -") - -(def fragment-shader " -precision mediump float; - -void main() -{ - gl_FragColor = vec4(0, 0.8, 0, 1); -} -") - -(defn create-shader - [gl text type] - (let [shader (.createShader gl type)] - (.shaderSource gl shader text) - (.compileShader gl shader) - shader)) - -(defn create-program - [gl vstr fstr] - (let [program (.createProgram gl) - vshader (create-shader gl vstr (.-VERTEX_SHADER gl)) - fshader (create-shader gl fstr (.-FRAGMENT_SHADER gl))] - (.attachShader gl program vshader) - (.attachShader gl program fshader) - (.linkProgram gl program) - program)) - -(defn get-context - [canvas] - (.getContext canvas "experimental-webgl")) - -(defn init - [context] - (let [buffer (.createBuffer context) - program (create-program context vertex-shader fragment-shader)] - (.bindBuffer context (.-ARRAY_BUFFER context) buffer) - (.useProgram context program) - (set! (.-vertexPosAttrib program) (.getAttribLocation context program "pos")) - (.enableVertexAttribArray context (.-vertexPosAttrib program)) - (.vertexAttribPointer context (.-vertexPosAttrib program) - 2 - (.-FLOAT context) - false - 0 - 0))) - -(defn clear - [context] - (.clear context (.-COLOR_BUFFER_BIT context))) - -(defn ellipse - [context [x, y] radius segments] -;; Let's call triangle as 'circle'. - (let [coords (js/Array. x (- x radius) - (- x (/ radius 2)) (- y (/ radius 2)) - (+ x (/ radius 2)) (- y (/ radius 2)))] - (.bufferData context (.-ARRAY_BUFFER context) - (js/Float32Array. coords) - (.-STATIC_DRAW context)) - (.drawArrays context (.-TRIANGLES context) 0 3))) diff --git a/web/frontend/src/main/clojurescript/hyperspace/world.cljs b/web/frontend/src/main/clojurescript/hyperspace/world.cljs deleted file mode 100644 index 003af6f..0000000 --- a/web/frontend/src/main/clojurescript/hyperspace/world.cljs +++ /dev/null @@ -1,108 +0,0 @@ -(ns hyperspace.world - (:require [hyperspace.misc :as misc])) - -(def missile-radius 5) -(def player-radius 15) -(def missile-mass 10) - -(def amount-of-planets 3) -(def amount-of-players 2) -(def amount-of-missiles 0) - -;;; Planets related stuff - -(defn make-planet - [position radius] - - {:position position - :radius radius - :mass (* (Math/sqrt radius) - (Math/pow 10 7))}) - -(defn add-random-planet - [{planets :planets - [x, y] :position - [width, height] :size - :as world}] - (let [radius (-> (min width height) (/ 5) rand) - x (misc/rand-range (+ x radius) (- (+ x width) radius)) - y (misc/rand-range (+ y radius) (- (+ y height) radius))] - (assoc world - :planets (conj planets (make-planet [x, y] radius))))) - -(defn generate-planets - [world] - (-> (iterate add-random-planet world) - (nth amount-of-planets))) - -;;; Missles related stuff - -(defn make-missile - [position velocity trace-index] - - {:position position - :velocity velocity - :radius missile-radius - :mass missile-mass - :trace-index trace-index}) - -(defn add-missile - [{missiles :missiles - traces :traces - :as world} - position velocity] - (let [new-missile (make-missile position - velocity - (count traces))] - (assoc world - :missiles (conj missiles new-missile) - :traces (conj traces [])))) - -;;; Fragments related stuff - -(defn make-fragment - [position velocity radius] - - {:position position - :velocity velocity - :radius radius - :mass radius}) - -;;; Players related stuff - -(defn make-player - [position heading] - - {:position position - :heading heading - :radius player-radius}) - -(defn add-random-player - [{[x, y] :position - [width, height] :size - players :players - :as world}] - (let [x (misc/rand-range (+ x player-radius) (- (+ x width) player-radius)) - y (misc/rand-range (+ y player-radius) (- (+ y height) player-radius))] - (assoc world - :players (conj players (make-player [x, y] [0, 3.0]))))) - -(defn generate-players - [world] - (-> (iterate add-random-player world) - (nth amount-of-players))) - -;;; World related stuff - -(defn generate-world - [width height] - (->> {:position [0, 0] - :size [width, height] - :players [] - :planets () - :missiles () - :fragments () - :traces [] - :exit false} - generate-players - generate-planets)) \ No newline at end of file diff --git a/web/frontend/src/main/html/index.html b/web/frontend/src/main/html/index.html deleted file mode 100644 index 051d1a7..0000000 --- a/web/frontend/src/main/html/index.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - Hyperspace - - - - -

Hyperspace

- - -