Skip to content

Commit

Permalink
refactor addCharacter
Browse files Browse the repository at this point in the history
  • Loading branch information
karlyanelson committed Jan 13, 2021
1 parent dfacf33 commit 51ab82e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/js/handlers/clickHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function clickHandler(event) {
}

if (buttonTarget.matches("#addCharacter")) {
addCharacter(DATA_STORE);
addCharacter();
}

if (buttonTarget.matches("[data-zoom]")) {
Expand Down
13 changes: 7 additions & 6 deletions src/js/utils/addCharacter.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { generateRandomID } from "./utils";
import { DEFAULT_COLOR } from "../globals/variables";
import DATA_STORE from "../globals/store";

export default function addCharacter(store) {
const numOfCharacters = (store.data.characters.length + 1).toString();
export default function addCharacter() {
const numOfCharacters = (DATA_STORE.data.characters.length + 1).toString();

const zoomRatio = store.data.zoom / 100;
const zoomRatio = DATA_STORE.data.zoom / 100;
const positiveOrNegative = Math.random() < 0.5 ? -1 : 1;
const randomNumberX =
Math.floor(Math.random() * Math.floor(40)) * positiveOrNegative;
const randomNumberY =
Math.floor(Math.random() * Math.floor(40)) * positiveOrNegative;
const posX =
(window.innerWidth / 2 + window.pageXOffset) / zoomRatio -
store.data.pieceSize / 2 +
DATA_STORE.data.pieceSize / 2 +
randomNumberX;
const posY =
(window.innerHeight / 2 + window.pageYOffset) / zoomRatio -
store.data.pieceSize / 2 +
DATA_STORE.data.pieceSize / 2 +
randomNumberY;

var newCharacter = {
Expand All @@ -34,5 +35,5 @@ export default function addCharacter(store) {
y: posY,
size: 1,
};
store.data.characters.push(newCharacter);
DATA_STORE.data.characters.push(newCharacter);
}

0 comments on commit 51ab82e

Please sign in to comment.