Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Feature/wombat animation #361

Open
wants to merge 28 commits into
base: develop
Choose a base branch
from
Open

Conversation

elibosley
Copy link
Contributor

PR for #274 .

PR Status

IN DEVELOPMENT

Ready for PR?

  • I have updated the CHANGELOG with an entry including a description, a link to my profile, and a link to the issue ticket. This change is filed under an Enhancement or Bug Fix, is shown under the relevant release tag name, and is included in my PR branch.
  • I have run lein run-lint to check for linting errors.

Changes Proposed:

Breaking changes?

Screenshot of feature:

@emilyseibert @oconn @dehli

@elibosley elibosley requested a review from dehli August 10, 2017 15:06
@dehli dehli changed the title Feature/wombat animation WIP: Feature/wombat animation Aug 10, 2017
;; Iterate through all of the arena rows
(doseq [[y row] (map-indexed vector arena)]
(doseq [[x cell] (map-indexed vector row)]
(let
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put x-coord on this line.

"Given the start and the end dimension object, calculate which direction
has movement"
[start end]
(if (pos? (Math/abs (- (:x start) (:x end))))
Copy link
Contributor

@dehli dehli Aug 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is a bit confusing and would probably be easier to follow like:

(defn- get-direction-key
  [{x0 :x} {x1 :x}] ;; you could also use [start end] here 
  (if (= x0 x1)     ;; and then pull out the values here.
    :y
    :x))

width (/ (canvas/width canvas-element) (count (get arena 0)))]

;; Iterate through all of the arena rows
(doseq [[y row] (map-indexed vector arena)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think this could still be done cleaner by combining draw-arena-canvas into draw-arena-canvas-animated.

I think the best way would be to sort all of the elements by the order that they should be drawn (such that the animated elements are always drawn after the non-animated). You would need to merge in their x/y coordinates. Then you can simply draw the element at the correct location. Does that make sense?

@dehli
Copy link
Contributor

dehli commented Aug 11, 2017

What's the reasoning for still having the two for loops?

@elibosley
Copy link
Contributor Author

elibosley commented Aug 11, 2017

@dehli the draw arena method is still being used - I haven't depreciated it yet so the dual loops are still used for rendering the game-play, not the simulator.

(* height y))}
{:x (if (= direction-key :x)
(* width
(+ -1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dec should work here.

@elibosley
Copy link
Contributor Author

@dehli What's the status with this PR? If you get a chance to review this in the next few weeks, I'd be more than happy to update the code and make sure everything is good to go!

@dehli
Copy link
Contributor

dehli commented Aug 18, 2017

Hey @elibosley! It's on my backlog. I will get around to this either this weekend, or sometime next week! Hope you had a good week! 😄

@elibosley
Copy link
Contributor Author

Awesome! Thanks, I had a great week! 😆

Copy link
Contributor

@dehli dehli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @elibosley! Here's some suggestions. I think it's a good start! Feel free to address these whenever you have time and I'll do another review afterwards. Good luck with classes!

;; Draw background first
(draw-background canvas-element x y width height)
(when (= background true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just use (when background


(let [{contents :contents
meta :meta} cell
cell-type (:type contents)]

(when (nil? cell-type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these prn calls.

;; step size is neg if moving up or left, so wrap to the largest dimension,
;; this logic is reversed because step-size is calculated as the opposite
;; sign when wrapping occurs
(if (pos? step-size)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a lot easier to follow if you had the (pos? step-size when you actually calculate the values for :x and :y. Additionally, it would allow you to remove code that you have replicated.

"Add local :x and :y coordinates to arena matrix"
[arena]
(map-indexed
(fn [y row] (map-indexed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is off in this function.

(get-in item [:contents :uuid]))

(defn- dimensions
[item]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function isn't needed since item already has the proper attributes.

Copy link
Contributor Author

@elibosley elibosley Oct 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does - but I need the previous dimensions to be stored somewhere - I think I figured it was easier to just pull both at once. Would you suggest putting all of that information on the :cell attribute for the vector?

Answered down below

;; Get the coordinates of the animated items on the grid
prev-coords (filter-arena prev-frame-locs [:zakano :wombat])
next-coords (filter-arena next-frame-locs [:zakano :wombat])
canvas-element (.getElementById js/document canvas-id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to use ref instead of (.getElementById

;; Need to get two sets of coordinates -
;; one to run wombat off the screen, one to wrap the new wombat on

(defn- draw-arena-canvas-animated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method draws all cells (not just animated). If so, it would make sense to rename this method.

:step-size step-size})]
;; Draw a blank tile before the animated item to fix
;; weird drawing issues
(draw-background
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to draw a blank tile if the elements are ordered according to when they should be drawn.

;; this when subtracts one from the end because the animation runs once
;; before it gets a callback
(when (<= (:progress animation-progress) (dec (:end animation-progress)))
(.requestAnimationFrame js/window
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be cleaner to just do a forceUpdate every animation frame so that the arena completely rerenders.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to do this - would you replace the .requestAnimationFrame ? and what would I call force-update on to make sure I could increment the animation progress?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm thinking (might not actually work) is doing the following:

(.requestAnimationFrame js/window
                        (.forceUpdate this))

Then you're triggering a rerender every animation frame so you don't need to have any logic inside of the callback. Does that make sense?

total-step-size (get-step-size start end direction-key)
step-size (/ total-step-size frame-time)]
(if (> (Math/abs total-step-size) 1) ;; wrap-around
(let [cell-coords (get-wrapped-coord
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have all these coords? I'd imagine just two would be enough for wrapping (current coord, and wrapped coord), otherwise we'd just need one coordinate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So one set of coords draws the wombat out of the frame and the other one draws it in - that was the best way that I could see to have the transition be smooth - lmk what you think!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess my question is what is cell-coords, vs. wrapped-coords, vs. same-side-wrap. Makes sense though!

@elibosley
Copy link
Contributor Author

Ugh so I'm finally getting around to this and having crazy setup issues with Ubuntu - hopefully I can figure out what's wrong with my configuration but I just figured I'd update you about it!

@dehli
Copy link
Contributor

dehli commented Sep 27, 2017

Sounds good! @elibosley Let me know if you need anything!

@elibosley
Copy link
Contributor Author

@dehli Should I fork this repo to push my commit back up? I've updated the code a bit but I'm not sure how I should get it back into a PR

@dehli
Copy link
Contributor

dehli commented Oct 3, 2017

Hey @elibosley! You can just push directly to this branch and it will update the PR appropriately 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants