การออกแบบเกมแนว Farmville อย่างง่ายด้วย Spring Boot

เกมแนว Farmville มีความนิยมอย่างแพร่หลายเนื่องจากกลไกที่เรียบง่ายแต่ดึงดูดใจผู้เล่น เช่น การหว่านเมล็ด การเติบโตของพืชผล และการเก็บเกี่ยวเพื่อทำกำไร ในบทความนี้ เราจะสำรวจวิธีการสร้างเกมแนว Farmville อย่างง่ายโดยใช้ Spring Boot ซึ่งเป็น Framework สำหรับการพัฒนา Backend ที่มีความยืดหยุ่นและทรงพลัง

1. โครงสร้างโปรเจกต์

เริ่มต้นด้วยการสร้างโครงสร้างโปรเจกต์พื้นฐาน:

  • Spring Boot Application: ใช้ Spring Initializr เพื่อสร้างโปรเจกต์
  • Dependencies ที่จำเป็น:
    • Spring Web (สำหรับ REST APIs)
    • Spring Data JPA (สำหรับการจัดการฐานข้อมูล)
    • H2 Database (ฐานข้อมูลในหน่วยความจำสำหรับการทดสอบ)

2. การจัดการเวลา

ในเกมแนวนี้ ระบบเวลามีบทบาทสำคัญ เช่น การกำหนดเวลาให้พืชเติบโตหรือการรอคอยสำหรับการเก็บเกี่ยว เราสามารถใช้ ScheduledExecutorService ของ Java หรือ @Scheduled ของ Spring Boot เพื่อจัดการงานตามเวลาที่กำหนด

@Component
public class TimeManager {

    @Autowired
    private CropService cropService;

    @Scheduled(fixedRate = 60000) // เรียกทุก 1 นาที
    public void updateCropStatus() {
        cropService.updateGrowthStatus();
    }
}

ใน CropService จะมีเมธอด updateGrowthStatus() ที่ตรวจสอบและปรับปรุงสถานะของพืช:

@Service
public class CropService {

    @Autowired
    private CropRepository cropRepository;

    public void updateGrowthStatus() {
        List crops = cropRepository.findAll();
        for (Crop crop : crops) {
            if (!crop.isHarvested() && crop.getPlantedAt().plusMinutes(crop.getGrowthTimeInMinutes()).isBefore(LocalDateTime.now())) {
                crop.setReadyToHarvest(true);
                cropRepository.save(crop);
            }
        }
    }
}

3. การออกแบบ Entity

สร้าง Entity ที่เกี่ยวข้อง เช่น FarmCrop, และ Player

@Entity
public class Crop {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private LocalDateTime plantedAt;
    private int growthTimeInMinutes; // ระยะเวลาที่ใช้ในการเติบโต
    private boolean isHarvested;

    // Getters และ Setters
}

4. ระบบหว่านเมล็ด

เมื่อผู้เล่นหว่านเมล็ด ระบบจะบันทึกข้อมูล เช่น เวลาเริ่มต้นการปลูก (plantedAt) และระยะเวลาที่พืชต้องเติบโต (growthTimeInMinutes)

@RestController
@RequestMapping("/api/farm")
public class FarmController {

    @Autowired
    private CropRepository cropRepository;

    @PostMapping("/plant")
    public ResponseEntity<String> plantCrop(@RequestBody Crop crop) {
        crop.setPlantedAt(LocalDateTime.now());
        crop.setHarvested(false);
        cropRepository.save(crop);
        return ResponseEntity.ok("Planted " + crop.getName() + " successfully!");
    }
}

5. การเติบโตและการเก็บเกี่ยว

ตรวจสอบว่าพืชพร้อมสำหรับการเก็บเกี่ยวโดยคำนวณจากเวลาเริ่มต้นการปลูกและระยะเวลาที่กำหนด

@Service
public class CropService {

    @Autowired
    private CropRepository cropRepository;

    public List<Crop> getReadyToHarvest() {
        return cropRepository.findAll().stream()
            .filter(crop -> !crop.isHarvested() && 
                crop.getPlantedAt().plusMinutes(crop.getGrowthTimeInMinutes()).isBefore(LocalDateTime.now()))
            .collect(Collectors.toList());
    }

    public String harvestCrop(Long cropId) {
        Crop crop = cropRepository.findById(cropId)
            .orElseThrow(() -> new RuntimeException("Crop not found"));
        if (!crop.isHarvested() && crop.getPlantedAt().plusMinutes(crop.getGrowthTimeInMinutes()).isBefore(LocalDateTime.now())) {
            crop.setHarvested(true);
            cropRepository.save(crop);
            return "Successfully harvested " + crop.getName();
        }
        return "Crop is not ready for harvesting!";
    }
}

6. ระบบการขาย

เมื่อเก็บเกี่ยวพืช ผู้เล่นสามารถขายผลผลิตเพื่อรับเงินหรือทรัพยากรเพิ่มเติม

@RestController
@RequestMapping("/api/farm")
public class MarketController {

    @Autowired
    private CropRepository cropRepository;

    @PostMapping("/sell")
    public ResponseEntity<String> sellCrop(@RequestParam Long cropId) {
        Crop crop = cropRepository.findById(cropId)
            .orElseThrow(() -> new RuntimeException("Crop not found"));
        if (crop.isHarvested()) {
            cropRepository.delete(crop); // ขายแล้วลบออกจากฐานข้อมูล
            return ResponseEntity.ok("Sold " + crop.getName() + " successfully!");
        }
        return ResponseEntity.badRequest().body("Crop is not ready for sale!");
    }
}

สรุป

การออกแบบเกมแนว Farmville ด้วย Spring Boot นั้นสามารถเริ่มต้นได้ง่ายด้วยโครงสร้างพื้นฐานที่ชัดเจน เช่น การจัดการเวลา การออกแบบ Entity และ API สำหรับการหว่านเมล็ด เก็บเกี่ยว และขาย การเพิ่มฟีเจอร์เพิ่มเติม เช่น ระบบเงินหรือการปรับแต่งฟาร์ม จะช่วยเพิ่มความสนุกให้กับเกมของคุณ!

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

รับติดตั้งระบบป้ายโฆษณาดิจิตอล Digital Signage ตั้งเวลาทำงานและควบคุมผ่าน Internet ได้

แจกฟรี ระบบแสดงคิวงาน สำหรับร้านนวด สปา หรือ ร้านทำผม

บริการออกแบบ Character Agent สำหรับธุรกิจ

ป้ายดิจิตอลที่โต้ตอบได้