-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sliding animation working but with flicker
- Loading branch information
Showing
8 changed files
with
122 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,40 @@ | ||
(ns cljs-2048.animation | ||
(:require [cljs-2048.board :as b] | ||
[cljs.spec.alpha :as s])) | ||
(:require [cljs-2048.game :as g])) | ||
|
||
(def board-size 4) ; Add constant for magic number 4 | ||
|
||
(s/def ::position (s/tuple int? int?)) | ||
(s/def ::moves (s/map-of ::position ::position)) | ||
(defn get-transition-style | ||
"Return --move-x and --move-y based on direction and row/col | ||
Move must be within bounds of the grid with 4 rows and 4 columns. | ||
Maximum move is 3 rows or columns in any direction. | ||
If col is 0, and direction is right, we can move 3 more columns to the right. | ||
If col is 1, and direction is right, we can move 2 more columns to the right. | ||
If col is 2, and direction is right, we can move 1 more columns to the right. | ||
If col is 3, and direction is right, we can move 0 more columns to the right. | ||
(defn board->tile-positions | ||
"Creates a map of positions to tile values, excluding empty tiles" | ||
[board] | ||
(->> (for [row (range board-size) | ||
col (range board-size) | ||
:let [tile (b/get-tile board row col)] | ||
:when (not= [0] tile)] | ||
[[row col] (first tile)]) | ||
(into {}))) | ||
If col is 0, and direction is left, we can move 0 more columns to the left. | ||
If col is 1, and direction is left, we can move 1 more columns to the left. | ||
If col is 2, and direction is left, we can move 2 more columns to the left. | ||
If col is 3, and direction is left, we can move 3 more columns to the left. | ||
(defn find-tile-destination | ||
"Find the destination position for a tile value in the new board. | ||
Returns [row col] of destination or nil if no move." | ||
[value old-pos new-board] | ||
(->> (for [row (range board-size) | ||
col (range board-size) | ||
:let [new-val (first (b/get-tile new-board row col))] | ||
:when (and (not= [row col] old-pos) | ||
(or (= new-val value) | ||
(= new-val (* 2 value))))] | ||
[row col]) | ||
first)) | ||
If row is 0, and direction is down, we can move 3 more rows down. | ||
(defn calculate-moves | ||
"Calculate the moves for each tile from old board to new board. | ||
Returns a map of {[old-row old-col] [new-row new-col]} for each moved tile." | ||
[old-board new-board] | ||
(if (= old-board new-board) | ||
{} | ||
(let [old-positions (board->tile-positions old-board)] | ||
(->> old-positions | ||
(keep (fn [[old-pos value]] | ||
(when-let [new-pos (find-tile-destination value old-pos new-board)] | ||
[old-pos new-pos]))) | ||
(into {}))))) | ||
If row is 3, and direction is up, we can move 3 more rows up. | ||
(defn get-transition-class | ||
"Get the CSS class for tile position" | ||
[[row col]] | ||
(str "translate-x-" (* col 100) "% translate-y-" (* row 100) "%")) | ||
We multiply by 8em to match the tile size in the css grid." | ||
[[row col] direction] | ||
(let [max-moves 3 | ||
[move-x move-y] (case direction | ||
::g/right (if (< col max-moves) | ||
[(- max-moves col) 0] | ||
[0 0]) | ||
::g/left (if (pos? col) | ||
[(- col) 0] | ||
[0 0]) | ||
::g/down (if (< row max-moves) | ||
[0 (- max-moves row)] | ||
[0 0]) | ||
::g/up (if (pos? row) | ||
[0 (- row)] | ||
[0 0]))] | ||
|
||
(defn get-transition-style | ||
"Get the CSS class for tile position" | ||
[[old-row old-col] [new-row new-col]] | ||
{"--move-x" (str (* (- new-row old-row) 8) "em") | ||
"--move-y" (str (* (- new-col old-col) 8) "em")}) | ||
{"--move-x" (str (* move-x 8) "em") | ||
"--move-y" (str (* move-y 8) "em")})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.