(defn spawn-words-button
“Starts the game with a preset set of words.”
[]
(let [game-started (rf/subscribe [::events/game-started])]
(when-not @game-started
[:button
{:on-click #(rf/dispatch [::events/set-words-and-letters words/word-collection])
:class “button-style”}
“Get Letters!”])))
(defn text-input
“Field for the user to input a word of their choosing.”
[]
(let [input-value (rf/subscribe [::events/current-input])]
[:input {:type “text”
:placeholder “Type here!”
:value @input-value
:on-change #(rf/dispatch [::events/set-current-input (-> % .-target .-value)])
:class “input-style”}]))
(defn shuffle-order-button!
“Shuffles the order of the letters displayed.”
[display-letters]
[:button {:on-click #(rf/dispatch [::events/shuffle-letter-order display-letters])
:class “button-style”}
“Shuffle letters”])
[:div {:class “side-panel-style”}
[:h3
“Found words:”]
[:ul (for [word (sort @found-words)] ; sort found words into an alphabetical list
[:li word])]]])
]]]]))
(defn init []
(install-global-key-listeners) ; listen for keypress events
(rf/dispatch-sync [::events/initialize-db]) ; get re-frame atom initialized
(stylefy/init {:dom (stylefy-reagent/init)}) ; set up css
(dev-setup)
(mount-root))
(defn find-common-letter [word-set]
(reduce
set/intersection
(map set (seq word-set))))
(defn validate-word
“Checks the given word against the current word list and letter set to see if it is valid. Gives the following keywords as a result.
:submit-ok :too-short :invalid :no-common :not-in-list :other”
[word word-list letters common-letter]
(cond
(contains? word-list word) :submit-ok ; first check if the word is in the word collection
(> 4 (count (seq word))) :too-short ; check length, notify if less than 3 letters
(not (every? letters (set word))) :invalid ; check if every letter in the word is in letters set
(not (contains? (set word) (first common-letter))) :no-common ; if it does not contain the common letter
(contains? (set word) (first common-letter)) :not-in-list ; then check if the word at least contains common letter
:else :other)) ; generic if it somehow manages to not match one of the above
(rf/reg-event-db ::submit-word
(fn [db [_ word]]
(let [letters (:letters db)
common-letter (:common-letter db)
words (:words db)
point-val (calculate-points word letters)]
(case (validate-word word words letters common-letter)
:submit-ok (if (contains? (:found-words db) word)
(assoc db :message “You’ve already found that word!”)
(-> db
(update :found-words conj word)
(update :score + point-val)
(assoc :message (str "Great job! You found " word ", worth a score of " point-val “!”)))) ; add the valid word to found words
:too-short (assoc db :message “Only words with 4 letters or more count.”)
:not-in-list (assoc db :message (str “Sorry, " word " isn’t in the word list today.”))
:no-common (assoc db :message “Nice try, but the word needs to contain the common letter.”)
:invalid (assoc db :message “All letters in the word must be from the given letter set.”)
:other (assoc db :message “Try again.”)))))
; use reg-event-fx to dispatch further event to clear input
Can you help me add screenshake events on getting the wrong word?
To implement a screenshake effect in your ClojureScript Spelling Bee game when a user submits an incorrect word, you can use CSS animations for the shake effect and apply the animation to the main container when necessary.
First, let's define a CSS animation for the screenshake effect. You can put the CSS directly into your HTML file within a <style> tag, or add it to your separate CSS file if you have one:
Next, let's add the code to trigger the effect within your ::submit-word handler when a word is invalid. You'll want to define a new key in your app state (:shake?) that will control whether the screenshake animation is applied:
clojure
(rf/reg-event-fx
::submit-word
(fn [{:keys [db]} [_ word]]
(let [letters (:letters db)
common-letter (:common-letter db)
words (:words db)
point-val (calculate-points word letters)
shake? (not= (validate-word word words letters common-letter) :submit-ok)]
{:db (-> db
(assoc :shake? shake?)
(case (validate-word word words letters common-letter)
:submit-ok (if (contains? (:found-words db) word)
(assoc :message "You've already found that word!")
(-> db
(update :found-words conj word)
(update :score + point-val)
(assoc :message (str "Great job! You found " word ", worth a score of " point-val "!"))))
:too-short (assoc :message "Only words with 4 letters or more count.")
; rest of the cases here
))
:fx (when shake? [::trigger-shake])})))
(rf/reg-fx
::trigger-shake
(fn []
(let [root (.querySelector js/document ".main-style")]
(.classList.add root "shake")
(js/setTimeout
#(when root
(.classList.remove root "shake"))
500))))
Finally, let's update the main-panel function to include the screenshake class when the :shake? key in the app state is true:
clojure
(defn main-panel []
;; ... rest of your code
(with-let [name (rf/subscribe [::events/name])
;; ... rest of your subscriptions
shake? (rf/subscribe [::events/shake?])]
;; ... within your HTML rendering
[:div {:class (str "main-style" (when @shake? " shake"))}
;; ... rest of your code for the div
]))
The proposed changes will add the shake CSS class to your .main-style div whenever an invalid word is submitted, which will then trigger the animation. After a certain time (500ms in this example), we remove the class to stop the animation. The :trigger-shake effect handles this logic.
Make sure you also subscribe to the :shake? key in your subscriptions:
clojure
(rf/reg-sub
::shake?
(fn [db]
(:shake? db)))
With this setup, every time an invalid word is submitted, you should see a screenshake effect on the main interface of your game.
It adds the class shake to the list of classes of this element. This triggers the CSS animation defined for the shake class, causing the screenshake effect.
It sets a timeout using js/setTimeout that waits for a specified duration (500 milliseconds in the example) and then removes the shake class from the element, stopping the screenshake animation.
Here is the ::trigger-shake reg-fx broken down:
clojure
(rf/reg-fx
::trigger-shake
(fn []
(let [root (.querySelector js/document ".main-style")] ; Find the element with the class "main-style"
(.classList.add root "shake") ; Add the "shake" class to trigger the animation
(js/setTimeout ; Set a timer to remove the class after 500ms
#(when root
(.classList.remove root "shake")) ; Remove the "shake" class which stops the animation
500))))
It is necessary to remove the shake class after the animation completes, or else the shake CSS class would remain on the element, which could have side effects such as preventing it from being triggered again, or interfering with other styles or elements.
Now, why is the JavaScript interop necessary? While you could simply toggle the shake class within your ClojureScript event handler and render functions, doing so would only work for the initial animation trigger. To reset the animation and allow it to be triggered again in the future, you need some way to remove the shake class after the animation has run its course. This resetting process is typically outside the realm of pure ClojureScript and requires some interaction with the browser's DOM and JavaScript's runtime environment (js), hence the JavaScript interop (js/setTimeout call).
Using effect handlers (reg-fx) in re-frame is a clean way to handle side effects such as these. It separates concerns, keeps your event handlers pure, and allows you to reuse the effect for other animations or effects if needed.