projects.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { REST } from 'api/rest-axios'
  2. export default {
  3. namespaced: true,
  4. // initial state
  5. state: {
  6. projects: []
  7. },
  8. // getters
  9. getters: {
  10. getGridPos: (state) => (index) => {
  11. let popped = state.projects[index].grid.pop()
  12. console.log('getGridPos: popped', popped)
  13. return popped
  14. }
  15. },
  16. // mutations
  17. mutations: {
  18. setProjects (state, projects) {
  19. // record the retrieved data
  20. state.projects = projects
  21. // randomly define building sizes
  22. let totalW = 0
  23. for (var i = 0; i < state.projects.length; i++) {
  24. let w = Math.round(21 + Math.random() * 15)
  25. totalW += w
  26. state.projects[i].size = {
  27. x: w,
  28. y: Math.round(100 + Math.random() * 250),
  29. z: Math.round(10 + Math.random() * 30)
  30. }
  31. }
  32. // positioning buildings on x regarding the widths
  33. // & setting up the window sizing
  34. // & setting up the content grid
  35. let margin = 5
  36. totalW += margin * (state.projects.length - 1)
  37. let wall, a
  38. let grid
  39. // fro each PROJECTS
  40. for (var j = 0; j < state.projects.length; j++) {
  41. // X POS
  42. let x = -1 * totalW / 2// + state.projects[0].size.x / 2
  43. for (var k = 0; k < j; k++) {
  44. x += state.projects[k].size.x
  45. }
  46. x += margin * j
  47. x += state.projects[j].size.x / 2
  48. state.projects[j].position = {
  49. x: x,
  50. y: -1 * state.projects[j].size.y / 2 + 10 + Math.random() * 30, // -10 + Math.random() * this.size.y / 2
  51. z: -10 + Math.random() * 10
  52. }
  53. // WINDOWS
  54. wall = {
  55. wallW: 0.001,
  56. // dig windows on face and back
  57. winW: 2 + Math.random() * 2,
  58. winH: 4 + Math.random() * 4,
  59. margin: 2,
  60. nbrWinX: 0,
  61. paddingX: 0,
  62. nbrWinY: 0,
  63. paddingY: 0,
  64. nbrWinZ: 0,
  65. paddingZ: 0
  66. }
  67. // removing windows on X until padding is enough
  68. a = 0
  69. wall.nbrWinX = Math.floor((state.projects[j].size.x - 2 * wall.margin) / wall.winW)
  70. while (wall.paddingX < 0.4) {
  71. wall.nbrWinX -= a
  72. wall.paddingX = (state.projects[j].size.x - 2 * wall.margin - wall.winW * wall.nbrWinX) / (wall.nbrWinX - 1)
  73. a++
  74. }
  75. // removing windows on Y until padding is enough
  76. a = 0
  77. wall.nbrWinY = Math.floor((state.projects[j].size.y - 2 * wall.margin) / wall.winH)
  78. while (wall.paddingY < 0.4) {
  79. wall.nbrWinY -= a
  80. wall.paddingY = (state.projects[j].size.y - 2 * wall.margin - wall.winH * wall.nbrWinY) / (wall.nbrWinY - 1)
  81. a++
  82. }
  83. state.projects[j].wall = { ...wall }
  84. // CONTENTS GRID
  85. a = 0
  86. wall.nbrWinZ = Math.floor((state.projects[j].size.z - 2 * wall.margin) / wall.winW)
  87. while (wall.paddingZ < 0.4) {
  88. wall.nbrWinZ -= a
  89. wall.paddingZ = (state.projects[j].size.z - 2 * wall.margin - wall.winW * wall.nbrWinZ) / (wall.nbrWinZ - 1)
  90. a++
  91. }
  92. grid = []
  93. for (var l = 0; l < wall.nbrWinZ * 2; l++) { // cols
  94. for (var m = 0; m < wall.nbrWinY * 2; m++) { // rows
  95. grid.push({
  96. z: margin + wall.winW / 2 * l,
  97. y: margin + wall.winH / 2 * m
  98. })
  99. }
  100. }
  101. // shuffle the grid
  102. for (let n = grid.length - 1; n > 0; n--) {
  103. const o = Math.floor(Math.random() * n)
  104. const temp = grid[n]
  105. grid[n] = grid[o]
  106. grid[o] = temp
  107. }
  108. state.projects[j].grid = [ ...grid ]
  109. }
  110. }
  111. },
  112. // actions
  113. actions: {
  114. // async get authors
  115. getProjects ({ dispatch, commit, state }) {
  116. REST.get(`projects`, {})
  117. .then(({ data }) => {
  118. console.log('projects getProjects REST: data', data)
  119. commit('setProjects', data)
  120. })
  121. .catch((error) => {
  122. console.warn('Issue with getProjects', error)
  123. Promise.reject(error)
  124. })
  125. }
  126. }
  127. }