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

Sockets Tatiana #43

Open
wants to merge 3 commits 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
41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,26 @@ class App extends Component {
console.log(this.state);
}

onItemClicked = () => {
// Fill this in!
onItemClicked = (itemType) => {
// only add points when the item clicked is litter
console.log(this.state.points)
return () => {
if (itemType === "litter") {
this.setState({
points: this.state.points += 1
})
}
}
}

render() {
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
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
iconName={item.type}
onItemClicked={this.onItemClicked}
/>;
});

Expand All @@ -63,7 +71,7 @@ class App extends Component {

<section className="level">
{ this.levelBackground() }
{ items }
{items}
</section>

</div>
Expand Down
24 changes: 20 additions & 4 deletions src/components/GameItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,39 @@ import ItemIcons from '../ItemIcons.js';
import PropTypes from 'prop-types';

class GameItem extends Component {
propTypes = {
static propTypes = {
height: PropTypes.number.isRequired,
layer: PropTypes.number.isRequired,
}

constructor(props) {
super(props)
this.state = {
addClickCss: ""
}
}

onNatureItemClick = () => {
this.setState(
{
addClickCss: this.props.iconName === "litter" ?

Choose a reason for hiding this comment

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

I think there is some room for improvement around your naming here.

onNatureItemClick is close to the convention, but not all of the game items are nature items (some are litter). Since we don't have any other click handlers to worry about, I might shorten the name of this to just onClick.

addClickCss isn't a great name for a piece of state. The word "add" makes it a verb phrase, which we usually reserve for functions. Something like overlayClass or clickedClass might be a better choice.

"spotted-litter" : "spotted-nature"
})
this.props.onItemClicked(this.props.iconName)()
}

render() {
const itemStyle = {
bottom: `${this.props.height}px`, // use props.height to offset from the bottom of screen
zIndex: this.props.layer, // use props.layer to set z-index, so we display ontop of background
};

// Update this to select the correct icon for each item
const icon = ItemIcons.rock;
const icon = ItemIcons[this.props.iconName];

return (
<div className="game-item" style={itemStyle}>
<img src={icon} alt="Item" className="icon-item"></img>
<div className={`game-item ${this.state.addClickCss}`} onClick={this.onNatureItemClick} style={itemStyle}>
<img src={icon} alt="Item" className="icon-item"></img>
</div>
);
}
Expand Down