Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TDDD27-Web-Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Simon Magnusson
TDDD27-Web-Project
Commits
7a09a461
Commit
7a09a461
authored
1 year ago
by
Simon Magnusson
Browse files
Options
Downloads
Patches
Plain Diff
clicking adds to the board now
parent
e79ff481
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
client/src/App.css
+1
-1
1 addition, 1 deletion
client/src/App.css
client/src/App.js
+15
-0
15 additions, 0 deletions
client/src/App.js
client/src/GameBoard.js
+20
-3
20 additions, 3 deletions
client/src/GameBoard.js
with
36 additions
and
4 deletions
client/src/App.css
+
1
−
1
View file @
7a09a461
...
...
@@ -14,7 +14,7 @@
}
.App-header
{
background-color
:
#
282c34
;
background-color
:
#
bcbcbc
;
min-height
:
100vh
;
display
:
flex
;
flex-direction
:
column
;
...
...
This diff is collapsed.
Click to expand it.
client/src/App.js
+
15
−
0
View file @
7a09a461
...
...
@@ -13,6 +13,21 @@ function App() {
<
header
className
=
"
App-header
"
>
<
div
style
=
{{
display
:
'
flex
'
,
flexDirection
:
'
column
'
,
justifyContent
:
'
center
'
,
padding
:
'
20px
'
}}
>
<
div
style
=
{{
boxShadow
:
'
0 4px 8px 0 rgba(0,0,0,0.2)
'
,
transition
:
'
0.3s
'
,
width
:
`
${
boardWidth
}
px`
,
// Dynamically set width
height
:
`100px`
,
margin
:
'
10px
'
,
position
:
'
relative
'
,
overflow
:
'
hidden
'
,
borderRadius
:
'
10px
'
,
// Rounded corners
}}
>
<
div
>
<
/div
>
<
/div
>
<
div
style
=
{{
boxShadow
:
'
0 4px 8px 0 rgba(0,0,0,0.2)
'
,
transition
:
'
0.3s
'
,
...
...
This diff is collapsed.
Click to expand it.
client/src/GameBoard.js
+
20
−
3
View file @
7a09a461
import
React
,
{
useState
,
useEffect
,
useRef
,
useCallback
}
from
'
react
'
;
const
GameBoard
=
()
=>
{
const
[
camera
,
setCamera
]
=
useState
({
x
:
0
,
y
:
0
});
const
[
camera
,
setCamera
]
=
useState
({
x
:
0
,
y
:
0
});
// camera pos is at the top right corner! good to keep in mind
const
[
dimensions
,
setDimensions
]
=
useState
({
width
:
0
,
height
:
0
});
const
gameBoardRef
=
useRef
(
null
);
const
dragStartRef
=
useRef
({
x
:
0
,
y
:
0
});
...
...
@@ -12,8 +12,10 @@ const GameBoard = () => {
const
[
isDragging
,
setIsDragging
]
=
useState
(
false
);
const
[
clickPositions
,
setClickPositions
]
=
useState
([]);
const
[
boardState
,
setBoardState
]
=
useState
([]);
// this is supposed to be integer values corresponding to the cells
const
cellSize
=
65
;
const
cellSize
=
65
;
// make variable at some point
// Update dimensions based on the parent container
const
updateDimensions
=
useCallback
(()
=>
{
...
...
@@ -67,6 +69,7 @@ const GameBoard = () => {
if
(
clickX
>=
0
&&
clickX
<=
gameBoardRect
.
width
&&
clickY
>=
0
&&
clickY
<=
gameBoardRect
.
height
)
{
setClickedAt
({
x
:
clickX
+
camera
.
x
,
y
:
clickY
+
camera
.
y
});
setClickPositions
(
oldPositions
=>
[...
oldPositions
,
{
x
:
clickX
+
camera
.
x
,
y
:
clickY
+
camera
.
y
}]);
// add the camera as like a translation bruh
addToBoard
(
clickX
+
camera
.
x
,
clickY
+
camera
.
y
);
}
}
setIsMouseDown
(
false
);
...
...
@@ -83,6 +86,17 @@ const GameBoard = () => {
};
},
[
isMouseDown
,
isDragging
]);
function
addToBoard
(
x
,
y
)
{
// Get pos on the board when clicking!
var
floor
=
Math
.
floor
;
var
boardPosX
=
floor
(
Number
(
x
/
cellSize
));
var
boardPosY
=
floor
(
Number
(
y
/
cellSize
));
console
.
log
(
boardPosX
,
boardPosY
);
setBoardState
(
oldState
=>
[...
oldState
,
{
x
:
boardPosX
,
y
:
boardPosY
}])
}
// Adjusted render function to use component dimensions
const
renderGameBoard
=
()
=>
{
const
startCol
=
Math
.
floor
(
camera
.
x
/
cellSize
);
...
...
@@ -102,7 +116,10 @@ const GameBoard = () => {
<
svg
width
=
{
dimensions
.
width
}
height
=
{
dimensions
.
height
}
style
=
{{
display
:
'
block
'
}}
>
{
lines
}
{
clickPositions
.
map
((
pos
,
index
)
=>
(
<
circle
key
=
{
index
}
cx
=
{
pos
.
x
-
camera
.
x
}
cy
=
{
pos
.
y
-
camera
.
y
}
r
=
"
10
"
fill
=
"
red
"
/>
<
circle
key
=
{
index
}
cx
=
{
pos
.
x
-
camera
.
x
}
cy
=
{
pos
.
y
-
camera
.
y
}
r
=
"
5
"
fill
=
"
red
"
/>
))}
{
boardState
.
map
((
pos
,
index
)
=>
(
<
circle
key
=
{
index
}
cx
=
{
pos
.
x
*
cellSize
+
cellSize
/
2
-
camera
.
x
}
cy
=
{
pos
.
y
*
cellSize
+
cellSize
/
2
-
camera
.
y
}
r
=
"
10
"
fill
=
"
blue
"
/>
))}
<
/svg
>
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment