racecar.lua 959 B

12345678910111213141516171819202122232425262728293031
  1. local drive_speed = 20
  2. local drive_distance = 30
  3. core.register_entity("soundstuff:racecar", {
  4. initial_properties = {
  5. physical = false,
  6. collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  7. selectionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  8. visual = "upright_sprite",
  9. visual_size = {x = 1, y = 1, z = 1},
  10. textures = {"soundstuff_racecar.png", "soundstuff_racecar.png^[transformFX"},
  11. static_save = false,
  12. },
  13. on_activate = function(self, _staticdata, _dtime_s)
  14. self.min_x = self.object:get_pos().x - drive_distance * 0.5
  15. self.max_x = self.min_x + drive_distance
  16. self.vel = vector.new(drive_speed, 0, 0)
  17. end,
  18. on_step = function(self, _dtime, _moveresult)
  19. local pos = self.object:get_pos()
  20. if pos.x < self.min_x then
  21. self.vel = vector.new(drive_speed, 0, 0)
  22. elseif pos.x > self.max_x then
  23. self.vel = vector.new(-drive_speed, 0, 0)
  24. end
  25. self.object:set_velocity(self.vel)
  26. end,
  27. })