aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Letan <contact@thomasletan.fr>2018-07-04 00:13:44 +0200
committerThomas Letan <contact@thomasletan.fr>2018-07-04 00:13:44 +0200
commitcda14dc0c98ee8aeea98c02b1e52b74df2c1e2e1 (patch)
tree57619f02770b186cef6ceb71a56d24e3652e1f8d
parentchore: Add a LICENSE file (diff)
feature: Direction is now an angle, not an enum
-rw-r--r--lykanc/client.lisp29
-rw-r--r--lykanc/command.lisp7
-rw-r--r--lykanc/keyboard.lisp18
-rw-r--r--lykanc/lykanc.asd1
-rw-r--r--lykanc/message.lisp22
-rw-r--r--lykanc/puppet.lisp21
-rw-r--r--lykand/lib/lykan/character.ex27
-rw-r--r--lykand/lib/lykan/command.ex55
-rw-r--r--lykand/lib/lykan/puppeteer/player.ex18
-rw-r--r--lykand/lib/lykan/system/physics.ex10
-rw-r--r--lykand/mix.exs1
-rw-r--r--lykand/mix.lock5
12 files changed, 153 insertions, 61 deletions
diff --git a/lykanc/client.lisp b/lykanc/client.lisp
index fbb3a99..a42903d 100644
--- a/lykanc/client.lisp
+++ b/lykanc/client.lisp
@@ -38,9 +38,9 @@
(let* ((tmx-file (concatenate 'string *maps_dir* map-key ".tmx"))
(map-layer (make-instance 'fairy/tiled:tile-map :path tmx-file)))
(setf (fairy:get-child app :game-scene) map-layer))
- (setf (fairy:sort-with (get-objects-layer app))
- (lambda (p1 p2)
- (> (gamekit:y (fairy:origin p1)) (gamekit:y (fairy:origin p2))))))
+ (setf (fairy:sort-with (get-objects-layer app))
+ (lambda (p1 p2)
+ (> (gamekit:y (fairy:origin p1)) (gamekit:y (fairy:origin p2))))))
(defmethod get-objects-layer ((app client))
(fairy/tiled:get-map-layer (fairy:get-child app :game-scene) "objects"))
@@ -55,9 +55,10 @@
(defmethod attribute-puppet ((app client) key)
(setf (main-puppet app) key))
-(defmethod add-puppet ((app client) key x y)
+(defmethod add-puppet ((app client) key x y dir)
(let ((puppet (new-puppet "../example/assets/tilesets/character.tsx"
x y)))
+ (changes-direction puppet dir)
(fairy:add-child (get-objects-layer app)
puppet
:with-key key))
@@ -101,7 +102,7 @@
(force-cursor app vx vy)))
(defun get-angle (x y)
- (atan x y))
+ (+ (- (atan x y)) (/ pi 2)))
(defmethod force-cursor ((app client) x y)
(let* ((cursor (gamekit:vec2 x y))
@@ -138,23 +139,17 @@
(defmethod set-direction ((app client) dir)
(let ((already-moving? (current-direction (keyboard app))))
(add-direction (keyboard app) dir)
- (wsd:send-text (socket app) (cond
- ((eq dir :up) "UP")
- ((eq dir :down) "DOWN")
- ((eq dir :right) "RIGHT")
- ((eq dir :LEFT) "LEFT")))
+ (wsd:send-text (socket app)
+ (command-set-direction (current-direction-angle (keyboard app))))
(when (not already-moving?)
(wsd:send-text (socket app) "MOVE"))))
(defmethod unset-direction ((app client) dir)
(remove-direction (keyboard app) dir)
- (let ((dir (current-direction (keyboard app))))
- (if dir
- (wsd:send-text (socket app) (cond
- ((eq dir :up) "UP")
- ((eq dir :down) "DOWN")
- ((eq dir :right) "RIGHT")
- ((eq dir :LEFT) "LEFT")))
+ (let ((dir-angle (current-direction-angle (keyboard app))))
+ (if dir-angle
+ (wsd:send-text (socket app)
+ (command-set-direction dir-angle))
(wsd:send-text (socket app) "STOP"))))
(defmethod bind-direction ((app client) key dir)
diff --git a/lykanc/command.lisp b/lykanc/command.lisp
new file mode 100644
index 0000000..b399c5a
--- /dev/null
+++ b/lykanc/command.lisp
@@ -0,0 +1,7 @@
+(cl:in-package :lykanc)
+
+(defun command-set-direction (angle)
+ (jsown:to-json `(:obj
+ ("opcode" . "SET_DIRECTION")
+ ("arguments" . (:obj
+ ("angle" . ,angle))))))
diff --git a/lykanc/keyboard.lisp b/lykanc/keyboard.lisp
index b523a94..7dac922 100644
--- a/lykanc/keyboard.lisp
+++ b/lykanc/keyboard.lisp
@@ -16,3 +16,21 @@
(defmethod current-direction ((k keyboard))
(car (arrows k)))
+
+(defmethod current-direction-angle ((k keyboard))
+ (let ((d1 (car (arrows k)))
+ (d2 (car (cdr (arrows k)))))
+ (cond
+ ((and (eq d1 :up) (eq d2 :right)) (/ pi 4))
+ ((and (eq d1 :up) (eq d2 :left)) (* 3 (/ pi 4)))
+ ((and (eq d1 :up)) (/ pi 2))
+ ((and (eq d1 :right) (eq d2 :up)) (/ pi 4))
+ ((and (eq d1 :right) (eq d2 :down)) (/ (- pi) 4))
+ ((and (eq d1 :right)) 0)
+ ((and (eq d1 :down) (eq d2 :right)) (/ (- pi) 4))
+ ((and (eq d1 :down) (eq d2 :left)) (* 3 (/ (- pi) 4)))
+ ((and (eq d1 :down)) (/ (- pi) 2))
+ ((and (eq d1 :left) (eq d2 :down)) (* 3 (/ (- pi) 4)))
+ ((and (eq d1 :left) (eq d2 :up)) (* 3 (/ pi 4)))
+ ((and (eq d1 :left)) pi)
+ (t nil))))
diff --git a/lykanc/lykanc.asd b/lykanc/lykanc.asd
index 0b12546..ecd7d4d 100644
--- a/lykanc/lykanc.asd
+++ b/lykanc/lykanc.asd
@@ -15,6 +15,7 @@
(:file "assets")
(:file "puppet")
(:file "keyboard")
+ (:file "command")
(:file "client")
(:file "message")
(:file "lykanc")))
diff --git a/lykanc/message.lisp b/lykanc/message.lisp
index 7db8f50..0f9ac83 100644
--- a/lykanc/message.lisp
+++ b/lykanc/message.lisp
@@ -17,21 +17,25 @@
(jsown:do-json-keys (key puppet-desc) puppets
(add-puppet app key
(jsown:val puppet-desc "x")
- (jsown:val puppet-desc "y"))))))
+ (jsown:val puppet-desc "y")
+ (jsown:val puppet-desc "direction"))))))
;; PUPPET ENTERS
(defun handle-puppet-enters (message app)
(in-game-loop
(let* ((key (jsown:val message "puppet_key"))
(digest (jsown:val message "digest")))
- (add-puppet app key (jsown:val digest "x") (jsown:val digest "y")))))
+ (add-puppet app key
+ (jsown:val digest "x")
+ (jsown:val digest "y")
+ (jsown:val digest "direction")))))
;; PUPPET MOVES
(defun handle-puppet-moves (message app)
(in-game-loop
- (let* ((key (jsown:val message "puppet_key"))
- (position (jsown:val message "position")))
- (puppet-moves app key (jsown:val position "x") (jsown:val position "y")))))
+ (let* ((key (jsown:val message "puppet_key"))
+ (position (jsown:val message "position")))
+ (puppet-moves app key (jsown:val position "x") (jsown:val position "y")))))
;; PUPPET LEAVES
@@ -49,15 +53,9 @@
;; PUPPET DIRECTION
(defun handle-puppet-direction (message app)
(in-game-loop
- (let* ((str-dir (jsown:val message "direction"))
- (dir (cond
- ((string= str-dir "down") :down)
- ((string= str-dir "up") :up)
- ((string= str-dir "right") :right)
- ((string= str-dir "left") :left))))
(puppet-changes-direction app
(jsown:val message "puppet_key")
- dir))))
+ (jsown:val message "direction"))))
;; PUPPET STARTS ATTACKING
(defun handle-puppet-attacks (message app)
diff --git a/lykanc/puppet.lisp b/lykanc/puppet.lisp
index 40f3ee3..33102d2 100644
--- a/lykanc/puppet.lisp
+++ b/lykanc/puppet.lisp
@@ -86,18 +86,19 @@
(defmethod fairy:height ((p puppet))
(fairy:height (fairy:get-child p :character)))
-(defmethod changes-direction ((p puppet) dir)
- (when (not (eq (direction p) dir))
- (setf (direction p) dir)
- (update-animation p)))
+(defmethod changes-direction ((p puppet) angle)
+ (let ((dir (get-dir angle)))
+ (when (not (eq (direction p) dir))
+ (setf (direction p) dir)
+ (update-animation p))))
(defun get-dir (angle)
(cond
- ((<= angle (- (* 3 (/ pi 4)))) :down)
- ((<= angle (- (* 1 (/ pi 4)))) :left)
- ((<= angle (* 1 (/ pi 4))) :up)
- ((<= angle (* 3 (/ pi 4))) :right)
- (t :down)))
+ ((<= angle (- (* 3 (/ pi 4)))) :left)
+ ((<= angle (- (* 1 (/ pi 4)))) :down)
+ ((<= angle (* 1 (/ pi 4))) :right)
+ ((<= angle (* 3 (/ pi 4))) :up)
+ (t :left)))
(defmethod look-at ((p puppet) angle)
- (changes-direction p (get-dir angle)))
+ (changes-direction p angle))
diff --git a/lykand/lib/lykan/character.ex b/lykand/lib/lykan/character.ex
index e4247e9..0d5225f 100644
--- a/lykand/lib/lykan/character.ex
+++ b/lykand/lib/lykan/character.ex
@@ -19,37 +19,40 @@ defpuppet Lykan.Character do
{{box, vec}, state}
end
end
+
defmodule Body do
use Lykan.System.Physics.Body
def init_state(_key) do
- {:ok, :down}
+ {:ok, nil}
end
- def get_position(key, dir) do
+ def get_position(key, nil) do
Option.some(x) = read(key, :x)
Option.some(y) = read(key, :y)
- {Vector.new(x, y), dir}
+ {Vector.new(x, y), nil}
end
- def get_box(_key, dir) do
- {Box.new(24, 24), dir}
+ def get_box(_key, nil) do
+ {Box.new(24, 24), nil}
end
- def set_position(key, pos, dir) do
+ def set_position(key, pos, nil) do
write(key, :x, pos.x)
write(key, :y, pos.y)
- dir
+ nil
end
- def get_direction(_key, dir) do
- {dir, dir}
+ def get_direction(key, nil) do
+ Option.some(dir) = read(key, :direction)
+ {dir, nil}
end
- def set_direction(_key, dir, _) do
- dir
+ def set_direction(key, dir, _) do
+ write(key, :direction, dir)
+ nil
end
end
@@ -82,6 +85,8 @@ defpuppet Lykan.Character do
:color => "black",
:x => 50,
:y => 50,
+ :direction => Math.pi / 2,
+ :look_at => Math.pi / 2,
}
end
diff --git a/lykand/lib/lykan/command.ex b/lykand/lib/lykan/command.ex
new file mode 100644
index 0000000..8ba8da4
--- /dev/null
+++ b/lykand/lib/lykan/command.ex
@@ -0,0 +1,55 @@
+defmodule Lykan.Commands do
+ defmodule SetDirection do
+ defstruct [
+ :angle
+ ]
+ end
+
+ defmodule LookAt do
+ defstruct [
+ :angle
+ ]
+ end
+
+ defmodule StartsWalking do
+ defstruct []
+ end
+end
+
+defmodule Lykan.Command do
+ @derive [Poison.Encoder]
+ defstruct [
+ :opcode,
+ :arguments,
+ ]
+
+ @commands %{
+ "SET_DIRECTION" => %Lykan.Commands.SetDirection{},
+ "LOOK_AT" => %Lykan.Commands.LookAt{},
+ "STARTS_WALKING" => %Lykan.Commands.StartsWalking{},
+ }
+
+ def decode(json) do
+ cmd = Poison.decode(json, as: %__MODULE__{})
+
+ case cmd do
+ {:ok, cmd} ->
+ mod = Enum.reduce_while(@commands, nil, fn ({k, v}, acc) ->
+ if k == cmd.opcode do
+ {:halt, v}
+ else
+ {:cont, acc}
+ end
+ end)
+
+ if mod do
+ # TODO: Find a better way to do it
+ case Poison.decode(Poison.encode!(cmd.arguments), as: mod) do
+ {:ok, cmd} -> cmd
+ _ -> json
+ end
+ end
+ _ -> json
+ end
+ end
+end
diff --git a/lykand/lib/lykan/puppeteer/player.ex b/lykand/lib/lykan/puppeteer/player.ex
index cc435ee..623da92 100644
--- a/lykand/lib/lykan/puppeteer/player.ex
+++ b/lykand/lib/lykan/puppeteer/player.ex
@@ -2,8 +2,8 @@ use Lkn.Prelude
import Lykan.Message, only: [defmessage: 2]
+alias Lykan.Commands, as: C
alias Lykan.System.Physics.PuppetHitsTeleport
-alias Lkn.Physics.Geometry.Vector
defmodule Lykan.Puppeteer.Player do
defmessage AttributePuppet do
@@ -77,7 +77,7 @@ defmodule Lykan.Puppeteer.Player do
cast inject(cmd :: any) do
case instance_key do
- Option.some(instance_key) -> consume_cmd(cmd, state, instance_key)
+ Option.some(instance_key) -> consume_cmd(Lykan.Command.decode(cmd), state, instance_key)
Option.nothing() -> cast_return()
end
end
@@ -93,6 +93,16 @@ defmodule Lykan.Puppeteer.Player do
Lkn.Core.Puppeteer.start_link(__MODULE__, puppeteer_key, socket: socket, main: main)
end
+ defp consume_cmd(cmd = %C.SetDirection{}, state, instance_key) do
+ Lykan.System.Physics.puppet_changes_dir(instance_key, state.puppet, cmd.angle)
+ cast_return()
+ end
+
+ defp consume_cmd(cmd = %C.LookAt{}, _state, _instance_key) do
+ IO.puts cmd.angle
+ cast_return()
+ end
+
defp consume_cmd("MOVE", state, instance_key) do
Lykan.System.Physics.puppet_starts_moving(instance_key, state.puppet)
@@ -124,7 +134,7 @@ defmodule Lykan.Puppeteer.Player do
end
defp consume_cmd("LEFT", state, instance_key) do
- Lykan.System.Physics.puppet_changes_dir(instance_key, state.puppet, :left)
+ Lykan.System.Physics.puppet_changes_dir(instance_key, state.puppet, Math.pi)
cast_return()
end
@@ -141,7 +151,7 @@ defmodule Lykan.Puppeteer.Player do
cast_return()
end
- defp consume_cmd(cmd, _state, _instance_key) do
+ defp consume_cmd(_cmd, _state, _instance_key) do
# unknown command, we bail and do nothing for now
cast_return()
diff --git a/lykand/lib/lykan/system/physics.ex b/lykand/lib/lykan/system/physics.ex
index 4ff775d..1a1635c 100644
--- a/lykand/lib/lykan/system/physics.ex
+++ b/lykand/lib/lykan/system/physics.ex
@@ -230,12 +230,10 @@ defsystem Lykan.System.Physics do
dir = Body.get_direction(puppet_key)
{vec, col} =
- Physics.World.move(state.world, puppet_key, case dir do
- :up -> Vector.new(0, 8)
- :down -> Vector.new(0, -8)
- :right -> Vector.new(8, 0)
- :left -> Vector.new(-8, 0)
- end)
+ Physics.World.move(
+ state.world,
+ puppet_key,
+ Vector.from_polar(8, dir))
col = handle_collision(state, puppet_key, col)
diff --git a/lykand/mix.exs b/lykand/mix.exs
index 08966f7..54049fd 100644
--- a/lykand/mix.exs
+++ b/lykand/mix.exs
@@ -31,6 +31,7 @@ defmodule Lykan.Mixfile do
{:lkn_physics, path: "../../lkn-physics"},
{:lkn_prelude, "~> 0.1.2"},
{:beacon, "~> 1.1"},
+ {:math, "~> 0.3"},
{:socket, "~> 0.3"},
{:poison, "~> 3.1.0"},
{:postgrex, "~> 0.13"},
diff --git a/lykand/mix.lock b/lykand/mix.lock
index eb22595..4eb4229 100644
--- a/lykand/mix.lock
+++ b/lykand/mix.lock
@@ -3,13 +3,16 @@
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], []},
"db_connection": {:hex, :db_connection, "1.1.3", "89b30ca1ef0a3b469b1c779579590688561d586694a3ce8792985d4d7e575a61", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]},
"decimal": {:hex, :decimal, "1.5.0", "b0433a36d0e2430e3d50291b1c65f53c37d56f83665b43d79963684865beab68", [:mix], []},
- "distillery": {:hex, :distillery, "1.5.2", "eec18b2d37b55b0bcb670cf2bcf64228ed38ce8b046bb30a9b636a6f5a4c0080", [:mix], []},
+ "distillery": {:hex, :distillery, "1.5.3", "b2f4fc34ec71ab4f1202a796f9290e068883b042319aa8c9aa45377ecac8597a", [:mix], []},
"earmark": {:hex, :earmark, "1.2.5", "4d21980d5d2862a2e13ec3c49ad9ad783ffc7ca5769cf6ff891a4553fbaae761", [:mix], []},
"ecto": {:hex, :ecto, "2.2.10", "e7366dc82f48f8dd78fcbf3ab50985ceeb11cb3dc93435147c6e13f2cda0992e", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]},
"esqlite": {:hex, :esqlite, "0.2.3", "1a8b60877fdd3d50a8a84b342db04032c0231cc27ecff4ddd0d934485d4c0cd5", [:rebar3], []},
"ex_doc": {:hex, :ex_doc, "0.18.3", "f4b0e4a2ec6f333dccf761838a4b253d75e11f714b85ae271c9ae361367897b7", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]},
"lkn_core": {:hex, :lkn_core, "0.4.3", "87a3eb148ec94ee9b396a35bc06549ca628445a579d81f2455aec7a92fd17274", [:mix], [{:beacon, "~> 1.1", [hex: :beacon, optional: false]}, {:lkn_prelude, "~> 0.1.2", [hex: :lkn_prelude, optional: false]}, {:uuid, "~> 1.1", [hex: :uuid, optional: false]}]},
"lkn_prelude": {:hex, :lkn_prelude, "0.1.2", "a5daa06c097bc00b9c6f77e7c02c028adc4fd7656445bfffa92e021eeb8375dd", [:mix], []},
+ "mapail": {:hex, :mapail, "1.0.2", "45d0bc390b965f21b80c02e8fb2279f9bcc57cf5c78f964ef63113317d6944d1", [:mix], [{:maptu, "~> 1.0.0", [hex: :maptu, optional: false]}]},
+ "maptu": {:hex, :maptu, "1.0.0", "3f1915d3241a733f46a39e935bed369faaf4ff69182b95ea58e9ad1b065c3b11", [:mix], []},
+ "math": {:hex, :math, "0.3.0", "e14e7291115201cb155a3567e66d196bf5088a6f55b030d598107d7ae934a11c", [:mix], []},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
"postgrex": {:hex, :postgrex, "0.13.5", "3d931aba29363e1443da167a4b12f06dcd171103c424de15e5f3fc2ba3e6d9c5", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]},