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

Jane's litter-patrol #44

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
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: 8s;
animation-name: item-move;
animation-timing-function: linear;

Expand Down
53 changes: 33 additions & 20 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,55 @@ class App extends Component {
};

// 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);
}

onItemClicked = () => {
onItemClicked = (index) => {
// Fill this in!
let updatedPoints = this.state.points;
let updatedItems = this.state.items;
const item = this.state.items[index]

if(item.type === "litter" && !(item.clicked)){
updatedPoints += 1
updatedItems[index].clicked = true
this.setState({ points: updatedPoints });
this.setState({ items: updatedItems });
}
}

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
/>;
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
type={item.type}
pointsCallback={this.onItemClicked}
index={i}
/>;
});

return (
<div className="game">
<section className="hud">
<h2 className="score">Litter Spotted: { this.state.points }</h2>
<img className="logo" src={logo} alt="Litter Patrol logo" />
</section>
<section className="hud">
<h2 className="score">Litter Spotted: { this.state.points }</h2>
<img className="logo" src={logo} alt="Litter Patrol logo" />
</section>

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

</div>
);
Expand Down Expand Up @@ -140,7 +154,6 @@ class App extends Component {
choice -= weight; // otherwise move past this entry
}
});

return selectedType;
}

Expand All @@ -150,10 +163,10 @@ class App extends Component {

levelBackground() {
const layers = ['clouds-1', 'clouds-2', 'clouds-3', 'clouds-4',
'hills-1','hills-2','bushes','trees-1','trees-2','ground'];
'hills-1','hills-2','bushes','trees-1','trees-2','ground'];
return (
<div className="level-bg">
{layers.map(layer => (<div className={`level-bg-${layer}`} key={layer} />))}
{layers.map(layer => (<div className={`level-bg-${layer}`} key={layer} />))}
</div>
);
}
Expand Down
24 changes: 21 additions & 3 deletions src/components/GameItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@ import ItemIcons from '../ItemIcons.js';
import PropTypes from 'prop-types';

class GameItem extends Component {
constructor() {
super();
this.state = {
litterSpottedClass: "",
};
}

spotLitter = () =>{
if (this.props.type === "litter"){
this.setState({ litterSpottedClass: "spotted-litter" });
} else {
this.setState({ litterSpottedClass: "spotted-nature" });
}
this.props.pointsCallback(this.props.index)
}

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

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

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