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

Sigrid Benezra - Edges - Litter Patrol #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ body {
margin: 5px;
cursor: pointer;

animation-duration: 4s;
animation-duration: 4s; /* orig 4s */
animation-name: item-move;
animation-timing-function: linear;

Expand Down
26 changes: 20 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,46 @@ class App extends Component {
super();

this.state = {
items: [],
items: [], /* item array keeps track of items and if they've been spotted */
points: 0,
};

// Uncomment this to spawn a single test item
//const testItem = this.spawnItem(Date.now());
//this.state.items.push(testItem);
// const testItem = this.spawnItem(Date.now());
// this.state.items.push(testItem);

// Uncomment this to automatically spawn new items
this.enableSpawner();

console.log(this.state);
// console.log(this.state);
}

onItemClicked = () => {
onItemClicked = (index) => {
// Fill this in!
// This click handler is passed via props to GameItem
console.log("Item was clicked");
// identify item clicked on
console.log(index);
// change it's state to spotted
let updatedItemData = this.state.items;
updatedItemData[index].wasSpotted = true;
this.setState({items: updatedItemData});
console.log(this.state.items[index]);
}

render() {
// mapping over item array from state to create array of GameItems
// array of GameItems to be returned in return statement
const items = this.state.items.map((item, i) => {
return <GameItem
height={item.height} // Height - used for a CSS style to position on the screen
layer={100 + i} // Layer - used for a CSS style to show items on-top of bg
key={item.id} // Key - to help React with performance

// Additional props (event callbacks, etc.) can be passed here
index={i}
type={item.type}
wasSpotted={false}
onClickCallback={this.onItemClicked}
/>;
});

Expand Down
36 changes: 33 additions & 3 deletions src/components/GameItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import PropTypes from 'prop-types';

class GameItem extends Component {

clickCallback = (e) => {
console.log("An item was clicked!");
console.log(this.props.index);
this.props.onClickCallback(this.props.index);
}

render() {
const itemStyle = {
Expand All @@ -13,11 +18,35 @@ class GameItem extends Component {
};

// Update this to select the correct icon for each item
const icon = ItemIcons.rock;
let icon;
let className = "game-item";

switch(this.props.type) {
case 'litter':
icon = ItemIcons.litter;
break;
case 'rock':
icon = ItemIcons.rock;
break;
case 'bush':
icon = ItemIcons.bush;
break;
case 'flower':
icon = ItemIcons.flower;
break;
default:
icon = ItemIcons.mushroom;
};

if (this.props.type === "litter" && this.props.wasSpotted) {
className = "game-item spotted-litter"
} else if (this.props.wasSpotted) {
className = "game-item spotted-nature";
}

return (
<div className="game-item" style={itemStyle}>
<img src={icon} alt="Item" className="icon-item"></img>
<div className={className} style={itemStyle} onClick={this.clickCallback}>
<img src={icon} alt="Item" className="icon-item" />
</div>
);
}
Expand All @@ -27,6 +56,7 @@ class GameItem extends Component {
GameItem.propTypes = {
height: PropTypes.number.isRequired,
layer: PropTypes.number.isRequired,
type: PropTypes.string,
}

export default GameItem;